本文整理汇总了Python中symtable.symtable方法的典型用法代码示例。如果您正苦于以下问题:Python symtable.symtable方法的具体用法?Python symtable.symtable怎么用?Python symtable.symtable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类symtable
的用法示例。
在下文中一共展示了symtable.symtable方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import symtable [as 别名]
# 或者: from symtable import symtable [as 别名]
def __init__(self, code: str, key: bytes, ns_manager: BuiltInManager):
self.symbol_table: symtable = symtable(code, '<string>', 'exec')
self.code: str = code
self.input_vars: List[SymbolWrapper] = CodeObject._find_input_variables(self.symbol_table, ns_manager
)
self.output_vars: FrozenSet[SymbolWrapper] = CodeObject._find_output_variables(self.symbol_table
)
h = blake2b(digest_size=10, key=key)
if len(self.output_vars) > 0:
display_id_prefix = "+".join(map(str, self.output_vars))
h.update(display_id_prefix.encode('utf-8'))
self.display_id = f"{display_id_prefix}-{h.hexdigest()}"
else:
h.update(self.code.encode('utf-8'))
self.display_id = f"{h.hexdigest()}"
示例2: test_filename_correct
# 需要导入模块: import symtable [as 别名]
# 或者: from symtable import symtable [as 别名]
def test_filename_correct(self):
### Bug tickler: SyntaxError file name correct whether error raised
### while parsing or building symbol table.
def checkfilename(brokencode, offset):
try:
symtable.symtable(brokencode, "spam", "exec")
except SyntaxError as e:
self.assertEqual(e.filename, "spam")
self.assertEqual(e.lineno, 1)
self.assertEqual(e.offset, offset)
else:
self.fail("no SyntaxError for %r" % (brokencode,))
checkfilename("def f(x): foo)(", 14) # parse-time
checkfilename("def f(x): global x", 10) # 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")
示例3: onelinerize
# 需要导入模块: import symtable [as 别名]
# 或者: from symtable import symtable [as 别名]
def onelinerize(original):
# original :: string
# :: string
t = ast.parse(original)
table = symtable.symtable(original, '<string>', 'exec')
original = original.strip()
# If there's only one line anyways, be lazy
if len(original.splitlines()) == 1 and \
len(t.body) == 1 and \
type(t.body[0]) in (ast.Delete, ast.Assign, ast.AugAssign, ast.Print,
ast.Raise, ast.Assert, ast.Import, ast.ImportFrom,
ast.Exec, ast.Global, ast.Expr, ast.Pass):
return original
return get_init_code(t, table)
示例4: get_inferred_type
# 需要导入模块: import symtable [as 别名]
# 或者: from symtable import symtable [as 别名]
def get_inferred_type(self, name):
# type: (str) -> Any
# Given a symbol name, check whether a type
# has been inferred.
# The stdlib symtable will already fall back to
# global scope if necessary.
symbol = self._local_table.lookup(name)
if symbol.is_global():
try:
global_symbol = self._global_table.lookup(name)
except KeyError:
# It's not an error if a symbol.is_global()
# but is not in our "_global_table", because
# we're not considering the builtin scope.
# In this case we just say that there is no
# type we've inferred.
return None
return getattr(global_symbol, 'inferred_type', None)
return getattr(symbol, 'inferred_type', None)
示例5: test_filename_correct
# 需要导入模块: import symtable [as 别名]
# 或者: from symtable import symtable [as 别名]
def test_filename_correct(self):
### Bug tickler: SyntaxError file name correct whether error raised
### while parsing or building symbol table.
def checkfilename(brokencode, offset):
try:
symtable.symtable(brokencode, "spam", "exec")
except SyntaxError as e:
self.assertEqual(e.filename, "spam")
self.assertEqual(e.lineno, 1)
self.assertEqual(e.offset, offset)
else:
self.fail("no SyntaxError for %r" % (brokencode,))
checkfilename("def f(x): foo)(", 14) # parse-time
checkfilename("def f(x): global x", 10) # symtable-build-time
symtable.symtable("pass", b"spam", "exec")
with self.assertWarns(DeprecationWarning), \
self.assertRaises(TypeError):
symtable.symtable("pass", bytearray(b"spam"), "exec")
with self.assertWarns(DeprecationWarning):
symtable.symtable("pass", memoryview(b"spam"), "exec")
with self.assertRaises(TypeError):
symtable.symtable("pass", list(b"spam"), "exec")
示例6: test_filename_correct
# 需要导入模块: import symtable [as 别名]
# 或者: from symtable import symtable [as 别名]
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
示例7: test_eval
# 需要导入模块: import symtable [as 别名]
# 或者: from symtable import symtable [as 别名]
def test_eval(self):
symbols = symtable.symtable("42", "?", "eval")
示例8: test_single
# 需要导入模块: import symtable [as 别名]
# 或者: from symtable import symtable [as 别名]
def test_single(self):
symbols = symtable.symtable("42", "?", "single")
示例9: test_exec
# 需要导入模块: import symtable [as 别名]
# 或者: from symtable import symtable [as 别名]
def test_exec(self):
symbols = symtable.symtable("def f(x): return x", "?", "exec")
示例10: format_table
# 需要导入模块: import symtable [as 别名]
# 或者: from symtable import symtable [as 别名]
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