Skip to content

Commit c726ca4

Browse files
amcneli-schwartz
authored andcommitted
Prevent raw exception during project()
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, mesonbuild#5134 (cherry picked from commit 74dd77e)
1 parent b252b34 commit c726ca4

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

mesonbuild/interpreter/interpreterobjects.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .. import build
1313
from .. import mlog
1414

15-
from ..modules import ModuleReturnValue, ModuleObject, ModuleState, ExtensionModule
15+
from ..modules import ModuleReturnValue, ModuleObject, ModuleState, ExtensionModule, NewExtensionModule
1616
from ..backend.backends import TestProtocol
1717
from ..interpreterbase import (
1818
ContainerTypeInfo, KwargInfo, MesonOperator,
@@ -844,6 +844,10 @@ def method_call(self, method_name: str, args: T.List[TYPE_var], kwargs: TYPE_kwa
844844
args = flatten(args)
845845
if not getattr(method, 'no-second-level-holder-flattening', False):
846846
args, kwargs = resolve_second_level_holders(args, kwargs)
847+
if not self.interpreter.active_projectname:
848+
assert isinstance(modobj, (ExtensionModule, NewExtensionModule)), 'for mypy'
849+
full_method_name = f'{modobj.INFO.name}.{method_name}'
850+
raise mesonlib.MesonException(f'Module methods ({full_method_name}) cannot be invoked during project declaration.')
847851
state = ModuleState(self.interpreter)
848852
# Many modules do for example self.interpreter.find_program_impl(),
849853
# so we have to ensure they use the current interpreter and not the one
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# GH issue 11393
2+
project('module use inside project decl', 'c',
3+
version: run_command(
4+
import('python').find_installation('python3')
5+
)
6+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"stdout": [
3+
{
4+
"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."
5+
}
6+
]
7+
}

0 commit comments

Comments
 (0)