当前位置: 首页>>代码示例>>Python>>正文


Python ast.AST属性代码示例

本文整理汇总了Python中ast.AST属性的典型用法代码示例。如果您正苦于以下问题:Python ast.AST属性的具体用法?Python ast.AST怎么用?Python ast.AST使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在ast的用法示例。


在下文中一共展示了ast.AST属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _grab_ast

# 需要导入模块: import ast [as 别名]
# 或者: from ast import AST [as 别名]
def _grab_ast(repo, abspath):
  """Parses the Python file indicated by `abspath`.

  Args:
    * repo (RecipeRepo) - The repo which contains `abspath`. Used for error
      reporting.
    * abspath (str) - The absolute (native) path to the Python file to parse.

  Returns the Python AST object if the file exists and is parsable. Otherwise
  logs an error and returns None.
  """
  assert isinstance(repo, RecipeRepo), type(repo)
  relpath = os.path.relpath(abspath, repo.path)
  assert '..' not in relpath
  try:
    with open(abspath, 'rb') as f:
      return ast.parse(f.read(), relpath)
  except SyntaxError as ex:
    LOGGER.warn('skipping %s: bad syntax: %s', _to_posix(relpath), ex)
  except OSError as ex:
    LOGGER.warn('skipping %s: %s', _to_posix(relpath), ex)
  return None 
开发者ID:luci,项目名称:recipes-py,代码行数:24,代码来源:cmd.py

示例2: _parse_mock_imports

# 需要导入模块: import ast [as 别名]
# 或者: from ast import AST [as 别名]
def _parse_mock_imports(mod_ast, expanded_imports):
  """Parses a module AST node for import statements and resolves them against
  expanded_imports (such as you might get from _expand_mock_imports).

  If an import is not recognized, it is omitted from the returned dictionary.

  Returns a dictionary suitable for eval'ing a statement in mod_ast, with
  symbols from mod_ast's imports resolved to real objects, as per
  expanded_imports.
  """
  ret = {}

  for node in mod_ast.body:
    if isinstance(node, ast.Import):
      for alias in node.names:
        if alias.name in expanded_imports:
          ret[alias.asname or alias.name] = expanded_imports[alias.name]
    elif isinstance(node, ast.ImportFrom):
      if node.level == 0:
        for alias in node.names:
          fullname ='%s.%s' % (node.module, alias.name)
          if fullname in expanded_imports:
            ret[alias.asname or alias.name] = expanded_imports[fullname]

  return ret 
开发者ID:luci,项目名称:recipes-py,代码行数:27,代码来源:cmd.py

示例3: __init__

# 需要导入模块: import ast [as 别名]
# 或者: from ast import AST [as 别名]
def __init__(self):
        # type: () -> None
        self.exceptions = set()  # type: Set[str]

        # If we're in a bare handler, we have to capture new
        # exceptions raised separately from the existing ones.
        # So, we copy the existing exceptions over here.
        # This complicates the logic, for the calling class (as
        # contextual operations have to account for two cases),
        # but it doesn't seem avoidable.
        self.bare_handler_exceptions = None # type: Optional[Set[str]]

        # A lookup from variable names to AST nodes.
        # If the variable name occurs in a raise expression,
        # then the exception will be added using this lookup.
        self.variables = dict()  # type: Dict[str, Union[str, List[str]]]

        # The error(s) which the current exception block is
        # handling. (Since we only handle one handler at a time
        # in the context, and since they don't repeat the
        # exception, it's fine to overwrite this value.)
        self.handling = None  # type: Optional[List[str]] 
开发者ID:terrencepreilly,项目名称:darglint,代码行数:24,代码来源:raise_visitor.py

示例4: 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
    values = []
    for x in ast.walk(node):
        if isinstance(x, (ast.stmt, ast.ExceptHandler)):
            values.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
                    values.append(val[0].lineno - 1 - 1)
    values.sort()
    insert_index = bisect_right(values, lineno)
    start = values[insert_index - 1]
    if insert_index >= len(values):
        end = None
    else:
        end = values[insert_index]
    return start, end 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:24,代码来源:source.py

示例5: ast_to_string

# 需要导入模块: import ast [as 别名]
# 或者: from ast import AST [as 别名]
def ast_to_string(ast_node, line_indent=''):
    next_line_indent = line_indent + '  '

    if isinstance(ast_node, ast.AST):
        return (ast_node.__class__.__name__
                + '('
                + ','.join('\n' + next_line_indent + field_name + ' = ' + ast_to_string(child_node, next_line_indent)
                           for field_name, child_node in ast.iter_fields(ast_node))
                + ')')
    elif isinstance(ast_node, list):
        return ('['
                + ','.join('\n' + next_line_indent + ast_to_string(child_node, next_line_indent)
                           for child_node in ast_node)
                + ']')
    else:
        return repr(ast_node) 
开发者ID:google,项目名称:tmppy,代码行数:18,代码来源:_ast_to_string.py

示例6: add_symbol

# 需要导入模块: import ast [as 别名]
# 或者: from ast import AST [as 别名]
def add_symbol(self,
                   name: str,
                   expr_type: ir2.ExprType,
                   definition_ast_node: ast.AST,
                   is_only_partially_defined: bool,
                   is_function_that_may_throw: bool,
                   source_module: Optional[str] = None):
        """
        Adds a symbol to the symbol table.

        This throws an error (created by calling `create_already_defined_error(previous_type)`) if a symbol with the
        same name and different type was already defined in this scope.
        """
        if is_function_that_may_throw:
            assert isinstance(expr_type, ir2.FunctionType)

        self._check_not_already_defined(name, definition_ast_node)

        self.symbol_table.add_symbol(name=name,
                                     expr_type=expr_type,
                                     definition_ast_node=definition_ast_node,
                                     is_only_partially_defined=is_only_partially_defined,
                                     is_function_that_may_throw=is_function_that_may_throw,
                                     source_module=source_module) 
开发者ID:google,项目名称:tmppy,代码行数:26,代码来源:_ast_to_ir2.py

示例7: test_AST_objects

# 需要导入模块: import ast [as 别名]
# 或者: from ast import AST [as 别名]
def test_AST_objects(self):
        x = ast.AST()
        self.assertEqual(x._fields, ())

        with self.assertRaises(AttributeError):
            x.vararg

        with self.assertRaises(AttributeError):
            x.foobar = 21

        with self.assertRaises(AttributeError):
            ast.AST(lineno=2)

        with self.assertRaises(TypeError):
            # "_ast.AST constructor takes 0 positional arguments"
            ast.AST(2) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:18,代码来源:test_ast.py

示例8: fields_same

# 需要导入模块: import ast [as 别名]
# 或者: from ast import AST [as 别名]
def fields_same(n1: ast.AST, n2: ast.AST) -> bool:
    for (a1, v1), (a2, v2) in zip(ast.iter_fields(n1), ast.iter_fields(n2)):
        # ignore ast attributes, they'll be covered by walk
        if a1 != a2:
            return False
        elif _all_isinstance((v1, v2), ast.AST):
            continue
        elif _all_isinstance((v1, v2), (list, tuple)):
            if len(v1) != len(v2):
                return False
            # ignore sequences which are all-ast, they'll be covered by walk
            elif _all_isinstance(v1, ast.AST) and _all_isinstance(v2, ast.AST):
                continue
            elif v1 != v2:
                return False
        elif v1 != v2:
            return False
    return True 
开发者ID:asottile,项目名称:pyupgrade,代码行数:20,代码来源:pyupgrade.py

示例9: end_col_offset

# 需要导入模块: import ast [as 别名]
# 或者: from ast import AST [as 别名]
def end_col_offset(self) -> Optional[int]:
        """End col offset: Python 3.8 will have this defined, in Python 3.7 it will be None."""
        ecol: Optional[int] = getattr(self.node, "end_col_offset", None)
        return ecol


####################################################################################################
# MUTATE AST Definitions
# Includes MutateBase and Mixins for 3.7 and 3.8 AST support
# MutateAST is constructed from Base + Mixins depending on sys.version_info
#################################################################################################### 
开发者ID:EvanKepner,项目名称:mutatest,代码行数:13,代码来源:transformers.py

示例10: __init__

# 需要导入模块: import ast [as 别名]
# 或者: from ast import AST [as 别名]
def __init__(
        self,
        target_idx: Optional[LocIndex] = None,
        mutation: Optional[Any] = None,
        readonly: bool = False,
        src_file: Optional[Union[Path, str]] = None,
    ) -> None:
        """Create the AST node transformer for mutations.

        If readonly is set to True then no transformations are applied;
        however, the locs attribute is updated with the locations of nodes that could
        be transformed. This allows the class to function both as an inspection method
        and as a mutation transformer.

        Note that different nodes handle the ``LocIndex`` differently based on the context. For
        example, ``visit_BinOp`` uses direct AST types, while ``visit_NameConstant`` uses values,
        and ``visit_AugAssign`` uses custom strings in a dictionary mapping.

        All ``visit_`` methods take the ``node`` as an argument and rely on the class properties.

        This MutateBase class is designed to be implemented with the appropriate Mixin Class
        for supporting either Python 3.7 or Python 3.8 ASTs. If the base class is used
        directly certain operations - like ``visit_If`` and ``visit_NameConstant`` will not
        work as intended..

        Args:
            target_idx: Location index for the mutatest in the AST
            mutation: the mutatest to apply, may be a type or a value
            readonly: flag for read-only operations, used to visit nodes instead of transform
            src_file: Source file name, used for logging purposes
        """
        self.locs: Set[LocIndex] = set()

        # managed via @property
        self._target_idx = target_idx
        self._mutation = mutation
        self._readonly = readonly
        self._src_file = src_file 
开发者ID:EvanKepner,项目名称:mutatest,代码行数:40,代码来源:transformers.py

示例11: target_idx

# 需要导入模块: import ast [as 别名]
# 或者: from ast import AST [as 别名]
def target_idx(self) -> Optional[LocIndex]:
        """Location index for the mutation in the AST"""
        return self._target_idx 
开发者ID:EvanKepner,项目名称:mutatest,代码行数:5,代码来源:transformers.py

示例12: visit_BinOp

# 需要导入模块: import ast [as 别名]
# 或者: from ast import AST [as 别名]
def visit_BinOp(self, node: ast.BinOp) -> ast.AST:
        """BinOp nodes are bit-shifts and general operators like add, divide, etc."""
        self.generic_visit(node)
        log_header = f"visit_BinOp: {self.src_file}:"

        # default case for this node, can be BinOpBC or BinOpBS
        ast_class = "BinOp"
        op_type = type(node.op)

        # binop_bit_cmp_types: Set[type] = {ast.BitAnd, ast.BitOr, ast.BitXor}
        if op_type in {ast.BitAnd, ast.BitOr, ast.BitXor}:
            ast_class = "BinOpBC"

        # binop_bit_shift_types: Set[type] = {ast.LShift, ast.RShift}
        if op_type in {ast.LShift, ast.RShift}:
            ast_class = "BinOpBS"

        node_span = NodeSpan(node)
        idx = LocIndex(
            ast_class=ast_class,
            lineno=node_span.lineno,
            col_offset=node_span.col_offset,
            op_type=op_type,
            end_lineno=node_span.end_lineno,
            end_col_offset=node_span.end_col_offset,
        )

        self.locs.add(idx)

        if idx == self.target_idx and self.mutation and not self.readonly:
            LOGGER.debug("%s mutating idx: %s with %s", log_header, self.target_idx, self.mutation)
            return ast.copy_location(
                ast.BinOp(left=node.left, op=self.mutation(), right=node.right), node
            )

        LOGGER.debug("%s (%s, %s): no mutations applied.", log_header, node.lineno, node.col_offset)
        return node 
开发者ID:EvanKepner,项目名称:mutatest,代码行数:39,代码来源:transformers.py

示例13: mixin_NameConstant

# 需要导入模块: import ast [as 别名]
# 或者: from ast import AST [as 别名]
def mixin_NameConstant(self, node: Union[ast.NameConstant, ast.Constant]) -> ast.AST:
        """Constants: ``True, False, None``.

        This method is called by using the Mixin classes for handling the difference of
        ast.NameConstant (Py 3.7) an ast.Constant (Py 3.8).
        """
        self.generic_visit(node)
        log_header = f"visit_NameConstant: {self.src_file}:"

        node_span = NodeSpan(node)
        idx = LocIndex(
            ast_class="NameConstant",
            lineno=node_span.lineno,
            col_offset=node_span.col_offset,
            op_type=node.value,
            end_lineno=node_span.end_lineno,
            end_col_offset=node_span.end_col_offset,
        )
        self.locs.add(idx)

        if idx == self.target_idx and not self.readonly:
            LOGGER.debug("%s mutating idx: %s with %s", log_header, self.target_idx, self.mutation)
            return ast.copy_location(self.constant_type(value=self.mutation), node)

        LOGGER.debug("%s (%s, %s): no mutations applied.", log_header, node.lineno, node.col_offset)
        return node 
开发者ID:EvanKepner,项目名称:mutatest,代码行数:28,代码来源:transformers.py

示例14: visit_Constant

# 需要导入模块: import ast [as 别名]
# 或者: from ast import AST [as 别名]
def visit_Constant(self, node: ast.Constant) -> ast.AST:
        """Constants: https://bugs.python.org/issue32892
            NameConstant: ``True, False, None``.
            Num: isinstance(int, float)
            Str: isinstance(str)
        """
        # NameConstant behavior consistent with Python 3.7
        if isinstance(node.value, bool) or node.value is None:
            return self.mixin_NameConstant(node)  # type: ignore

        return node


# PYTHON 3.7 
开发者ID:EvanKepner,项目名称:mutatest,代码行数:16,代码来源:transformers.py

示例15: get_mutations_for_target

# 需要导入模块: import ast [as 别名]
# 或者: from ast import AST [as 别名]
def get_mutations_for_target(target: LocIndex) -> Set[Any]:
    """Given a target, find all the mutations that could apply from the AST definitions.

    Args:
        target: the location index target

    Returns:
        Set of types that can mutated into the target location.
    """
    search_space: List[Set[Any]] = [m.operations for m in get_compatible_operation_sets()]
    mutation_ops: Set[Any] = set()

    for potential_ops in search_space:
        if target.op_type in potential_ops:
            LOGGER.debug("Potential mutatest operations found for target: %s", target.op_type)
            mutation_ops = potential_ops.copy()
            mutation_ops.remove(target.op_type)

            # Special case for If_Statement since that is a default to transform to True or False
            # but not a validation mutation target by itself
            if "If_Statement" in mutation_ops:
                mutation_ops.remove("If_Statement")

            break

    return mutation_ops 
开发者ID:EvanKepner,项目名称:mutatest,代码行数:28,代码来源:transformers.py


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