本文整理汇总了Python中symtable.symtable函数的典型用法代码示例。如果您正苦于以下问题:Python symtable函数的具体用法?Python symtable怎么用?Python symtable使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了symtable函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: checkfilename
def checkfilename(brokencode):
try:
symtable.symtable(brokencode, "spam", "exec")
except SyntaxError as e:
self.assertEqual(e.filename, "spam")
else:
self.fail("no SyntaxError for %r" % (brokencode,))
示例2: test_annotated
def test_annotated(self):
st1 = symtable.symtable('def f():\n x: int\n', 'test', 'exec')
st2 = st1.get_children()[0]
self.assertTrue(st2.lookup('x').is_local())
self.assertTrue(st2.lookup('x').is_annotated())
self.assertFalse(st2.lookup('x').is_global())
st3 = symtable.symtable('def f():\n x = 1\n', 'test', 'exec')
st4 = st3.get_children()[0]
self.assertTrue(st4.lookup('x').is_local())
self.assertFalse(st4.lookup('x').is_annotated())
示例3: __tests
def __tests():
import symtable
class ClassFinder(ast.NodeVisitor):
def __init__(self, name):
ast.NodeVisitor.__init__(self)
self.name = name
self.classes = []
def visit_ClassDef(self, node):
if node.name == self.name:
self.classes.append(node)
with open('tests/game2.py') as source_file:
code = source_file.read()
ast_node = ast.parse(code)
symbol_table = symtable.symtable(code, 'game2.py', 'exec')
class_finder = ClassFinder('Game')
class_finder.visit(ast_node)
subclass = class_finder.classes[0]
subclass_symbol_table = symbol_table.lookup('Game').get_namespace()
superclasses, subclass = SuperClassExtractor.extract_into_ideal_cohesion(subclass, subclass_symbol_table)
for superclass_node in superclasses:
print to_source(superclass_node)
# print
# print ast.dump(superclass_node)
print
print '#' * 200
print
print to_source(subclass)
示例4: __init__
def __init__(self, code, ns=None):
global __default_namespace__
if ns is None:
ns = __default_namespace__
var = [v for v in symtable.symtable(code, '<string>', 'exec').get_identifiers() if v in ns]
super(PythonCode, self).__init__(var=var, namespace=ns)
self.code = compile(code, '<string>', 'exec')
示例5: parse_module
def parse_module(module_name):
filename = module_name + ".py"
with open(os.path.join(SOLUTIONS_DIR, filename), 'r') as f:
source = f.read()
srclines = source.splitlines()
module = ast.parse(source, filename)
table = symtable.symtable(source, filename, "exec")
stmts = list(get_stmts(module))
last_fun = stmts[0][0]
lines = {name:"\n".join(srclines[s:e]).strip() for (name,(s,e)) in stmts}
imports = dict(get_imports(module))
def parse_dependencies(name):
import builtins
for tab in get_child_tables(table.lookup(name).get_namespace()):
for g in tab.get_globals():
if g in dir(builtins):
continue
if table.lookup(g).is_imported():
imported = imports[g]
if imported[0] != "leetcode":
yield imported
else:
yield (module_name, g)
return last_fun, lines, {name:tuple(parse_dependencies(name)) for name in lines}
示例6: _inspect
def _inspect(self,string):
symtab = symtable.symtable(string,'rule',self.mode)
symbols = symtab.get_symbols()
defs = [sym.get_name() for sym in symbols if sym.is_assigned()]
refs = [sym.get_name() for sym in symbols if sym.is_referenced()]
self.all_symbols = frozenset(symtab.get_identifiers())
self.sym_assigned = defset = frozenset(defs)
self.sym_internal = frozenset(defset.intersection(refs))
示例7: __find_class_symbol_tables
def __find_class_symbol_tables(self):
class_names = self.__get_class_names()
source_code = self.get_source_code()
module_symbol_table = symtable.symtable(source_code, self.file_path, 'exec')
symbol_tables = []
for name in class_names:
class_symbol_table = module_symbol_table.lookup(name).get_namespace()
symbol_tables.append(class_symbol_table)
return symbol_tables
示例8: get_symtable
def get_symtable(path):
import symtable
try:
st = symtables[path]
except KeyError:
with open(path, 'r') as f:
st = symtable.symtable(f.read(), path, 'exec')
symtables[path] = st
return st
示例9: __init__
def __init__(self, source_no_encoding, pubapi):
# Our public API (__all__)
self.pubapi = pubapi
# Names of imported modules
self.modnames = []
self.symtab = symtable.symtable(source_no_encoding, "-", "exec")
cst = parser.suite(source_no_encoding)
elements = parser.ast2tuple(cst, line_info=1)
self.names = {}
self.walk(elements, [self.symtab])
示例10: __init__
def __init__(self, source, path='<string>'):
self.source = source
self.path = path
self.ast = ast.parse(source, path)
self.symtable = symtable.symtable(source, path, 'exec')
self.tokens = tokenize_string(source)
cw = ChainWalker(self.ast, self.symtable)
self.nodes = cw.nodes
TokenAssociator(self.nodes, self.tokens)
self.ast_map = {node.ast_node: node for node in self.nodes}
示例11: format_table
def format_table(code_string):
s_table = symtable(code_string, "string", "exec")
table = []
for child in s_table.get_children():
row = {
"name": child.get_name(),
"scope": "global",
"children": [subchild.get_name() for subchild in child.get_children()],
}
table.append(row)
return s_table, table
示例12: getDefinitions
def getDefinitions(self, doc, identifier):
if doc.get_language().get_name() != "Python":
return
doc_location = doc.get_location()
with open(doc_location.get_path()) as f:
table = symtable.symtable(
f.read(),
doc_location.get_basename(),
"exec",
)
for line in self.generateDefLines(table, identifier):
yield doc_location, line, "", doc_location.get_path()
示例13: load_file
def load_file(filename):
filename = os.path.abspath(filename)
with open(filename) as fp:
syminfo = symtable.symtable(fp.read() + '\n', filename, 'exec')
if(os.path.splitext(filename)[1] == '.py'):
try:
py_compile.compile(filename, filename+'c', doraise=True)
except py_compile.PyCompileError, msg:
print str(msg)
print 'Couldn\'t compile %s, stopping.' % filename
os._exit(0)
filename += 'c'
示例14: test_annotated
def test_annotated(self):
st1 = symtable.symtable('def f():\n x: int\n', 'test', 'exec')
st2 = st1.get_children()[0]
self.assertTrue(st2.lookup('x').is_local())
self.assertTrue(st2.lookup('x').is_annotated())
self.assertFalse(st2.lookup('x').is_global())
st3 = symtable.symtable('def f():\n x = 1\n', 'test', 'exec')
st4 = st3.get_children()[0]
self.assertTrue(st4.lookup('x').is_local())
self.assertFalse(st4.lookup('x').is_annotated())
# Test that annotations in the global scope are valid after the
# variable is declared as nonlocal.
st5 = symtable.symtable('global x\nx: int', 'test', 'exec')
self.assertTrue(st5.lookup("x").is_global())
# Test that annotations for nonlocals are valid after the
# variable is declared as nonlocal.
st6 = symtable.symtable('def g():\n'
' x = 2\n'
' def f():\n'
' nonlocal x\n'
' x: int',
'test', 'exec')
示例15: test_filename_correct
def test_filename_correct(self):
### Bug tickler: SyntaxError file name correct whether error raised
### while parsing or building symbol table.
def checkfilename(brokencode):
try:
symtable.symtable(brokencode, "spam", "exec")
except SyntaxError as e:
self.assertEqual(e.filename, "spam")
else:
self.fail("no SyntaxError for %r" % (brokencode,))
checkfilename("def f(x): foo)(") # parse-time
checkfilename("def f(x): global x") # symtable-build-time
symtable.symtable("pass", b"spam", "exec")
with self.assertRaises(TypeError):
symtable.symtable("pass", bytearray(b"spam"), "exec")
symtable.symtable("pass", memoryview(b"spam"), "exec")
with self.assertRaises(TypeError):
symtable.symtable("pass", list(b"spam"), "exec")