Source

pypy / pypy / interpreter / astcompiler / asthelpers.py

from pypy.interpreter.astcompiler import ast, misc
from pypy.interpreter.error import OperationError


class UnacceptableExpressionContext(Exception):

    def __init__(self, node, msg):
        self.node = node
        self.msg = msg
setattr(ast, "UnacceptableExpressionContext", UnacceptableExpressionContext)


class __extend__(ast.AST):

    def as_node_list(self, space):
        raise AssertionError("only for expressions")

    def set_context(self, ctx):
        raise AssertionError("should only be on expressions")


class __extend__(ast.expr):

    constant = False

    def as_node_list(self, space):
        return None

    def set_context(self, ctx):
        if ctx == ast.Del:
            msg = "can't delete %s" % (self._description,)
        else:
            msg = "can't assign to %s" % (self._description,)
        raise UnacceptableExpressionContext(self, msg)


class __extend__(ast.List):

    def as_node_list(self, space):
        return self.elts

    def set_context(self, ctx):
        if self.elts:
            for elt in self.elts:
                elt.set_context(ctx)
        self.ctx = ctx


class __extend__(ast.Attribute):

    def set_context(self, ctx):
        if ctx == ast.Store:
            misc.check_forbidden_name(self.attr, self)
        self.ctx = ctx


class __extend__(ast.Subscript):

    def set_context(self, ctx):
        self.ctx = ctx


class __extend__(ast.Name):

    def set_context(self, ctx):
        if ctx == ast.Store:
            misc.check_forbidden_name(self.id, self)
        self.ctx = ctx


class __extend__(ast.Tuple):

    _description = "()"

    def as_node_list(self, space):
        return self.elts

    def set_context(self, ctx):
        if self.elts:
            for elt in self.elts:
                elt.set_context(ctx)
            self.ctx = ctx
        else:
            # Assignment to () raises an error.
            ast.expr.set_context(self, ctx)

class __extend__(ast.Lambda):

    _description = "lambda"


class __extend__(ast.Call):

    _description = "function call"


class __extend__(ast.BoolOp, ast.BinOp, ast.UnaryOp):

    _description = "operator"


class __extend__(ast.GeneratorExp):

    _description = "generator expression"


class __extend__(ast.Yield):

    _description = "yield expression"


class __extend__(ast.ListComp):

    _description = "list comprehension"


class __extend__(ast.SetComp):

    _description = "set comprehension"


class __extend__(ast.DictComp):

    _description = "dict comprehension"


class __extend__(ast.Dict, ast.Set, ast.Str, ast.Num, ast.Const):

    _description = "literal"


class __extend__(ast.Compare):

    _description = "comparison"


class __extend__(ast.IfExp):

    _description = "conditional expression"


class __extend__(ast.Repr):

    _description = "repr"


class __extend__(ast.Const):

    constant = True

    def as_node_list(self, space):
        try:
            values_w = space.unpackiterable(self.value)
        except OperationError:
            return None
        line = self.lineno
        column = self.col_offset
        return [ast.Const(w_obj, line, column) for w_obj in values_w]


class __extend__(ast.Str):

    constant = True


class __extend__(ast.Num):

    constant = True
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.