-
Notifications
You must be signed in to change notification settings - Fork 419
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
Clean up exec
and globals
#1761
base: master
Are you sure you want to change the base?
Conversation
Especially in the latter case of the `Verify` plugin, this exposed that some methods were missing parameters.
As of PEP558, `locals()` is not populated by `exec()`. This change means that this call is broken on Python 3.13.
result = {} | ||
exec(s, globals(), result) | ||
return result["fn"] | ||
except SyntaxError: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note, since the string above only defines a function, I can't see how this could fail in a way other than a syntax error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PS, this file could be written in a way that doesn't use exec
with some lambda
s, but as it seemed that this was done for speed, I'm not sure if I should change that without having any benchmarks available. So I wonder if there are any benchmarks for this bit of code?
olddad, | ||
yngmom, | ||
yngdad, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two examples of undefined variables that happened because globals()
modifications set them, instead of passing them as parameters.
result = {} | ||
exec(s, globals(), result) | ||
return result["fn"] | ||
except SyntaxError: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong format strings produce TypeErrors:
>>> '' % 'a'
Traceback (most recent call last):
File "<python-input-0>", line 1, in <module>
'' % 'a'
~~~^~~~~
TypeError: not all arguments converted during string formatting
>>> '%x' % 'a'
Traceback (most recent call last):
File "<python-input-1>", line 1, in <module>
'%x' % 'a'
~~~~~^~~~~
TypeError: %x format: an integer is required, not str
>>> '%s %s' % 'a'
Traceback (most recent call last):
File "<python-input-2>", line 1, in <module>
'%s %s' % 'a'
~~~~~~~~^~~~~
TypeError: not enough arguments for format string
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Format strings are only evaluated on the line defining s
earlier, or within fn
itself, but not at the outer level at which the exec
is run.
All the examples you've given pass when wrapped in def fn():
.
Note, fn
is not called here, but cached for use in the callers of this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright then, sorry for the noise.
Applied all changes to the individual files in my Fedora 41 install. Seems to work! |
Using
exec
andglobals
can sometimes obscure what code is doing.The last commit also fixes bug 13347.