Skip to content

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

Closed
jfine2358 mannequin opened this issue Aug 18, 2018 · 8 comments · Fixed by #115212
Closed

Docs does not mention that eval() allows a code object as an argument #78612

jfine2358 mannequin opened this issue Aug 18, 2018 · 8 comments · Fixed by #115212
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@jfine2358
Copy link
Mannequin

jfine2358 mannequin commented Aug 18, 2018

BPO 34431
Nosy @berkerpeksag, @csabella, @jfine2358, @furkanonder
PRs
  • gh-78612: Docs does not eval allows code object as argument #20000
  • 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:

    assignee = None
    closed_at = None
    created_at = <Date 2018-08-18.16:03:19.804>
    labels = ['3.8', 'type-bug', '3.7', 'docs']
    title = 'Docs does not eval allows code object as argument'
    updated_at = <Date 2020-05-17.20:03:25.956>
    user = 'https://github.com/jfine2358'

    bugs.python.org fields:

    activity = <Date 2020-05-17.20:03:25.956>
    actor = 'furkanonder'
    assignee = 'docs@python'
    closed = False
    closed_date = None
    closer = None
    components = ['Documentation']
    creation = <Date 2018-08-18.16:03:19.804>
    creator = 'jfine2358'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 34431
    keywords = ['patch']
    message_count = 8.0
    messages = ['323716', '323717', '323720', '323722', '323723', '333082', '333148', '369152']
    nosy_count = 5.0
    nosy_names = ['docs@python', 'berker.peksag', 'cheryl.sabella', 'jfine2358', 'furkanonder']
    pr_nums = ['20000']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue34431'
    versions = ['Python 2.7', 'Python 3.6', 'Python 3.7', 'Python 3.8']

    Linked PRs

    @jfine2358
    Copy link
    Mannequin Author

    jfine2358 mannequin commented Aug 18, 2018

    See https://docs.python.org/3.6/library/functions.html#eval
    This says the following won't happen. But it does.

    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)
    #69996 (Python 3 documentation for eval is incorrect)

    @jfine2358 jfine2358 mannequin added 3.8 (EOL) end of life 3.7 (EOL) end of life labels Aug 18, 2018
    @jfine2358 jfine2358 mannequin assigned docspython Aug 18, 2018
    @jfine2358 jfine2358 mannequin added the docs Documentation in the Doc dir label Aug 18, 2018
    @berkerpeksag
    Copy link
    Member

    Thank you for your report.

    Quoting from https://docs.python.org/3.6/library/functions.html#eval

    This function can also be used to execute arbitrary code objects
    (such as those created by compile()). In this case pass a code
    object instead of a string.
    

    So, your example is already documented as a legal way of using the eval() function.

    I don't see anything wrong here.

    @berkerpeksag berkerpeksag added invalid type-bug An unexpected behavior, bug, or error labels Aug 18, 2018
    @jfine2358
    Copy link
    Mannequin Author

    jfine2358 mannequin commented Aug 18, 2018

    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.

    ===
    I read this and concluded, fairly I think, that I'm not allowed to pass in a code object [1]. So I didn't read any further. I'd already got the answer to my question.

    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
    ===
    This function supports dynamic execution of Python code. object must be either a string or a code object.
    ===

    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'.

    @berkerpeksag
    Copy link
    Member

    berkerpeksag commented Aug 18, 2018

    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.

    @jfine2358
    Copy link
    Mannequin Author

    jfine2358 mannequin commented Aug 18, 2018

    OK. I'll do as you say. I've just signed the CLA.

    @csabella
    Copy link
    Contributor

    csabella commented Jan 5, 2019

    Hi Jonathan,

    Did you have any additional questions about opening a pull request for these changes? Thanks!

    @jfine2358
    Copy link
    Mannequin Author

    jfine2358 mannequin commented Jan 7, 2019

    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.

    @furkanonder
    Copy link
    Mannequin

    furkanonder mannequin commented May 17, 2020

    Hi Jonathan,
    Are you still planning to work on the patch?

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @erlend-aasland erlend-aasland removed 3.8 (EOL) end of life 3.7 (EOL) end of life labels Feb 9, 2024
    erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue Feb 9, 2024
    Also mention that the *expression* parameter can be a string.
    @erlend-aasland erlend-aasland changed the title Docs does not eval allows code object as argument Docs does not mention that eval() allows a code object as an argument Feb 9, 2024
    @erlend-aasland erlend-aasland linked a pull request Feb 9, 2024 that will close this issue
    erlend-aasland added a commit that referenced this issue Feb 28, 2024
    Also mention that the 'expression' parameter can be a string.
    miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 28, 2024
    Also mention that the 'expression' parameter can be a string.
    (cherry picked from commit a71e32c)
    
    Co-authored-by: Erlend E. Aasland <[email protected]>
    miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 28, 2024
    Also mention that the 'expression' parameter can be a string.
    (cherry picked from commit a71e32c)
    
    Co-authored-by: Erlend E. Aasland <[email protected]>
    woodruffw pushed a commit to woodruffw-forks/cpython that referenced this issue Mar 4, 2024
    Also mention that the 'expression' parameter can be a string.
    adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 25, 2024
    Also mention that the 'expression' parameter can be a string.
    diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
    Also mention that the 'expression' parameter can be a string.
    erlend-aasland added a commit that referenced this issue May 7, 2024
    Also mention that the 'expression' parameter can be a string.
    (cherry picked from commit a71e32c)
    
    Co-authored-by: Erlend E. Aasland <[email protected]>
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    Successfully merging a pull request may close this issue.

    3 participants