本文整理汇总了Python中_ast.AST属性的典型用法代码示例。如果您正苦于以下问题:Python _ast.AST属性的具体用法?Python _ast.AST怎么用?Python _ast.AST使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类_ast
的用法示例。
在下文中一共展示了_ast.AST属性的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generic_visit
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import AST [as 别名]
def generic_visit(self, node):
for field, old_value in iter_fields(node):
old_value = getattr(node, field, None)
if isinstance(old_value, list):
new_values = []
for value in old_value:
if isinstance(value, AST):
value = self.visit(value)
if value is None:
continue
elif not isinstance(value, AST):
new_values.extend(value)
continue
new_values.append(value)
old_value[:] = new_values
elif isinstance(old_value, AST):
new_node = self.visit(old_value)
if new_node is None:
delattr(node, field)
else:
setattr(node, field, new_node)
return node
示例2: get_statement_startend2
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import AST [as 别名]
def get_statement_startend2(lineno, node):
import ast
# flatten all statements and except handlers into one lineno-list
# AST's line numbers start indexing at 1
l = []
for x in ast.walk(node):
if isinstance(x, _ast.stmt) or isinstance(x, _ast.ExceptHandler):
l.append(x.lineno - 1)
for name in "finalbody", "orelse":
val = getattr(x, name, None)
if val:
# treat the finally/orelse part as its own statement
l.append(val[0].lineno - 1 - 1)
l.sort()
insert_index = bisect_right(l, lineno)
start = l[insert_index - 1]
if insert_index >= len(l):
end = None
else:
end = l[insert_index]
return start, end
示例3: test_pythontypes
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import AST [as 别名]
def test_pythontypes(self):
# check all types defined in Python/
size = test.test_support.calcobjsize
vsize = test.test_support.calcvobjsize
check = self.check_sizeof
# _ast.AST
import _ast
check(_ast.AST(), size(''))
# imp.NullImporter
import imp
check(imp.NullImporter(self.file.name), size(''))
try:
raise TypeError
except TypeError:
tb = sys.exc_info()[2]
# traceback
if tb != None:
check(tb, size('2P2i'))
# symtable entry
# XXX
# sys.flags
check(sys.flags, vsize('') + self.P * len(sys.flags))
示例4: test_pythontypes
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import AST [as 别名]
def test_pythontypes(self):
# check all types defined in Python/
size = test.support.calcobjsize
vsize = test.support.calcvobjsize
check = self.check_sizeof
# _ast.AST
import _ast
check(_ast.AST(), size('P'))
try:
raise TypeError
except TypeError:
tb = sys.exc_info()[2]
# traceback
if tb is not None:
check(tb, size('2P2i'))
# symtable entry
# XXX
# sys.flags
check(sys.flags, vsize('') + self.P * len(sys.flags))
示例5: parse
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import AST [as 别名]
def parse(expr, filename="<unknown>", mode="exec"):
"""Parse an expression into an AST node."""
return compile(expr, filename, mode, PyCF_ONLY_AST)
示例6: generic_visit
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import AST [as 别名]
def generic_visit(self, node):
"""Called if no explicit visitor function exists for a node."""
for field, value in iter_fields(node):
if isinstance(value, list):
for item in value:
if isinstance(item, AST):
self.visit(item)
elif isinstance(value, AST):
self.visit(value)
示例7: compile_
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import AST [as 别名]
def compile_(source, filename=None, mode='exec', flags=
generators.compiler_flag, dont_inherit=0):
""" compile the given source to a raw code object,
and maintain an internal cache which allows later
retrieval of the source code for the code object
and any recursively created code objects.
"""
if _ast is not None and isinstance(source, _ast.AST):
# XXX should Source support having AST?
return cpy_compile(source, filename, mode, flags, dont_inherit)
_genframe = sys._getframe(1) # the caller
s = Source(source)
co = s.compile(filename, mode, flags, _genframe=_genframe)
return co
示例8: getstatementrange_ast
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import AST [as 别名]
def getstatementrange_ast(lineno, source, assertion=False, astnode=None):
if astnode is None:
content = str(source)
try:
astnode = compile(content, "source", "exec", 1024) # 1024 for AST
except ValueError:
start, end = getstatementrange_old(lineno, source, assertion)
return None, start, end
start, end = get_statement_startend2(lineno, astnode)
# we need to correct the end:
# - ast-parsing strips comments
# - there might be empty lines
# - we might have lesser indented code blocks at the end
if end is None:
end = len(source.lines)
if end > start + 1:
# make sure we don't span differently indented code blocks
# by using the BlockFinder helper used which inspect.getsource() uses itself
block_finder = inspect.BlockFinder()
# if we start with an indented line, put blockfinder to "started" mode
block_finder.started = source.lines[start][0].isspace()
it = ((x + "\n") for x in source.lines[start:end])
try:
for tok in tokenize.generate_tokens(lambda: next(it)):
block_finder.tokeneater(*tok)
except (inspect.EndOfBlock, IndentationError):
end = block_finder.last + start
except Exception:
pass
# the end might still point to a comment or empty line, correct it
while end:
line = source.lines[end - 1].lstrip()
if line.startswith("#") or not line:
end -= 1
else:
break
return astnode, start, end
示例9: test_pythontypes
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import AST [as 别名]
def test_pythontypes(self):
# check all types defined in Python/
size = test.test_support.calcobjsize
vsize = test.test_support.calcvobjsize
check = self.check_sizeof
# _ast.AST
import _ast
check(_ast.AST(), size(''))
# imp.NullImporter
import imp
f = open(test.test_support.TESTFN, 'wb')
try:
check(imp.NullImporter(f.name), size(''))
finally:
f.close()
test.test_support.unlink(test.test_support.TESTFN)
try:
raise TypeError
except TypeError:
tb = sys.exc_info()[2]
# traceback
if tb != None:
check(tb, size('2P2i'))
# symtable entry
# XXX
# sys.flags
check(sys.flags, vsize('') + self.P * len(sys.flags))
示例10: make_assign_unpack
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import AST [as 别名]
def make_assign_unpack(i, bytecode, unpack_num=-1):
if unpack_num < 1:
logger.error("Could not find the number of unpacked items. ")
return i, None
store_exprs = []
value_exprs = []
store_state, value_state = True, False
while i >= 0:
op, arg = bytecode[i][2], bytecode[i][3]
if store_state:
if op == UNPACK_SEQUENCE:
store_state = False
prev_op = bytecode[i - 1][2] if i > 0 else -1
if prev_op == BUILD_TUPLE:
value_state = True
else:
i, value_exprs = Statement.make_expr(i - 1, bytecode)
break
elif op in STORE_OPCODES:
i, store_stmt = Statement.make_expr(i, bytecode, context=_ast.Store())
store_exprs.insert(0, store_stmt)
elif value_state:
i, value_stmt = Statement.make_expr(i, bytecode)
value_exprs.insert(0, value_stmt)
i -= 1
store_exprs = _ast.Tuple(store_exprs, _ast.Store())
if not isinstance(value_exprs, _ast.AST):
value_exprs = _ast.Tuple(value_exprs, _ast.Load())
return i, _ast.Assign([store_exprs], value_exprs)
示例11: make_assign_opt_unpack
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import AST [as 别名]
def make_assign_opt_unpack(i, bytecode):
store_exprs = []
value_exprs = []
store_state, value_state = True, False
while i >= 0:
op, arg = bytecode[i][2], bytecode[i][3]
if store_state:
if op == ROT_TWO:
prev_op = bytecode[i - 1][2] if i > 0 else -1
if prev_op == ROT_THREE:
i -= 1
value_state = True
store_state = False
elif op in STORE_OPCODES:
i, store_stmt = Statement.make_expr(i, bytecode, context=_ast.Store())
store_exprs.insert(0, store_stmt)
elif value_state:
i, value_stmt = Statement.make_expr(i, bytecode)
value_exprs.insert(0, value_stmt)
i -= 1
store_exprs = _ast.Tuple(store_exprs, _ast.Store())
if not isinstance(value_exprs, _ast.AST):
value_exprs = _ast.Tuple(value_exprs, _ast.Load())
return i, _ast.Assign([store_exprs], value_exprs)
# Only one case here for:
# a = b = z.d.f = foo()
# => AST: _ast.Assign(targets=[Tuple(a, b, z.d.f)], value=foo())