FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-40926: Improve & fix command line usage of symtable by isidentical · Pull Request #20757 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
30 changes: 23 additions & 7 deletions Lib/symtable.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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)
Comment thread
isidentical marked this conversation as resolved.

if __name__ == "__main__":
main()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix command line interface of :mod:`symtable`.

Back | FazBrowse Home | New Git URL