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
Changes to supported evaluation syntax in Python 3 #3207
Comments
The problem isn't in the It seems there's been some small change in how Python 3 handles local variables when list comprehension is evaluated with >>> d = {'a': 1, 'b': 2}
>>> [d[k] for k in d]
[1, 2]
>>> eval('[d[k] for k in d]')
[1, 2]
>>> eval('[x[k] for k in x]', {'x': d})
[1, 2]
>>> eval('[x[k] for k in x]', {}, {'x': d})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
File "<string>", line 1, in <listcomp>
NameError: name 'x' is not defined The above was run using Python 3 but with Python 2 also the last This change looks really strange especially when local variables work just fine when using a list comprehension without eval: >>> def func1():
... x = {'a': 1, 'b': 2}
... return [x[k] for k in x]
...
>>> def func2():
... x = {'a': 1, 'b': 2}
... return eval('[x[k] for k in x]')
...
>>> func1()
[2, 1]
>>> func2()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in func2
File "<string>", line 1, in <module>
File "<string>", line 1, in <listcomp>
NameError: name 'x' is not defined
>>> And even more strange is that possible global variables change the behavior: >>> x = {'a': 'new', 'b': 'values'}
>>> func1()
[2, 1]
>>> func2()
['values', 'new'] This is most likely somehow related to changes handling internal variables in list comprehensions in Python 3. The behavior is so strange that it looks like a bug to me, but it's possible the change is by design. Need to study this a bit more and possibly submit a bug to Python. |
Notice that because the behavior comes directly from Python, we cannot really change it with Robot. Good news is that the original example can be changed to this:
This approach also works with Python: >>> d = {'a': 1, 'b': 2}
>>> eval('[x[k] for k in x]', {}, {'x': d})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
File "<string>", line 1, in <listcomp>
NameError: name 'x' is not defined
>>> eval('[v for v in x.values()]', {}, {'x': d})
[2, 1] I'll nevertheless leave this issue open until we know the root cause for the change in Python. |
This seems to be a known issue in Python 3: https://bugs.python.org/issue36300. Unfortunately it seems there are no plans to fix it. Anyway, there's nothing Robot Framework can do about it. |
Would it be possible to fix this on RFW side by adding the variables to the globals?
Or are there any security issues with this? |
Does that fix this issue? Have you checked how would it work with the current code? There have been changes. |
Oh... Yes, sorry. I've seen the implementation has changed (we are using RFW 3.0.4). I'll check with latest RFW later. |
Ok. I checked in version 3.2b3dev1 and created a namespace dictionary for usage as globals in eval().
Adding this dictionary as globals and ommiting locals did work for our special dict comprehension case, but
Adding globals and leaving locals as they are In our case, the Evaluation failed with RFW 3.0.4 in Python 2.7.6 as well as RFW 3.2b3dev1 in Python 3.6.7., but the workaround fixed it in 3.2b3dev1.
Maybe you have other examples to check this. And I really don't know if this change will have some negative influences to other things like security or execution speed. |
The example from above did work with the fix.
|
Any plans to implement the above fix? I've bumped into this bug twice in the last few weeks. A list comprehension is just so convenient and fits on one Evaluate row. OTOH, Collections.Remove Values From List does exactly what I was trying to do. |
I think the above fix isn't compatible with automatically importing modules. If someone comes up with a fix that doesn't break any existing tests, I'd be happy to review and merge a PR. |
np, I know what happens now |
Initial topic - https://groups.google.com/forum/#!topic/robotframework-users/OX5OvCmVwio
For some reasons expression
Evaluate [$PAGE_COMPONENTS[k]['name'] for k in $PAGE_COMPONENTS]
doesn't work for python 3.7, but works perfectly for python 2.7The text was updated successfully, but these errors were encountered: