本文整理匯總了Python中ast.fix_missing_locations方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.fix_missing_locations方法的具體用法?Python ast.fix_missing_locations怎麽用?Python ast.fix_missing_locations使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ast
的用法示例。
在下文中一共展示了ast.fix_missing_locations方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: visit_Expr
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import fix_missing_locations [as 別名]
def visit_Expr(self, node):
newnode = ast.copy_location(ast.Expr(
value = ast.Call(
func = ast.Attribute(
value = ast.Name(id='__swirlypy_recorder__',
ctx=ast.Load()),
attr="record",
ctx=ast.Load()),
args=[node.value],
keywords=[],
starargs=None,
kwargs=None
)
),
node)
ast.fix_missing_locations(newnode)
return newnode
示例2: preprocess_method_body
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import fix_missing_locations [as 別名]
def preprocess_method_body(source_code_obj):
src = inspect.getsource(source_code_obj)
ast_tree = ast.parse(src)
visitor = RewriteDicts()
ast_tree = visitor.visit(ast_tree)
ast.fix_missing_locations(ast_tree)
updated_code = compile(ast_tree, filename='<ast>', mode='exec')
bc = Bytecode.from_code(updated_code)
dlist = visitor.updated_dicts
RewriteDicts.updated_dicts = []
RewriteDicts.last_store_name = None
block_code = get_code_block(bc)
return block_code.arg, dlist
示例3: visit_BinOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import fix_missing_locations [as 別名]
def visit_BinOp(self, node):
'If node is a constant expression, replace it with its evaluated value'
if node in self.constexpr:
# evaluation
fake_node = ast.Expression(ast.BinOp(node, ast.Mod(),
ast.Num(self.mod)))
ast.fix_missing_locations(fake_node)
code = compile(fake_node, '<constant folding>', 'eval')
obj_env = globals().copy()
exec code in obj_env
value = eval(code, obj_env)
new_node = ast.Num(value)
return new_node
else:
return self.generic_visit(node)
示例4: visit_UnaryOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import fix_missing_locations [as 別名]
def visit_UnaryOp(self, node):
'Same idea as visit_BinOp'
if node in self.constexpr:
# evaluation
fake_node = ast.Expression(ast.BinOp(node, ast.Mod(),
ast.Num(self.mod)))
ast.fix_missing_locations(fake_node)
code = compile(fake_node, '<constant folding>', 'eval')
obj_env = globals().copy()
exec code in obj_env
value = eval(code, obj_env)
new_node = ast.Num(value)
return new_node
else:
return self.generic_visit(node)
示例5: transform_ast
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import fix_missing_locations [as 別名]
def transform_ast(self, node):
"""Apply the AST transformations from self.ast_transformers
Parameters
----------
node : ast.Node
The root node to be transformed. Typically called with the ast.Module
produced by parsing user input.
Returns
-------
An ast.Node corresponding to the node it was called with. Note that it
may also modify the passed object, so don't rely on references to the
original AST.
"""
for transformer in self.ast_transformers:
try:
node = transformer.visit(node)
except Exception:
warn("AST transformer %r threw an error. It will be unregistered." % transformer)
self.ast_transformers.remove(transformer)
if self.ast_transformers:
ast.fix_missing_locations(node)
return node
示例6: updatePyAstVariableUsages
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import fix_missing_locations [as 別名]
def updatePyAstVariableUsages(pyAst, variablesInScope, varsToTurnIntoCalls, isClassContext):
#in the variables we've been handed, every member access chain 'x.y.z' has been
#replaced with an actual variable lookup of the form 'x.y.z'. We need to perform
#this same replacement in the actual python source code we're running, since
#we may not actually have enough information to recover 'x'
replacements = {}
for possible_replacement in variablesInScope:
if '.' in possible_replacement:
replacements[tuple(possible_replacement.split('.'))] = possible_replacement
#we need to deepcopy the AST since the collapser modifies the AST, and this is just a
#slice of a cached tree.
pyAst = ast.fix_missing_locations(copy.deepcopy(pyAst))
if varsToTurnIntoCalls:
pyAst = PyAstFreeVariableAnalyses.replaceUsesWithCalls(pyAst, set(varsToTurnIntoCalls), isClassContext)
pyAst = PyAstFreeVariableAnalyses.collapseFreeVariableMemberAccessChains(
pyAst,
replacements,
isClassContext=isClassContext
)
return ast.fix_missing_locations(pyAst)
示例7: parse_entry_points_setup_cfg
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import fix_missing_locations [as 別名]
def parse_entry_points_setup_cfg(visitor: Visitor) -> None:
if not os.path.exists('setup.cfg'):
return
with visitor.file_ctx('setup.cfg', is_test=False):
parser = configparser.ConfigParser()
parser.read('setup.cfg')
if 'options.entry_points' not in parser:
return
section = parser['options.entry_points']
for k, v in section.items():
for line in v.strip().splitlines():
match = ENTRYPOINT_RE.match(line)
if match:
node = ast.fix_missing_locations(ast.Str(match.group(1)))
visitor.read(match.group(1), node)
示例8: visit
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import fix_missing_locations [as 別名]
def visit(self, node, **kwargs):
if isinstance(node, string_types):
clean = self.preparser(node)
try:
node = ast.fix_missing_locations(ast.parse(clean))
except SyntaxError as e:
from keyword import iskeyword
if any(iskeyword(x) for x in clean.split()):
e.msg = ("Python keyword not valid identifier"
" in numexpr query")
raise e
method = 'visit_' + node.__class__.__name__
visitor = getattr(self, method)
return visitor(node, **kwargs)
示例9: all_preprocessings
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import fix_missing_locations [as 別名]
def all_preprocessings(asttarget, nbits=0):
'Apply all pre-processing transforms'
if not nbits:
nbits = asttools.get_default_nbits(asttarget)
asttarget = ShiftToMult().visit(asttarget)
asttarget = SubToMult().visit(asttarget)
asttarget = RemoveUselessAnd(nbits).visit(asttarget)
ast.fix_missing_locations(asttarget)
return asttarget
示例10: check_eq_z3
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import fix_missing_locations [as 別名]
def check_eq_z3(self, target, pattern):
'Check equivalence with z3'
# pylint: disable=exec-used
getid = asttools.GetIdentifiers()
getid.visit(target)
if getid.functions:
# not checking exprs with functions for now, because Z3
# does not seem to support function declaration with
# arbitrary number of arguments
return False
for var in self.variables:
exec("%s = z3.BitVec('%s', %d)" % (var, var, self.nbits))
target_ast = deepcopy(target)
target_ast = Unflattening().visit(target_ast)
ast.fix_missing_locations(target_ast)
code1 = compile(ast.Expression(target_ast), '<string>', mode='eval')
eval_pattern = deepcopy(pattern)
EvalPattern(self.wildcards).visit(eval_pattern)
eval_pattern = Unflattening().visit(eval_pattern)
ast.fix_missing_locations(eval_pattern)
getid.reset()
getid.visit(eval_pattern)
if getid.functions:
# same reason as before, not using Z3 if there are
# functions
return False
gvar = asttools.GetIdentifiers()
gvar.visit(eval_pattern)
if any(var.isupper() for var in gvar.variables):
# do not check if all patterns have not been replaced
return False
code2 = compile(ast.Expression(eval_pattern), '<string>', mode='eval')
sol = z3.Solver()
if isinstance(eval(code1), int) and eval(code1) == 0:
# cases where target == 0 are too permissive
return False
sol.add(eval(code1) != eval(code2))
return sol.check().r == -1
示例11: get_model
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import fix_missing_locations [as 別名]
def get_model(self, target, pattern):
'When target is constant and wildcards have no value yet'
# pylint: disable=exec-used
if target.n == 0:
# zero is too permissive
return False
getwild = asttools.GetIdentifiers()
getwild.visit(pattern)
if getwild.functions:
# not getting model for expr with functions
return False
wilds = getwild.variables
# let's reduce the model to one wildcard for now
# otherwise it adds a lot of checks...
if len(wilds) > 1:
return False
wil = wilds.pop()
if wil in self.wildcards:
if not isinstance(self.wildcards[wil], ast.Num):
return False
folded = deepcopy(pattern)
folded = Unflattening().visit(folded)
EvalPattern(self.wildcards).visit(folded)
folded = asttools.ConstFolding(folded, self.nbits).visit(folded)
return folded.n == target.n
else:
exec("%s = z3.BitVec('%s', %d)" % (wil, wil, self.nbits))
eval_pattern = deepcopy(pattern)
eval_pattern = Unflattening().visit(eval_pattern)
ast.fix_missing_locations(eval_pattern)
code = compile(ast.Expression(eval_pattern), '<string>', mode='eval')
sol = z3.Solver()
sol.add(target.n == eval(code))
if sol.check().r == 1:
model = sol.model()
for inst in model.decls():
self.wildcards[str(inst)] = ast.Num(int(model[inst].as_long()))
return True
return False
示例12: test_invalid_identitifer
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import fix_missing_locations [as 別名]
def test_invalid_identitifer(self):
m = ast.Module([ast.Expr(ast.Name(u"x", ast.Load()))])
ast.fix_missing_locations(m)
with self.assertRaises(TypeError) as cm:
compile(m, "<test>", "exec")
self.assertIn("identifier must be of type str", str(cm.exception))
示例13: test_invalid_string
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import fix_missing_locations [as 別名]
def test_invalid_string(self):
m = ast.Module([ast.Expr(ast.Str(43))])
ast.fix_missing_locations(m)
with self.assertRaises(TypeError) as cm:
compile(m, "<test>", "exec")
self.assertIn("string must be of type str or uni", str(cm.exception))
示例14: test_fix_missing_locations
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import fix_missing_locations [as 別名]
def test_fix_missing_locations(self):
src = ast.parse('write("spam")')
src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
[ast.Str('eggs')], [], None, None)))
self.assertEqual(src, ast.fix_missing_locations(src))
self.assertEqual(ast.dump(src, include_attributes=True),
"Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
"lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, "
"col_offset=6)], keywords=[], starargs=None, kwargs=None, "
"lineno=1, col_offset=0), lineno=1, col_offset=0), "
"Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, "
"col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], "
"keywords=[], starargs=None, kwargs=None, lineno=1, "
"col_offset=0), lineno=1, col_offset=0)])"
)
示例15: test_compile_manual
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import fix_missing_locations [as 別名]
def test_compile_manual(self):
# check that expressions which are built manually compile
for test in eval_tests:
a = ast.parse(test, "<unknown>", mode="eval")
b = ast.fix_missing_locations(eval(ast.dump(a, annotate_fields=False), vars(ast)))
compile(b, "<unknown>", mode="eval")
for test in exec_tests:
a = ast.parse(test, "<unknown>", mode="exec")
b = ast.fix_missing_locations(eval(ast.dump(a, annotate_fields=False), vars(ast)))
compile(b, "<unknown>", mode="exec")