Python ast.Constant() Examples

The following are 17 code examples of ast.Constant(). 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: python2ir.py    From ppci with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def gen_expr(self, expr):
        """ Generate code for a single expression """
        with self.use_location(expr):
            if isinstance(expr, ast.BinOp):
                value = self.gen_binop(expr)
            elif isinstance(expr, ast.Name):
                value = self.gen_name(expr)
            elif isinstance(expr, ast.Num):
                value = self.gen_num(expr)
            elif isinstance(expr, ast.Call):
                value = self.gen_call(expr)
            elif isinstance(expr, ast.Constant):
                value = self.gen_constant(expr)
            else:  # pragma: no cover
                self.not_impl(expr)
        return value 
Example #2
Source File: source_visitor.py    From vermin with MIT License 6 votes vote down vote up
def visit_JoinedStr(self, node):
    self.__fstrings = True
    self.__vvprint("f-strings require 3.6+")
    if hasattr(node, "values"):
      total = len(node.values)
      for i in range(total):
        val = node.values[i]
        # A self-referencing f-string will be at the end of the Constant, like "..stuff..expr=", and
        # the next value will be a FormattedValue(value=..) with Names or nested Calls with Names
        # inside, for instance.
        if isinstance(val, ast.Constant) and hasattr(val, "value") and \
           isinstance(val.value, str) and val.value.strip().endswith("=") and i + 1 < total:
            next_val = node.values[i + 1]
            if isinstance(next_val, ast.FormattedValue):
              fstring_value =\
                self.__trim_fstring_value(self.__extract_fstring_value(next_val.value))
              if len(fstring_value) > 0 and\
                self.__trim_fstring_value(val.value).endswith(fstring_value + "="):
                  self.__fstrings_self_doc = True
                  self.__vvprint("self-documenting fstrings require 3.8+")
                  break

    self.generic_visit(node)

  # Mark variable names as aliases. 
Example #3
Source File: test_transformer.py    From flynt with MIT License 6 votes vote down vote up
def test_unpack():

    txt = """a + 'Hello' + b + 'World'"""
    node = ast.parse(txt)

    seq = unpack_binop(node.body[0].value)

    assert len(seq) == 4

    assert isinstance(seq[0], ast.Name)
    assert seq[0].id == "a"

    assert isinstance(seq[1], ast.Constant)
    assert seq[1].value == "Hello"

    assert isinstance(seq[2], ast.Name)
    assert seq[2].id == "b"

    assert isinstance(seq[3], ast.Constant)
    assert seq[3].value == "World" 
Example #4
Source File: unparse.py    From android_universal with MIT License 5 votes vote down vote up
def _Attribute(self,t):
        self.dispatch(t.value)
        # Special case: 3.__abs__() is a syntax error, so if t.value
        # is an integer literal then we need to either parenthesize
        # it or add an extra space to get 3 .__abs__().
        if ((isinstance(t.value, ast.Num) and isinstance(t.value.n, int))
           or (isinstance(t.value, ast.Constant) and isinstance(t.value.value, int))):
            self.write(" ")
        self.write(".")
        self.write(t.attr) 
Example #5
Source File: transformers.py    From mutatest with MIT License 5 votes vote down vote up
def constant_type(self) -> Union[Type[ast.NameConstant], Type[ast.Constant]]:
        """Overridden using the MixinClasses for NameConstant(3.7) vs. Constant(3.8)."""
        raise NotImplementedError 
Example #6
Source File: unparse.py    From odoo13-x64 with GNU General Public License v3.0 5 votes vote down vote up
def _Attribute(self,t):
        self.dispatch(t.value)
        # Special case: 3.__abs__() is a syntax error, so if t.value
        # is an integer literal then we need to either parenthesize
        # it or add an extra space to get 3 .__abs__().
        if ((isinstance(t.value, ast.Num) and isinstance(t.value.n, int))
           or (isinstance(t.value, ast.Constant) and isinstance(t.value.value, int))):
            self.write(" ")
        self.write(".")
        self.write(t.attr) 
Example #7
Source File: source_visitor.py    From vermin with MIT License 5 votes vote down vote up
def visit_Constant(self, node):
    # From 3.8, Bytes(s=b'%x') is represented as Constant(value=b'%x', kind=None) instead.
    if hasattr(node, "value") and isinstance(node.value, bytes):
      self.__bytesv3 = True
      self.__vvprint("byte strings (b'..') require 3+ (or 2.6+ as `str` synonym)")

      for directive in BYTES_DIRECTIVE_REGEX.findall(str(node.value)):
        self.__add_bytes_directive(directive, node.lineno) 
Example #8
Source File: checker.py    From pyflakes with MIT License 5 votes vote down vote up
def _is_constant(node):
        return isinstance(node, ast.Constant) or _is_tuple_constant(node) 
Example #9
Source File: checker.py    From pyflakes with MIT License 5 votes vote down vote up
def _is_singleton(node):  # type: (ast.AST) -> bool
        return (
            isinstance(node, ast.Constant) and
            isinstance(node.value, (bool, type(Ellipsis), type(None)))
        ) 
Example #10
Source File: candidates.py    From flynt with MIT License 5 votes vote down vote up
def is_str_literal(node):
    """ Returns True if a node is a string literal """
    if isinstance(node, ast.Constant):
        return isinstance(node.value, str)
    elif isinstance(node, ast.JoinedStr):
        return True
    else:
        return False 
Example #11
Source File: transformer.py    From flynt with MIT License 5 votes vote down vote up
def visit_BinOp(self, node: ast.BinOp):
        """
        Transforms a string concat to an f-string
        """
        if is_string_concat(node):
            self.counter += 1
            left, right = node.left, node.right
            left = self.visit(left)
            right = self.visit(right)

            if not check_sns_depth(left) or not check_sns_depth(right):
                node.left = left
                node.right = right
                return node

            parts = []
            for p in [left, right]:
                if isinstance(p, ast.JoinedStr):
                    parts += p.values
                else:
                    parts.append(p)

            segments = []
            for p in parts:
                if isinstance(p, ast.Constant):
                    segments.append(ast_string_node(p.value))
                else:
                    segments.append(ast_formatted_value(p))

            return ast.JoinedStr(segments)
        else:
            return self.generic_visit(node) 
Example #12
Source File: github_increment_version.py    From py-ipv8 with GNU Lesser General Public License v3.0 5 votes vote down vote up
def parse_doc_conf():
    print("[1/8] | Parsing doc/conf.py file")

    with open('doc/conf.py', 'r') as f:
        file_contents = f.read()

    treeobj = ast.parse(file_contents, 'doc/conf.py')

    copyright_element = None
    version_element = None
    release_element = None
    for element in treeobj.body:
        if isinstance(element, ast.Assign) and isinstance(element.value, ast.Constant):
            if element.targets[0].id == "copyright":
                copyright_element = element.value
            elif element.targets[0].id == "version":
                version_element = element.value
            elif element.targets[0].id == "release":
                release_element = element.value

    if not copyright_element:
        print("No 'copyright' assignment found in doc/conf.py")
        sys.exit(1)
    if not version_element:
        print("No 'version' assignment found in doc/conf.py")
        sys.exit(1)
    if not release_element:
        print("No 'release' assignment found in doc/conf.py")
        sys.exit(1)

    return file_contents, (copyright_element, version_element, release_element) 
Example #13
Source File: transformers.py    From mutatest with MIT License 5 votes vote down vote up
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 
Example #14
Source File: transformers.py    From mutatest with MIT License 5 votes vote down vote up
def constant_type(self) -> Type[ast.Constant]:
        return ast.Constant 
Example #15
Source File: transformers.py    From mutatest with MIT License 5 votes vote down vote up
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 
Example #16
Source File: analyzer.py    From Python-DevOps with MIT License 4 votes vote down vote up
def visit_ImportFrom(self, node):
        self.logger.debug(
            'ImportFrom: from %s import %s'
            % (node.module, [format_alias(x) for x in node.names])
        )

        tgt_name = node.module
        from_node = self.get_node_of_current_namespace()
        to_node = self.get_node(
            '', tgt_name, node, flavor = Flavor.MODULE
        )  # module, in top-level namespace
        self.logger.debug('Use from %s to ImportFrom %s' % (from_node, to_node))
        if self.add_uses_edge(from_node, to_node):
            self.logger.info(
                'New edge added for Use from %s to ImportFrom %s'
                % (from_node, to_node)
            )

        if tgt_name in self.module_names:
            mod_name = self.module_names[tgt_name]
        else:
            mod_name = tgt_name

        for import_item in node.names:
            name = import_item.name
            new_name = (
                import_item.asname if import_item.asname is not None else name
            )
            # we imported the identifier name from the module mod_name
            tgt_id = self.get_node(
                mod_name, name, node, flavor = Flavor.IMPORTEDITEM
            )
            self.set_value(new_name, tgt_id)
            self.logger.info('From setting name %s to %s' % (new_name, tgt_id))

    #    # Edmund Horner's original post has info on what this fixed in Python 2.
    #    # https://ejrh.wordpress.com/2012/01/31/call-graphs-in-python-part-2/
    #    #
    #    # Essentially, this should make '.'.join(...) see str.join.
    #    # Pyan3 currently handles that in resolve_attribute() and get_attribute().
    #    #
    #    # Python 3.4 does not have ast.Constant, but 3.6 does. Disabling for now.
    #    # TODO: revisit this part after upgrading Python.
    #    #
    #    def visit_Constant(self, node):
    #        self.logger.debug("Constant %s" % (node.value))
    #        t = type(node.value)
    #        tn = t.__name__
    #        self.last_value = self.get_node('', tn, node)

    # attribute access (node.ctx determines whether set (ast.Store) or get (ast.Load)) 
Example #17
Source File: transformers.py    From mutatest with MIT License 4 votes vote down vote up
def visit_If(self, node: ast.If) -> ast.AST:
        """If statements e.g. If ``x == y`` is transformed to ``if True`` and ``if False``.

        This visit method only works when the appropriate Mixin is used.
        """
        self.generic_visit(node)
        log_header = f"visit_If: {self.src_file}:"

        # default for a comparison is "If_Statement" which will be changed to True/False
        # If_Statement is not set as a mutation target, controlled in get_mutations function
        if_type = "If_Statement"

        # Py 3.7 vs 3.8 - 3.7 uses NameConstant, 3.8 uses Constant
        if_mutations = {
            "If_True": self.constant_type(value=True),
            "If_False": self.constant_type(value=False),
        }
        if type(node.test) == self.constant_type:
            if_type: str = f"If_{bool(node.test.value)}"  # type: ignore

        node_span = NodeSpan(node)
        idx = LocIndex(
            ast_class="If",
            lineno=node_span.lineno,
            col_offset=node_span.col_offset,
            op_type=if_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.fix_missing_locations(
                ast.copy_location(
                    ast.If(test=if_mutations[self.mutation], body=node.body, orelse=node.orelse),
                    node,
                )
            )

        LOGGER.debug("%s (%s, %s): no mutations applied.", log_header, node.lineno, node.col_offset)
        return node