Skip to content
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

Variables in locals scope fails to be printed. #87673

Closed
xxm mannequin opened this issue Mar 16, 2021 · 2 comments
Closed

Variables in locals scope fails to be printed. #87673

xxm mannequin opened this issue Mar 16, 2021 · 2 comments
Labels
3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@xxm
Copy link
Mannequin

xxm mannequin commented Mar 16, 2021

BPO 43507
Nosy @mdickinson
Superseder
  • bpo-24800: exec docs should note that the no argument form in a local scope is really the two argument form
  • 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 = <Date 2021-03-16.10:41:20.143>
    created_at = <Date 2021-03-16.03:08:01.278>
    labels = ['interpreter-core', 'type-bug', '3.9']
    title = 'Variables in locals scope fails to be printed.'
    updated_at = <Date 2021-03-16.10:41:20.142>
    user = 'https://bugs.python.org/xxm'

    bugs.python.org fields:

    activity = <Date 2021-03-16.10:41:20.142>
    actor = 'mark.dickinson'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-03-16.10:41:20.143>
    closer = 'mark.dickinson'
    components = ['Interpreter Core']
    creation = <Date 2021-03-16.03:08:01.278>
    creator = 'xxm'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 43507
    keywords = []
    message_count = 2.0
    messages = ['388802', '388825']
    nosy_count = 2.0
    nosy_names = ['mark.dickinson', 'xxm']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '24800'
    type = 'behavior'
    url = 'https://bugs.python.org/issue43507'
    versions = ['Python 3.9']

    @xxm
    Copy link
    Mannequin Author

    xxm mannequin commented Mar 16, 2021

    The following code 1 calls function 'compile' and 'exec' and execute a statement "s=1". Then we print the value of 's'. This code can perform well on Python 3.9.2 and output the expected result. However, we pack the whole code into a function (code 2). The execution fails.

    code 1:
    ===================

    mstr = "s=1"
    exec(compile(mstr,'','exec'))
    print(s)

    ===================
    output: 1

    code2:
    ===================

    def foo():
         mstr = "s=1"
         exec(compile(mstr,'','exec'))
         print(s)
    foo()

    ===================

    output: 
    Traceback (most recent call last):
        File "/home/xxm/Desktop/apifuzz/doc/genDoc.py", line 37, in <module>
                foo()
        File "/home/xxm/Desktop/apifuzz/doc/genDoc.py", line 35, in foo
                print(s)
    NameError: name 's' is not defined

    By the way, we print locals(). 's' exists in the local scope. It should not fail.

    >print(locals())
    {'mstr': 's=1', 's': 1}

    @xxm xxm mannequin added 3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Mar 16, 2021
    @mdickinson
    Copy link
    Member

    I think this is essentially a duplicate of bpo-24800. (Short version, the behaviour is by design, and documented, but there may be scope for clarifying or improving the documentation.)

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant