• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python ast.fix_missing_locations函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中ast.fix_missing_locations函数的典型用法代码示例。如果您正苦于以下问题:Python fix_missing_locations函数的具体用法?Python fix_missing_locations怎么用?Python fix_missing_locations使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了fix_missing_locations函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:104player,项目名称:mutpy,代码行数:29,代码来源:operators.py


示例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)
开发者ID:kernc,项目名称:orange3,代码行数:26,代码来源:owfeatureconstructor.py


示例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))
开发者ID:timm,项目名称:timmnix,代码行数:7,代码来源:test_ast.py


示例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)
开发者ID:fjarri,项目名称:peval,代码行数:25,代码来源:test_function.py


示例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
开发者ID:a-stark,项目名称:qudi,代码行数:30,代码来源:qzmqkernel.py


示例6: generate

 def generate(self):
     self.reset()
     module = ast.Module()
     module.body = []
     module.body = self.random_body()
     ast.fix_missing_locations(module)
     return module
开发者ID:gak,项目名称:skynet,代码行数:7,代码来源:skynet.py


示例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
开发者ID:jpn--,项目名称:larch,代码行数:7,代码来源:aster.py


示例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
开发者ID:bshanks,项目名称:chameleon,代码行数:30,代码来源:compiler.py


示例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
开发者ID:MrSenko,项目名称:cosmic-ray,代码行数:27,代码来源:mutating.py


示例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
开发者ID:OliveBloom,项目名称:everfree-outpost,代码行数:7,代码来源:compile.py


示例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
开发者ID:pythran-travis,项目名称:pythran,代码行数:25,代码来源:expand_imports.py


示例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')
开发者ID:nueces,项目名称:ayrton,代码行数:26,代码来源:__init__.py


示例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
开发者ID:EricSchles,项目名称:pyrthon,代码行数:7,代码来源:_transformer.py


示例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()
开发者ID:satishgoda,项目名称:astviewer,代码行数:28,代码来源:main.py


示例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.")
开发者ID:zopefoundation,项目名称:RestrictedPython,代码行数:28,代码来源:transformer.py


示例16: instrumentModule

def instrumentModule(module_filename, out_dir, is_app=False, in_dir=""):

	mod_file = os.path.join(out_dir, module_filename)

	if os.path.exists(mod_file) and os.stat(os.path.join(in_dir, module_filename)).st_mtime < os.stat(mod_file).st_mtime:
		return

	print "Instrumenting %s" % module_filename

	if "se_dict.py" in module_filename:
		import_se_dict = False
	else:
		import_se_dict = True

	module_contents = file(os.path.join(in_dir, module_filename), "U").read()

	if len(module_contents.strip()) == 0:
		file(mod_file, "w").close()
		return
	root_node = ast.parse(module_contents)
	SplitBoolOpPass1().visit(root_node)
	LiftComputationFromConditionalPass2().visit(root_node)
	BranchIdentifierPass3(import_se_dict).visit(root_node)
	ast.fix_missing_locations(root_node)
	compile(root_node, module_filename, 'exec') # to make sure the new AST is ok
	unparse.Unparser(root_node, file(mod_file, "w"))
开发者ID:algobardo,项目名称:PyExZ3,代码行数:26,代码来源:predigest.py


示例17: get_import_code

 def get_import_code(self, node, fname='<string>'):
     """Get compiled code of all top-level import statements found in the
     AST of node."""
     self._import_nodes = []
     self.visit(node)
     body = []
     for imp_node in self._import_nodes:
         if isinstance(imp_node, ast.ImportFrom) and \
            imp_node.module == '__future__':
             # 'SyntaxError: from __future__ imports must occur at the
             # beginning of the file' is raised if a 'from __future__ import'
             # is wrapped in try-except, so use only the import statement.
             body.append(imp_node)
         else:
             if sys.version_info[0] >= 3: # Python 3
                 body.append(ast.Try(body=[imp_node], handlers=[
                     ast.ExceptHandler(type=None, name=None, body=[ast.Pass()])],
                     orelse=[], finalbody=[]))
             else:
                 body.append(ast.TryExcept(body=[imp_node], handlers=[
                     ast.ExceptHandler(type=None, name=None, body=[ast.Pass()])],
                     orelse=[]))
     node = ast.Module(body=body)
     ast.fix_missing_locations(node)
     code = compile(node, fname, 'exec')
     return code
开发者ID:Dudestin,项目名称:.emacs.d,代码行数:26,代码来源:pycomplete.py


示例18: _convert

 def _convert(self):
     """Attempts to convert expression to distance function.
     
     Constraints are often expressed as inequalities, such as x < 5, meaning
     that a policy is feasible if the value of x is less than 5.  It is
     sometimes useful to know how far a policy is from a feasibility
     threshold.  For example, x = 7 is closer to the feasibility threshold
     than x = 15.
     
     This method attempts to convert a comparison expression to a distance
     expression by manipulating the AST.  If successful, this method creates
     the _distance attribute.  Even if this method is successful, the
     generated expression may not be valid.
     """
     root = ast.parse(self.expr, mode="eval")
     
     if isinstance(root.body, ast.Compare) and len(root.body.ops) == 1:
         left_expr = root.body.left
         right_expr = root.body.comparators[0]
         
         distance_expr = ast.Expression(ast.BinOp(left_expr,
                                                  ast.Sub(),
                                                  right_expr))
         
         ast.fix_missing_locations(distance_expr)
         self._distance = compile(distance_expr, "<AST>", "eval")
开发者ID:Project-Platypus,项目名称:Rhodium,代码行数:26,代码来源:model.py


示例19: ast_print_node

def ast_print_node(values):
    print_node = _ast.Print(
        values=values,
        nl=True
    )
    ast.fix_missing_locations(print_node)
    return print_node
开发者ID:ryanfmurphy,项目名称:music,代码行数:7,代码来源:console.py


示例20: dafile_to_pyast

def dafile_to_pyast(filename, args=None):
    """Translates DistAlgo source file into executable Python AST.

    'filename' is the filename of source file. Optional argument 'args' is a
    Namespace object containing the command line parameters for the compiler.
    Returns the generated Python AST.

    """
    if args is None:
        args = parse_compiler_args([])
    daast = daast_from_file(filename, args)
    if daast is not None:
        pyast = PythonGenerator(filename, args).visit(daast)
        if pyast is None:
            print("Error: unable to generate Python AST from DistAlgo AST"
                  " for file ", filename, file=stderr)
            return None
        assert isinstance(pyast, list) and len(pyast) == 1 and \
            isinstance(pyast[0], ast.Module)
        pyast = pyast[0]
        ast.fix_missing_locations(pyast)
        if args and hasattr(args, 'dump_ast') and args.dump_ast:
            print(ast.dump(pyast, include_attributes=True), file=stderr)
        return pyast
    else:
        return None
开发者ID:DistAlgo,项目名称:distalgo,代码行数:26,代码来源:ui.py



注:本文中的ast.fix_missing_locations函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python ast.get_docstring函数代码示例发布时间:2022-05-24
下一篇:
Python ast.dump函数代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap