本文整理汇总了Python中ast.fix_missing_locations函数的典型用法代码示例。如果您正苦于以下问题:Python fix_missing_locations函数的具体用法?Python fix_missing_locations怎么用?Python fix_missing_locations使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了fix_missing_locations函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: visit
def visit(self, node):
if self.has_notmutate(node) or (self.coverage_injector and not self.coverage_injector.is_covered(node)):
return
if self.only_mutation and self.only_mutation.node != node and self.only_mutation.node not in node.children:
return
self.fix_lineno(node)
visitors = self.find_visitors(node)
if visitors:
for visitor in visitors:
try:
if self.sampler and not self.sampler.is_mutation_time():
raise MutationResign
if self.only_mutation and \
(self.only_mutation.node != node or self.only_mutation.visitor != visitor.__name__):
raise MutationResign
new_node = visitor(node)
self.visitor = visitor.__name__
self.current_node = node
self.fix_node_internals(node, new_node)
ast.fix_missing_locations(new_node)
yield new_node
except MutationResign:
pass
finally:
for new_node in self.generic_visit(node):
yield new_node
else:
for new_node in self.generic_visit(node):
yield new_node
示例2: make_lambda
def make_lambda(expression, args, values):
def make_arg(name):
if sys.version_info >= (3, 0):
return ast.arg(arg=name, annotation=None)
else:
return ast.Name(id=name, ctx=ast.Param(), lineno=1, col_offset=0)
lambda_ = ast.Lambda(
args=ast.arguments(
args=[make_arg(arg) for arg in args + values],
varargs=None,
varargannotation=None,
kwonlyargs=[],
kwarg=None,
kwargannotation=None,
defaults=[ast.Num(i) for i in range(len(values))],
kw_defaults=[]),
body=expression.body,
)
lambda_ = ast.copy_location(lambda_, expression.body)
exp = ast.Expression(body=lambda_, lineno=1, col_offset=0)
ast.dump(exp)
ast.fix_missing_locations(exp)
GLOBALS = __GLOBALS.copy()
GLOBALS["__builtins__"] = {}
return eval(compile(exp, "<lambda>", "eval"), GLOBALS)
示例3: test_invalid_string
def test_invalid_string(self):
m = ast.Module([ast.Expr(ast.Str(42))])
ast.fix_missing_locations(m)
with self.assertRaises(TypeError) as cm:
compile(m, "<test>", "exec")
if support.check_impl_detail():
self.assertIn("string must be of type str or uni", str(cm.exception))
示例4: function_from_source
def function_from_source(source, globals_=None):
"""
A helper function to construct a Function object from a source
with custom __future__ imports.
"""
module = ast.parse(unindent(source))
ast.fix_missing_locations(module)
for stmt in module.body:
if type(stmt) == ast.FunctionDef:
tree = stmt
name = stmt.name
break
else:
raise ValueError("No function definitions found in the provided source")
code_object = compile(module, '<nofile>', 'exec', dont_inherit=True)
locals_ = {}
eval(code_object, globals_, locals_)
function_obj = locals_[name]
function_obj._peval_source = astunparse.unparse(tree)
return Function.from_object(function_obj)
示例5: transform_ast
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 InputRejected:
# User-supplied AST transformers can reject an input by raising
# an InputRejected. Short-circuit in this case so that we
# don't unregister the transform.
raise
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: generate
def generate(self):
self.reset()
module = ast.Module()
module.body = []
module.body = self.random_body()
ast.fix_missing_locations(module)
return module
示例7: asterize
def asterize(cmd, mode="eval"):
tree = ast.parse(cmd, mode=mode)
tree = AstWrapper().visit(tree)
# Add lineno & col_offset to the nodes we created
ast.fix_missing_locations(tree)
co = compile(tree, "<ast>", mode)
return co
示例8: __init__
def __init__(self, engine, node):
self._scopes = [set()]
self._expression_cache = {}
self._translations = []
self._markers = set()
self._engine = ExpressionCompiler(
engine,
self._expression_cache,
self._markers
)
if isinstance(node_annotations, dict):
self.lock.acquire()
backup = node_annotations.copy()
else:
backup = None
try:
module = ast.Module([])
module.body += self.visit(node)
ast.fix_missing_locations(module)
generator = TemplateCodeGenerator(module)
finally:
if backup is not None:
node_annotations.clear()
node_annotations.update(backup)
self.lock.release()
self.code = generator.code
示例9: visit_mutation_site
def visit_mutation_site(self, node, op, num_mutations):
"""Potentially mutate `node`, returning the mutated version.
`Operator` calls this when AST iteration reaches a
potential mutation site. If that site is scheduled for
mutation, the subclass instance will be asked to perform the
mutation.
"""
# If the current operator will do at least that many mutations,
# then let it make the mutation now.
if self._count <= self._target < self._count + num_mutations:
assert self._activation_record is None
assert self._target - self._count < num_mutations
self._activation_record = {
'operator': _full_module_name(op),
'occurrence': self._target,
'line_number': cosmic_ray.util.get_line_number(node)
}
old_node = node
node = op.mutate(old_node, self._target - self._count)
# add lineno and col_offset for newly created nodes
ast.fix_missing_locations(node)
self._count += num_mutations
return node
示例10: gen_module
def gen_module(self, script):
func_args = ast.arguments(args=[])
func = [ast.FunctionDef(name='init', args=func_args, body=self.gen_init_body(script))]
m = ast.Module(self.gen_preamble() + func, lineno=0, col_offset=0)
FuncFix().visit(m)
ast.fix_missing_locations(m)
return m
示例11: visit_Module
def visit_Module(self, node):
"""
Visit the whole module and add all import at the top level.
>> import math
Becomes
>> import math as pythonic::math
And
>> import numpy.linalg
Becomes
>> import numpy as pythonic::numpy
"""
node.body = [k for k in (self.visit(n) for n in node.body) if k]
imports = [ast.Import([ast.alias(i, namespace + "::" + i)])
for i in self.imports]
node.body = imports + node.body
ast.fix_missing_locations(node)
return node
示例12: __init__
def __init__ (self, script=None, file=None, tree=None, globals=None,
locals=None, **kwargs):
if script is None and file is not None:
# it's a pity that compile() does not accept a file as input
# so we could avoid reading the whole file
script= open (file).read ()
else:
file= 'arg_to_main'
self.environ= Environment (globals, locals, **kwargs)
if tree is None:
tree= ast.parse (script)
# ImportFrom(module='bar', names=[alias(name='baz', asname=None)], level=0)
node= ImportFrom (module='ayrton',
names=[alias (name='CommandWrapper', asname=None)],
level=0)
node.lineno= 0
node.col_offset= 0
ast.fix_missing_locations (node)
tree.body.insert (0, node)
tree= CrazyASTTransformer(self.environ).visit (tree)
self.options= {}
self.source= compile (tree, file, 'exec')
示例13: transform
def transform(src):
""" Transforms the given source to use pvectors, pmaps and psets to replace built in structures """
tree = ast.parse(src)
transformer = PyrsistentTransformer()
new_tree = transformer.visit(tree)
ast.fix_missing_locations(new_tree)
return new_tree
示例14: _update_widgets
def _update_widgets(self):
""" Updates the tree and editor widgets.
"""
self.setWindowTitle('{} - {}'.format(self._file_name, PROGRAM_NAME))
self.editor.setPlainText(self._source_code)
if not self._source_code:
logger.debug("Empty source code, use empty tree.")
self.ast_tree.clear()
return
try:
syntax_tree = ast.parse(self._source_code, filename=self._file_name, mode=self._mode)
ast.fix_missing_locations(syntax_tree) # Doesn't seem to do anything.
except Exception as ex:
if DEBUGGING:
raise
else:
stack_trace = traceback.format_exc()
msg = "Unable to parse file: {}\n\n{}\n\n{}" \
.format(self._file_name, ex, stack_trace)
logger.exception(ex)
QtWidgets.QMessageBox.warning(self, 'error', msg)
else:
last_pos = self.editor.get_last_pos()
root_item = self.ast_tree.populate(syntax_tree, last_pos, root_label=self._file_name)
self.ast_tree.setCurrentItem(root_item)
self.ast_tree.expand_reset()
示例15: inject_print_collector
def inject_print_collector(self, node, position=0):
print_used = self.print_info.print_used
printed_used = self.print_info.printed_used
if print_used or printed_used:
# Add '_print = _print_(_getattr_)' add the top of a
# function/module.
_print = ast.Assign(
targets=[ast.Name('_print', ast.Store())],
value=ast.Call(
func=ast.Name("_print_", ast.Load()),
args=[ast.Name("_getattr_", ast.Load())],
keywords=[]))
if isinstance(node, ast.Module):
_print.lineno = position
_print.col_offset = position
ast.fix_missing_locations(_print)
else:
copy_locations(_print, node)
node.body.insert(position, _print)
if not printed_used:
self.warn(node, "Prints, but never reads 'printed' variable.")
elif not print_used:
self.warn(node, "Doesn't print, but reads 'printed' variable.")