Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
bpo-40926: Improve & fix command line usage of symtable
  • Loading branch information
isidentical committed Jun 9, 2020
commit 39502fb223fb5a877f65fd7eb3ecafecc28ddf40
30 changes: 23 additions & 7 deletions Lib/symtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,27 @@ def get_namespace(self):
raise ValueError("name is bound to multiple namespaces")
return self.__namespaces[0]

if __name__ == "__main__":
import os, sys
with open(sys.argv[0]) as f:
src = f.read()
mod = symtable(src, os.path.split(sys.argv[0])[1], "exec")
def main():
from argparse import ArgumentParser, FileType
parser = ArgumentParser(prog='python -m symtable')
parser.add_argument('infile', type=FileType(mode='rb'), nargs='?',
default='-',
help='the file to show symbol table; defaults to stdin')
parser.add_argument('-m', '--mode', default='exec',
choices=('exec', 'single', 'eval'),
help='specify what kind of code must be parsed')
args = parser.parse_args()

with args.infile as infile:
source = infile.read()

mod = symtable(source, args.infile.name, args.mode)
available_properties = [prop for prop in dir(Symbol) if prop.startswith("is_")]

for ident in mod.get_identifiers():
info = mod.lookup(ident)
print(info, info.is_local(), info.is_namespace())
symbol = mod.lookup(ident)
properties = {prop.removeprefix("is_") for prop in available_properties if getattr(symbol, prop)()}
print(symbol, "==>", properties)

if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix command line interface of :mod:`symtable`.