Feature or enhancement
symtable.symtable() currently only accepts str or bytes source. I propose to also accept an AST object, with the same semantics as the builtin compile(): the compile_type argument selects the expected root node, and future statements found in the tree are honored.
>>> import ast, symtable
>>> tree = ast.parse("def f(): pass")
>>> symtable.symtable(tree, "<string>", "exec")
<SymbolTable for top in <string>>
The restriction is an artifact of the module wrapper: the C implementation builds the symbol table from an AST anyway, and compile() has accepted AST input since Python 2.6. But there is no way to get scope information for a tree you already have — a transformed or synthesized AST has no source, and the only workaround is ast.unparse() plus re-parsing.
Linked PRs
Feature or enhancement
symtable.symtable()currently only acceptsstrorbytessource. I propose to also accept an AST object, with the same semantics as the builtincompile(): the compile_type argument selects the expected root node, and future statements found in the tree are honored.The restriction is an artifact of the module wrapper: the C implementation builds the symbol table from an AST anyway, and
compile()has accepted AST input since Python 2.6. But there is no way to get scope information for a tree you already have — a transformed or synthesized AST has no source, and the only workaround isast.unparse()plus re-parsing.Linked PRs