Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python expressions substitution include names of all parents for specified modules #688

Open
wants to merge 1 commit into
base: rolling
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.')
zflat marked this conversation as resolved.
Show resolved Hide resolved

# The expression should evaluate to True
assert result