Skip to content

Commit 6024df4

Browse files
committed
include names of all parents for modules
Signed-off-by: William Wedler <[email protected]>
1 parent b129eb6 commit 6024df4

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

launch/launch/substitutions/python_expression.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ def perform(self, context: LaunchContext) -> Text:
9595
"""Perform the substitution by evaluating the expression."""
9696
from ..utilities import perform_substitutions
9797
module_names = [context.perform_substitution(sub) for sub in self.python_modules]
98-
module_objects = [importlib.import_module(name) for name in module_names]
98+
# Get names of package and all parents for modules in the list of python_modules
99+
# For example, python module "a.b.c" has parents "a.b" and "a"
100+
inherited_names = [".".join(p[0:n]) for n in range(1, len(p)) for p in
101+
[name.split('.') for name in module_names]]
102+
all_module_names = list(set(module_names + inherited_names))
103+
module_objects = [importlib.import_module(name) for name in all_module_names]
99104
expression_locals = {}
100105
for module in module_objects:
101106
# For backwards compatility, we allow math definitions to be implicitly

launch/test/launch/substitutions/test_python_expression.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,27 @@ def test_python_substitution_two_modules():
114114
# Test the describe() method
115115
assert subst.describe() ==\
116116
"PythonExpr('math.isfinite(sys.getrefcount(str(\"hello world!\")))', ['sys', 'math'])"
117+
118+
119+
def test_python_substitution_submodule():
120+
"""Evaluation while passing modules within a package."""
121+
python_modules = [
122+
'launch.substitutions.PythonExpression',
123+
'launch.substitutions.SubstitutionFailure',
124+
'launch.utilities.is_a_subclass'
125+
]
126+
lc = LaunchContext()
127+
expr = (
128+
f'not launch.utilities.is_a_subclass('
129+
f' launch.substitutions.PythonExpression,'
130+
f' launch.substitutions.SubstitutionFailure'
131+
f')'
132+
)
133+
subst = PythonExpression([expr], python_modules)
134+
try:
135+
result = subst.perform(lc)
136+
except SubstitutionFailure:
137+
pytest.fail('Failed to evaluate PythonExpression containing sys module.')
138+
139+
# The expression should evaluate to True
140+
assert result

0 commit comments

Comments
 (0)