Skip to content

Commit

Permalink
include names of all parents for modules
Browse files Browse the repository at this point in the history
Signed-off-by: William Wedler <wwedler@riseup.net>
  • Loading branch information
zflat committed Aug 17, 2023
1 parent 3bc47a5 commit 7c3eac6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion launch/launch/substitutions/python_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ def perform(self, context: LaunchContext) -> Text:
"""Perform the substitution by evaluating the expression."""
from ..utilities import perform_substitutions
module_names = [context.perform_substitution(sub) for sub in self.python_modules]
module_objects = [importlib.import_module(name) for name in module_names]
# Get names of package and all parents for modules in the list of python_modules
# For example, python module "a.b.c" has parents "a.b" and "a"
inherited_names = [".".join(p[0:n]) for n in range(1, len(p)) for p in
[name.split('.') for name in module_names]]
all_module_names = list(set(module_names + inherited_names))
module_objects = [importlib.import_module(name) for name in all_module_names]
expression_locals = {}
for module in module_objects:
# For backwards compatility, we allow math definitions to be implicitly
Expand Down
24 changes: 24 additions & 0 deletions launch/test/launch/substitutions/test_python_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,27 @@ def test_python_substitution_two_modules():
# Test the describe() method
assert subst.describe() ==\
"PythonExpr('math.isfinite(sys.getrefcount(str(\"hello world!\")))', ['sys', 'math'])"


def test_python_substitution_submodule():
"""Evaluation while passing modules within a package."""
python_modules = [
'launch.substitutions.PythonExpression',
'launch.substitutions.SubstitutionFailure',
'launch.utilities.is_a_subclass'
]
lc = LaunchContext()
expr = (
f'not launch.utilities.is_a_subclass('
f' launch.substitutions.PythonExpression,'
f' launch.substitutions.SubstitutionFailure'
f')'
)
subst = PythonExpression([expr], python_modules)
try:
result = subst.perform(lc)
except SubstitutionFailure:
pytest.fail('Failed to evaluate PythonExpression containing sys module.')

# The expression should evaluate to True
assert result

0 comments on commit 7c3eac6

Please sign in to comment.