Python _ast.Name() Examples

The following are 30 code examples of _ast.Name(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module _ast , or try the search function .
Example #1
Source File: ast_transformer.py    From pymtl with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def reverse( self, tree ):
    # Visit the tree
    self.visit( tree )

    # The top of the stack is the new root of the tree
    current = new_root = self.stack.pop()
    name    = [current._name]

    # Pop each node off the stack, update pointers
    while self.stack:
      next_         = self.stack.pop()
      current.value = next_
      current       = next_
      name.append( current._name )

    # Name generation
    new_root._name = '.'.join( name ).replace('.[', '[')

    # Update the last pointer to None, return the new_root
    current.value = None
    return new_root 
Example #2
Source File: ast_transformer.py    From pymtl with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def visit_Name( self, node ):
    new_node = LocalVar( id=node.id )
    new_node._name = node.id
    return ast.copy_location( new_node, node )


#-------------------------------------------------------------------------
# ReorderAST
#-------------------------------------------------------------------------
# Reorders an AST branch beginning with the indicated node.  Intended
# for inverting the order of Name/Attribute chains so that the Name
# node comes first, followed by chains of Attribute/Subscript nodes.
#
# This visitor will also insert Self nodes to represent references to the
# self variable, and remove Index nodes which seem to serve no useful
# purpose. 
Example #3
Source File: source_parser.py    From django-djangui with GNU General Public License v3.0 6 votes vote down vote up
def get_node_args_and_keywords(assigned_objs, assignments, selector=None):
    referenced_nodes = set([])
    selector_line = -1
    assignment_nodes = []
    for node in assignments:
        for i in walk_tree(node):
            if i and isinstance(i, (_ast.keyword, _ast.Name)) and 'id' in i.__dict__:
                if i.id == selector:
                    selector_line = i.lineno
                elif i.lineno == selector_line:
                    referenced_nodes.add(i.id)
    for node in assigned_objs:
        for target in node.targets:
            if getattr(target, 'id', None) in referenced_nodes:
                assignment_nodes.append(node)
    return assignment_nodes 
Example #4
Source File: checker.py    From blackmamba with MIT License 6 votes vote down vote up
def NAME(self, node):
        """
        Handle occurrence of Name (which can be a load/store/delete access.)
        """
        # Locate the name in locals / function / globals scopes.
        if isinstance(node.ctx, (ast.Load, ast.AugLoad)):
            self.handleNodeLoad(node)
            if (node.id == 'locals' and isinstance(self.scope, FunctionScope)
                    and isinstance(node.parent, ast.Call)):
                # we are doing locals() call in current scope
                self.scope.usesLocals = True
        elif isinstance(node.ctx, (ast.Store, ast.AugStore)):
            self.handleNodeStore(node)
        elif isinstance(node.ctx, ast.Del):
            self.handleNodeDelete(node)
        else:
            # must be a Param context -- this only happens for names in function
            # arguments, but these aren't dispatched through here
            raise RuntimeError("Got impossible expression context: %r" % (node.ctx,)) 
Example #5
Source File: checker.py    From blackmamba with MIT License 6 votes vote down vote up
def convert_to_value(item):
    if isinstance(item, ast.Str):
        return item.s
    elif hasattr(ast, 'Bytes') and isinstance(item, ast.Bytes):
        return item.s
    elif isinstance(item, ast.Tuple):
        return tuple(convert_to_value(i) for i in item.elts)
    elif isinstance(item, ast.Num):
        return item.n
    elif isinstance(item, ast.Name):
        result = VariableKey(item=item)
        constants_lookup = {
            'True': True,
            'False': False,
            'None': None,
        }
        return constants_lookup.get(
            result.name,
            result,
        )
    elif (not PY33) and isinstance(item, ast.NameConstant):
        # None, True, False are nameconstants in python3, but names in 2
        return item.value
    else:
        return UnhandledKeyType() 
Example #6
Source File: calc.py    From yui with GNU Affero General Public License v3.0 6 votes vote down vote up
def visit_augassign(self, node: _ast.AugAssign):  # target, op, value
        value = self._run(node.value)
        target = node.target
        target_cls = target.__class__
        op_cls = node.op.__class__

        if target_cls == _ast.Name:
            target_id = target.id  # type: ignore
            self.symbol_table[target_id] = BINOP_TABLE[op_cls](
                self.symbol_table[target_id], value,
            )
        elif target_cls == _ast.Subscript:
            sym = self._run(target.value)  # type: ignore
            xslice = self._run(target.slice)  # type: ignore
            if isinstance(target.slice, _ast.Index):  # type: ignore
                sym[xslice] = BINOP_TABLE[op_cls](sym[xslice], value,)
            else:
                raise BadSyntax('This assign method is not allowed')
        else:
            raise BadSyntax('This assign method is not allowed')
        return 
Example #7
Source File: config.py    From portray with MIT License 6 votes vote down vote up
def setup_py(location: str) -> dict:
    """Returns back any configuration info we are able to determine from a setup.py file"""
    setup_config = {}
    try:
        with open(location) as setup_py_file:
            for node in ast.walk(ast.parse(setup_py_file.read())):
                if (
                    type(node) == _ast.Call
                    and type(getattr(node, "func", None)) == _ast.Name
                    and node.func.id == "setup"  # type: ignore
                ):
                    for keyword in node.keywords:  # type: ignore
                        if keyword.arg == "packages":
                            setup_config["modules"] = ast.literal_eval(keyword.value)
                            break
                    break
    except Exception as error:
        warnings.warn(f"Error ({error}) occurred trying to parse setup.py file: {location}")

    return setup_config 
Example #8
Source File: visitors.py    From pymtl with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def visit_Attribute( self, node ):

    # Visit children
    self.generic_visit( node )

    # If the direct child of this attribute was a portbundle then the node
    # will be removed by the visitor. We must update our name to include
    # portbundle name for proper mangling.
    if self.portbundle:
      new_node = _ast.Name( id  = '{}_{}'.format(self.portbundle, node.attr ),
                            ctx = node.ctx )
      new_node._object = node._object
      node = new_node

    # Attribute is a PortBundle, remove the node, set the submodule name
    if isinstance( node._object, PortBundle ):
      self.portbundle = node.attr
      return None

    # Otherwise, clear the submodule name, return node unmodified
    self.portbundle = None
    return ast.copy_location( node, node ) 
Example #9
Source File: cpp.py    From pymtl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def visit_Call( self, node ):
    self.generic_visit( node )
    if isinstance(node.func, _ast.Name) and node.func.id == "copy":
      return ast.copy_location( node.args[0], node )
    return node

#-----------------------------------------------------------------------
# InferTypes
#----------------------------------------------------------------------- 
Example #10
Source File: ast_transformer.py    From pymtl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def visit_Num( self, node ):
    # Name generation
    node._name = str( node.n )
    self.stack.append( node )

#------------------------------------------------------------------------
# Self
#------------------------------------------------------------------------
# New AST Node for references to self. Based on Attribute node. 
Example #11
Source File: visitors.py    From pymtl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def visit_Attribute( self, node ):

    if node.attr in ['next', 'value', 'n', 'v']:
      # Update the Load/Store information
      node.value.ctx = node.ctx
      return ast.copy_location( node.value, node )

    return node

#-------------------------------------------------------------------------
# RemoveSelf
#-------------------------------------------------------------------------
# Remove references to self.
# TODO: make Attribute attached to self a Name node? 
Example #12
Source File: visitors.py    From pymtl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def visit_Attribute( self, node ):

    # Visit children
    self.generic_visit( node )

    # If the direct child of this attribute was a submodule then the node
    # will be removed by the visitor. We must update our name to include
    # submodule name for proper mangling.
    if self.submodule:
      new_node = _ast.Name( id  = '{}${}'.format(self.submodule, node.attr ),
                            ctx = node.ctx )
      new_node._object = node._object
      node = new_node

    # Attribute is a submodel remove the node, set the submodule name
    if hasattr( node._object, 'class_name' ):
      self.submodule = node._object.name
      return None

    # Otherwise, clear the submodule name, return node unmodified
    self.submodule = None
    return ast.copy_location( node, node )

#-------------------------------------------------------------------------
# FlattenPortBundles
#-------------------------------------------------------------------------
# Transform AST branches for PortBundle signals. A PyMTL signal referenced
# as 's.portbundle.port' would appear in the AST as:
#
#   Attribute(port)
#   |- Attribute(portbundle)
#
# This visitor transforms the AST and name to 's.submodule_port':
#
#   Attribute(portbundle_port)
# 
Example #13
Source File: visitors.py    From pymtl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def visit_Name( self, node ):

    # Name is a PortBundle, remove the node, set the submodule name
    if isinstance( node._object, PortBundle ):
      self.portbundle = node.id
      return None

    # Otherwise, clear the submodule name, return node unmodified
    self.portbundle = None
    return node

#-------------------------------------------------------------------------
# FlattenListAttrs
#-------------------------------------------------------------------------
# Transform AST branches for attribute accesses from indexed objects.
# Attributes referenced as 's.sig[i].attr' would appear in the AST as:
#
#   Attribute(attr)
#   |- Subscript()
#      |- Attribute(sig)
#      |- Index()
#         |- Name(i)
#
# This visitor transforms the AST and name to 's.sig_attr[i]':
#
#   Subscript()
#   |- Attribute(sig_attr)
#   |- Index()
#      |- Name(i)
# 
Example #14
Source File: visitors.py    From pymtl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def visit_For( self, node ):
    if not (isinstance( node.iter,   _ast.Slice ) and
            isinstance( node.target, _ast.Name  )):
      raise VerilogTranslationError(
        'An internal error occured when translating a for loop!\n'
        'Please contact the PyMTL developers!',
        node.lineno
      )
    self.loopvar.add( node.target.id )
    self.generic_visit( node ) 
Example #15
Source File: ast_transformer.py    From pymtl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def visit_Name( self, node ):
    # TODO: Make sure name is actually self before transforming.
    if node.id == self.self_name:
      n = Self( attr=node.id )
    else:
      n = LocalObject( attr=node.id )
    # Name generation
    n._name = node.id
    self.stack.append( n ) 
Example #16
Source File: verilog_behavioral.py    From pymtl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def visit_For(self, node):

    if not (isinstance( node.iter,   _ast.Slice ) or
            isinstance( node.target, _ast.Name  )):
      raise VerilogTranslationError(
        'An unexpected error occurred when translating a "for loop"!\n'
        'Please inform the PyMTL developers!',
        node.lineno
      )

    i     = self.visit( node.target )
    lower = self.visit( node.iter.lower )
    upper = self.visit( node.iter.upper )
    step  = self.visit( node.iter.step  )
    lt_gt = node.iter.lt_gt

    body  = self.fmt_body( node.body )

    x = fmt("""
    for ({i}={lower}; {i} {lt_gt} {upper}; {i}={i}+{step})
    begin
    {body}
    end
    """, self.indent ).format( **locals() )

    return x

  #-----------------------------------------------------------------------
  # visit_BinOp
  #----------------------------------------------------------------------- 
Example #17
Source File: checker.py    From blackmamba with MIT License 5 votes vote down vote up
def EXCEPTHANDLER(self, node):
        if PY2 or node.name is None:
            self.handleChildren(node)
            return

        # 3.x: the name of the exception, which is not a Name node, but
        # a simple string, creates a local that is only bound within the scope
        # of the except: block.

        for scope in self.scopeStack[::-1]:
            if node.name in scope:
                is_name_previously_defined = True
                break
        else:
            is_name_previously_defined = False

        self.handleNodeStore(node)
        self.handleChildren(node)
        if not is_name_previously_defined:
            # See discussion on https://github.com/PyCQA/pyflakes/pull/59

            # We're removing the local name since it's being unbound
            # after leaving the except: block and it's always unbound
            # if the except: block is never entered. This will cause an
            # "undefined name" error raised if the checked code tries to
            # use the name afterwards.
            #
            # Unless it's been removed already. Then do nothing.

            try:
                del self.scope[node.name]
            except KeyError:
                pass 
Example #18
Source File: _task.py    From certvalidator with MIT License 5 votes vote down vote up
def _list_tasks():
    """
    Fetches a list of all valid tasks that may be run, and the args they
    accept. Does not actually import the task module to prevent errors if a
    user does not have the dependencies installed for every task.

    :return:
        A list of 2-element tuples:
         0: a unicode string of the task name
         1: a list of dicts containing the parameter definitions
    """

    out = []
    dev_path = os.path.join(package_root, 'dev')
    for fname in sorted(os.listdir(dev_path)):
        if fname.startswith('.') or fname.startswith('_'):
            continue
        if not fname.endswith('.py'):
            continue
        name = fname[:-3]
        args = ()

        full_path = os.path.join(package_root, 'dev', fname)
        with open(full_path, 'rb') as f:
            full_code = f.read()
            if sys.version_info >= (3,):
                full_code = full_code.decode('utf-8')

        task_node = ast.parse(full_code, filename=full_path)
        for node in ast.iter_child_nodes(task_node):
            if isinstance(node, _ast.Assign):
                if len(node.targets) == 1 \
                        and isinstance(node.targets[0], _ast.Name) \
                        and node.targets[0].id == 'run_args':
                    args = ast.literal_eval(node.value)
                    break

        out.append((name, args))
    return out 
Example #19
Source File: _task.py    From certbuilder with MIT License 5 votes vote down vote up
def _list_tasks():
    """
    Fetches a list of all valid tasks that may be run, and the args they
    accept. Does not actually import the task module to prevent errors if a
    user does not have the dependencies installed for every task.

    :return:
        A list of 2-element tuples:
         0: a unicode string of the task name
         1: a list of dicts containing the parameter definitions
    """

    out = []
    dev_path = os.path.join(package_root, 'dev')
    for fname in sorted(os.listdir(dev_path)):
        if fname.startswith('.') or fname.startswith('_'):
            continue
        if not fname.endswith('.py'):
            continue
        name = fname[:-3]
        args = ()

        full_path = os.path.join(package_root, 'dev', fname)
        with open(full_path, 'rb') as f:
            full_code = f.read()
            if sys.version_info >= (3,):
                full_code = full_code.decode('utf-8')

        task_node = ast.parse(full_code, filename=full_path)
        for node in ast.iter_child_nodes(task_node):
            if isinstance(node, _ast.Assign):
                if len(node.targets) == 1 \
                        and isinstance(node.targets[0], _ast.Name) \
                        and node.targets[0].id == 'run_args':
                    args = ast.literal_eval(node.value)
                    break

        out.append((name, args))
    return out 
Example #20
Source File: _task.py    From asn1crypto with MIT License 5 votes vote down vote up
def _list_tasks():
    """
    Fetches a list of all valid tasks that may be run, and the args they
    accept. Does not actually import the task module to prevent errors if a
    user does not have the dependencies installed for every task.

    :return:
        A list of 2-element tuples:
         0: a unicode string of the task name
         1: a list of dicts containing the parameter definitions
    """

    out = []
    dev_path = os.path.join(package_root, 'dev')
    for fname in sorted(os.listdir(dev_path)):
        if fname.startswith('.') or fname.startswith('_'):
            continue
        if not fname.endswith('.py'):
            continue
        name = fname[:-3]
        args = ()

        full_path = os.path.join(package_root, 'dev', fname)
        with open(full_path, 'rb') as f:
            full_code = f.read()
            if sys.version_info >= (3,):
                full_code = full_code.decode('utf-8')

        task_node = ast.parse(full_code, filename=full_path)
        for node in ast.iter_child_nodes(task_node):
            if isinstance(node, _ast.Assign):
                if len(node.targets) == 1 \
                        and isinstance(node.targets[0], _ast.Name) \
                        and node.targets[0].id == 'run_args':
                    args = ast.literal_eval(node.value)
                    break

        out.append((name, args))
    return out 
Example #21
Source File: _ast_util.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def visit_Slice(self, node):
        if node.lower is not None:
            self.visit(node.lower)
        self.write(":")
        if node.upper is not None:
            self.visit(node.upper)
        if node.step is not None:
            self.write(":")
            if not (isinstance(node.step, Name) and node.step.id == "None"):
                self.visit(node.step) 
Example #22
Source File: calc.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def visit_name(self, node: _ast.Name):  # id, ctx
        ctx = node.ctx.__class__
        if ctx in (_ast.Param, _ast.Del):
            return node.id
        else:
            if node.id in self.symbol_table:
                return self.symbol_table[node.id]
            if node.id in self.global_symbol_table:
                return self.global_symbol_table[node.id]
            raise NameError() 
Example #23
Source File: _ast_util.py    From misp42splunk with GNU Lesser General Public License v3.0 5 votes vote down vote up
def visit_Slice(self, node):
        if node.lower is not None:
            self.visit(node.lower)
        self.write(":")
        if node.upper is not None:
            self.visit(node.upper)
        if node.step is not None:
            self.write(":")
            if not (isinstance(node.step, Name) and node.step.id == "None"):
                self.visit(node.step) 
Example #24
Source File: _ast_util.py    From misp42splunk with GNU Lesser General Public License v3.0 5 votes vote down vote up
def visit_Slice(self, node):
        if node.lower is not None:
            self.visit(node.lower)
        self.write(":")
        if node.upper is not None:
            self.visit(node.upper)
        if node.step is not None:
            self.write(":")
            if not (isinstance(node.step, Name) and node.step.id == "None"):
                self.visit(node.step) 
Example #25
Source File: stmt.py    From equip with Apache License 2.0 5 votes vote down vote up
def make_expr(i, bytecode, context=None):
    if context is None:
      context = _ast.Load()

    op = bytecode[i][2]
    if op == LOAD_CONST:
      return Statement.make_const(i, bytecode)
    elif op in (LOAD_GLOBAL, LOAD_NAME, LOAD_FAST,        \
                STORE_GLOBAL, STORE_NAME, STORE_FAST,     \
                DELETE_GLOBAL, DELETE_NAME, DELETE_FAST):
      return Statement.make_name(i, bytecode, context=context)
    elif op in (LOAD_ATTR, STORE_ATTR, DELETE_ATTR):
      return Statement.make_attribute(i, bytecode, context=context)
    elif op in CALL_OPCODES:
      return Statement.make_call(i, bytecode)
    elif op in BINARY_OP_OPCODES:
      return Statement.make_binary_op(i, bytecode)
    elif op in (BUILD_TUPLE, BUILD_LIST):
      return Statement.make_tuple_list(i, bytecode)
    elif op in (STORE_MAP, BUILD_MAP):
      return Statement.make_dict(i, bytecode)
    elif op in (STORE_SUBSCR, BINARY_SUBSCR):
      return Statement.make_subscript(i, bytecode)
    elif op in STORE_SLICE_OPCODES or op in DELETE_SLICE_OPCODES:
      return Statement.make_store_delete_slice(i, bytecode)
    elif op == BUILD_SLICE:
      return Statement.make_slice(i, bytecode)

    logger.debug("Unhandled >> EXPR:\n%s", show_bytecode(bytecode[:i + 1]))

    # if we don't translate it, we generate a new named expr.
    Statement.UNDEFINED_COUNT += 1
    return i, _ast.Name('Undef_%d' % Statement.UNDEFINED_COUNT, _ast.Load()) 
Example #26
Source File: stmt.py    From equip with Apache License 2.0 5 votes vote down vote up
def make_binary_op(i, bytecode):
    op = bytecode[i][2]
    bin_op = Statement.BIN_OP_AST_NODE_MAP[op]()
    i, rhs = Statement.make_expr(i - 1, bytecode)
    i, lhs = Statement.make_expr(i - 1, bytecode)
    return i, _ast.BinOp(lhs, bin_op, rhs)


  # Build Attr(value=Attr(value=Name(id=a), attr=b), attr=c) <=> a.b.c 
Example #27
Source File: stmt.py    From equip with Apache License 2.0 5 votes vote down vote up
def make_name(i, bytecode, context=None):
    arg = bytecode[i][3]
    if context is None:
      context = _ast.Load()
    return i, _ast.Name(arg, context) 
Example #28
Source File: calc.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def assign(self, node, val):
        cls = node.__class__

        if cls == _ast.Name:
            self.symbol_table[node.id] = val
        elif cls in (_ast.Tuple, _ast.List):
            if not isinstance(val, abc.Iterable):
                raise TypeError(
                    'cannot unpack non-iterable {} object'.format(
                        type(val).__name__,
                    )
                )
            for telem, tval in itertools.zip_longest(
                node.elts, val, fillvalue=PLACEHOLDER,
            ):
                if telem == PLACEHOLDER:
                    raise ValueError('not enough values to unpack')
                if tval == PLACEHOLDER:
                    raise ValueError('too many values to unpack')
                self.assign(telem, tval)
        elif cls == _ast.Subscript:
            sym = self._run(node.value)
            xslice = self._run(node.slice)
            if isinstance(node.slice, _ast.Index):
                sym[xslice] = val
            elif isinstance(node.slice, _ast.Slice):
                sym[slice(xslice.start, xslice.stop)] = val
            elif isinstance(node.slice, _ast.ExtSlice):
                sym[xslice] = val
        else:
            raise BadSyntax('This assign method is not allowed') 
Example #29
Source File: calc.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def delete(self, node):
        cls = node.__class__

        if cls == _ast.Name:
            del self.symbol_table[node.id]
        elif cls == _ast.Tuple:
            for elt in node.elts:
                self.delete(elt) 
Example #30
Source File: calc.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def visit_delete(self, node: _ast.Delete):  # targets
        for target in node.targets:
            target_cls = target.__class__
            if target_cls == _ast.Name:
                del self.symbol_table[target.id]  # type: ignore
            elif target_cls == _ast.Subscript:
                sym = self._run(target.value)  # type: ignore
                xslice = self._run(target.slice)  # type: ignore
                if isinstance(target.slice, _ast.Index):  # type: ignore
                    del sym[xslice]
                else:
                    raise BadSyntax('This delete method is not allowed')
            else:
                raise BadSyntax('This delete method is not allowed')
        return