1

Consider this function:

def content(path):
  global file # not useful but valid
  with open(path) as file:
    return file.read()

When generating a symbol table (using module symtable) and inspecting the symbol file in the scope of the function content, it is global and local at the same time. After calling this function the global name file is bound to the file object. So I wonder why the symbol file in the function scope is also considered as a local symbol?

Here is code to reproduce the behaviour (put it in a file e.g. called global_and_local.py):

import symtable

def content(path):
  global file
  with open(path) as file:
    return file.read()

symtable_root = symtable.symtable(content(__file__), __file__, "exec")
symtable_function = symtable_root.get_children()[0]
symbol_file = symtable_function.lookup('file')
print("symbol 'file' in function scope: is_global() =", symbol_file.is_global())
print("symbol 'file' in function scope: is_local() =", symbol_file.is_local())
print("global scope: file =", file)

The following output is generated:

symbol 'file' in function scope: is_global() = True
symbol 'file' in function scope: is_local() = True
global scope: file = <_io.TextIOWrapper name='global_and_local.py' ...>

1 Answer 1

3

For some reason, symtable defines is_local as a check for whether any binding operations for a symbol occur in the scope (or annotations, which are lumped together with annotated assignments at this stage):

def is_local(self):
    return bool(self.__flags & DEF_BOUND)

rather than a check for whether the symbol is actually local, which would look like

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

I'm not sure why. This might be a bug. I don't think modules like this get much use - it took over a year before anyone noticed that adding the // operator broke the old parser module, so I could easily see this going unnoticed.

5
  • @coproc: Assignment, function definitions, imports, use as a for loop target, etc. Anything that would rebind a name. Apr 5, 2020 at 10:38
  • I have committed a bug report (see bugs.python.org/issue40196 ) and referred to your answer
    – coproc
    Apr 5, 2020 at 13:42
  • symtable.Symbol.is_local() is fixed now: github.com/python/cpython/pull/19391
    – coproc
    Apr 6, 2020 at 14:48
  • Ah, whoops. Python/symtable.c handles CELL a bit differently from other scopes, so I didn't realize it was a separate option from LOCAL. I'm pretty sure the bool is redundant, though (although it's not redundant in some of the other methods that perform an &). Apr 6, 2020 at 20:05
  • I'm glad somebody else caught that, so we didn't just go from one bug to another. Apr 6, 2020 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.