Skip to content

Commit

Permalink
Prevent raw exception during project()
Browse files Browse the repository at this point in the history
If a user imports a module and invokes a method on it,
a raw Python exception is raised to the user. This commit
adds a check to ensure that in this case an appropriate
exception is raised instead.

A test has been added to ensure that this exception is
in fact raised on offending code.

Fixes mesonbuild#11393
  • Loading branch information
amcn committed Sep 7, 2024
1 parent 53e1148 commit 476d058
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mesonbuild/interpreter/interpreterobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,9 @@ def method_call(self, method_name: str, args: T.List[TYPE_var], kwargs: TYPE_kwa
args = flatten(args)
if not getattr(method, 'no-second-level-holder-flattening', False):
args, kwargs = resolve_second_level_holders(args, kwargs)
if len(self.interpreter.active_projectname) == 0:
full_method_name = f'{modobj.INFO.name}.{method_name}'
raise mesonlib.MesonException(f'Module methods ({full_method_name}) cannot be invoked during project declaration.')
state = ModuleState(self.interpreter)
# Many modules do for example self.interpreter.find_program_impl(),
# so we have to ensure they use the current interpreter and not the one
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# GH issue 11393
project('module use inside project decl', 'c',
version: run_command(
import('python').find_installation('python3')
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"stdout": [
{
"line": "test cases/failing/132 module use inside project decl/meson.build:4:21: ERROR: Module methods (python.find_installation) cannot be invoked during project declaration."
}
]
}

0 comments on commit 476d058

Please sign in to comment.