-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Docs does not mention that eval() allows a code object as an argument #78612
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
Comments
See https://docs.python.org/3.6/library/functions.html#eval Python 3.6.2 (default, Jul 29 2017, 00:00:00)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def fn(): print(3); return 4
...
>>> eval(fn.__code__)
3
4 Compare with https://docs.python.org/3.6/library/functions.html#exec Search for eval in title of open issues bring up related #66255 (The doc say all globals are copied on eval(), but only __builtins__ is copied) |
Thank you for your report. Quoting from https://docs.python.org/3.6/library/functions.html#eval
So, your example is already documented as a legal way of using the eval() function. I don't see anything wrong here. |
Summary: There's my problem, and others. I'm willing to provide a patch, if supported. There's a gotcha here. I fell into it. The docs for eval state eval(expression, globals=None, locals=None)
The arguments are a string and optional globals and locals. === But I knew that exec would take a code object, so I had doubt, and did my little experiment. I'd prefer something more like exec, which says There are other problems, such as not agreeing with the help(eval) etc messages (and the still open bpo-22057, bpo-25810): eval(source, globals=None, locals=None, /)
Evaluate the given source in the context of globals and locals.
The source may be a string representing a Python expression
or a code object as returned by compile().
The globals must be a dictionary and locals can be any mapping,
defaulting to the current globals and locals.
If only globals is given, locals defaults to it.
===
exec(source, globals=None, locals=None, /)
Execute the given source in the context of globals and locals.
The source may be a string representing one or more Python statements
or a code object as returned by compile().
The globals must be a dictionary and locals can be any mapping,
defaulting to the current globals and locals.
If only globals is given, locals defaults to it. === Finally, I'm willing to provide a patch, if supported. (I've not contributed to Python before.) [1] I'd just read the docs for exec, which is up-front about 'string or code'. |
Ok, I think it would be a good idea to mention that the function accepts a code object in the first sentence. I'd be happy to review such PR. The eval() documentation is located at https://github.com/python/cpython/blob/master/Doc/library/functions.rst See https://devguide.python.org/ for more details about the contribution process. Note that you need to sign the CLA in order to contribute to Python: https://www.python.org/psf/contrib/contrib-form/ #66255 and #69996 are out of scope for this issue, so let's not discuss them here. |
OK. I'll do as you say. I've just signed the CLA. |
Hi Jonathan, Did you have any additional questions about opening a pull request for these changes? Thanks! |
This graceful reminder is most welcome. At present, it's not help I'm short of, but time. I've given myself a reminder, to spend time on this before the end of this month (January 2019). Once again, thank you for this reminder. This is something I want to do. It would be my first Python contribution. |
Hi Jonathan, |
Also mention that the *expression* parameter can be a string.
Also mention that the 'expression' parameter can be a string.
Also mention that the 'expression' parameter can be a string. (cherry picked from commit a71e32c) Co-authored-by: Erlend E. Aasland <[email protected]>
Also mention that the 'expression' parameter can be a string. (cherry picked from commit a71e32c) Co-authored-by: Erlend E. Aasland <[email protected]>
Also mention that the 'expression' parameter can be a string.
Also mention that the 'expression' parameter can be a string.
Also mention that the 'expression' parameter can be a string.
Also mention that the 'expression' parameter can be a string. (cherry picked from commit a71e32c) Co-authored-by: Erlend E. Aasland <[email protected]>
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: