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

symtable.Symbol.is_local() can be True for global symbols #84377

Closed
coproc mannequin opened this issue Apr 5, 2020 · 8 comments
Closed

symtable.Symbol.is_local() can be True for global symbols #84377

coproc mannequin opened this issue Apr 5, 2020 · 8 comments
Assignees
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@coproc
Copy link
Mannequin

coproc mannequin commented Apr 5, 2020

BPO 40196
Nosy @pablogsal, @miss-islington
PRs
  • bpo-40196: Fix a bug in the symtable when reporting inspecting global variables #19391
  • [3.8] bpo-40196: Fix a bug in the symtable when reporting inspecting global variables (GH-19391) #19394
  • [3.7] bpo-40196: Fix a bug in the symtable when reporting inspecting global variables (GH-19391) #19395
  • Files
  • global_and_local.py: console output (as in comment) shows that for global symbol 'e' also is_local() returns True
  • 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 = 'https://github.com/pablogsal'
    closed_at = <Date 2020-04-06.16:42:07.698>
    created_at = <Date 2020-04-05.13:30:26.178>
    labels = ['3.7', '3.8', 'type-bug', 'library', '3.9']
    title = 'symtable.Symbol.is_local() can be True for global symbols'
    updated_at = <Date 2020-04-06.16:42:07.698>
    user = 'https://bugs.python.org/coproc'

    bugs.python.org fields:

    activity = <Date 2020-04-06.16:42:07.698>
    actor = 'pablogsal'
    assignee = 'pablogsal'
    closed = True
    closed_date = <Date 2020-04-06.16:42:07.698>
    closer = 'pablogsal'
    components = ['Library (Lib)']
    creation = <Date 2020-04-05.13:30:26.178>
    creator = 'coproc'
    dependencies = []
    files = ['49037']
    hgrepos = []
    issue_num = 40196
    keywords = ['patch']
    message_count = 8.0
    messages = ['365820', '365821', '365845', '365853', '365855', '365868', '365870', '365871']
    nosy_count = 3.0
    nosy_names = ['pablogsal', 'miss-islington', 'coproc']
    pr_nums = ['19391', '19394', '19395']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue40196'
    versions = ['Python 3.5', 'Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9']

    @coproc
    Copy link
    Mannequin Author

    coproc mannequin commented Apr 5, 2020

    Consider this function:

    def f():
    	global e
    	e = 1

    When inspecting symbols with symtable, symbol 'e' will be global and local, whereas is_local() should return False. See the attached file for reproducing. It will output to stdout:

    symbol 'e' in function scope: is_global() = True, is_local() = True
    global scope: e = 1

    @coproc coproc mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir labels Apr 5, 2020
    @coproc
    Copy link
    Mannequin Author

    coproc mannequin commented Apr 5, 2020

    see https://stackoverflow.com/a/61040435/1725562 for a proposed fix

    @coproc coproc mannequin added type-bug An unexpected behavior, bug, or error labels Apr 5, 2020
    @pablogsal
    Copy link
    Member

    That fix is not correct. For instance consider:

    >>> code2 = """\
    ... def foo():
    ...    x = 42
    ...    def bar():
    ...       return -1
    ... """
    >>> top.get_children()[0]
    <Function SymbolTable for foo in ?>
    >>> top = symtable.symtable(code2, "?", "exec")
    >>> top.get_children()[0].lookup('x')._Symbol__scope == symtable.LOCAL
    True

    but if we return x from bar:

    >>> code = """\
    ... def foo():
    ...    x = 42
    ...    def bar():
    ...       return x
    ... """
    >>> import symtable
    >>> top = symtable.symtable(code, "?", "exec")
    >>> top.get_children()[0].lookup('x')._Symbol__scope == symtable.LOCAL
    False

    @coproc
    Copy link
    Mannequin Author

    coproc mannequin commented Apr 6, 2020

    In symtable.Function.get_locals() symbols with scopes in (LOCAL, CELL) are selected. Also

    >>> code = """\
    ... def foo():
    ...    x = 42
    ...    def bar():
    ...       return x
    ... """
    >>> import symtable
    >>> top = symtable.symtable(code, "?", "exec")
    >>> top.get_children()[0].lookup('x')._Symbol__scope == symtable.CELL
    True

    So I guess this would be the correct fix then:

    def is_local(self):
        return self.__scope in (LOCAL, CELL)

    @pablogsal
    Copy link
    Member

    In symtable.Function.get_locals() symbols with scopes in (LOCAL, CELL) are selected.

    Thanks for pointing that out. I will simplify PR 19391.

    @pablogsal
    Copy link
    Member

    New changeset 799d7d6 by Pablo Galindo in branch 'master':
    bpo-40196: Fix a bug in the symtable when reporting inspecting global variables (GH-19391)
    799d7d6

    @miss-islington
    Copy link
    Contributor

    New changeset 717f166 by Miss Islington (bot) in branch '3.7':
    bpo-40196: Fix a bug in the symtable when reporting inspecting global variables (GH-19391)
    717f166

    @pablogsal
    Copy link
    Member

    New changeset 8bd84e7 by Miss Islington (bot) in branch '3.8':
    bpo-40196: Fix a bug in the symtable when reporting inspecting global variables (GH-19391) (GH-19394)
    8bd84e7

    @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.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants