Skip to content

Expand Aliases when executing Evaluate Keyword #5428

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
33 changes: 33 additions & 0 deletions src/robot/libraries/BuiltIn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3781,6 +3781,39 @@ def evaluate(self, expression, modules=None, namespace=None):
evaluation namespace. It needed to be taken into use explicitly like
``modules=rootmod, rootmod.submod``.
"""

"""
If Libraries are aliased when imported, substitute alias for actual library
This is so the correct library can be passed to the python execution
The first for loop is to ensure there are no ambiguous substitutions
The second executes the substitutions for any assumed object dereference
operations
Currently this will execute the substitution inside a string
Example:
Library foo AS BAR
Evaluate BAR.baz() --> Evaluate foo.baz()

Library foo AS BAR
Evaluate BAR.BAR.baz() --> Evaluate foo.BAR.baz()
"""
libraries = self._namespace._kw_store.libraries
for library in libraries.values():
for name, module_obj in libraries.items():
if name != library.name and (
module_obj.real_name == library.name or
module_obj.real_name == library.real_name):
raise DataError("Evaluate Expression failed: "
f"Ambiguous aliases with Library '{name}' "
f"As '{module_obj.real_name}' and Library "
f"'{library.name}' As '{library.real_name}'")
for library in libraries.values():
if library.name != library.real_name:
escaped_name = re.escape(library.name)
search_regex = re.compile(r'(?<!\.)' + escaped_name + r'\.')
if re.search(search_regex, expression):
expression = re.sub(search_regex,
f"{library.real_name}.",
expression)
try:
return evaluate_expression(
expression,
Expand Down