1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
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
_description = None
def as_node_list(self, space):
return None
def set_context(self, ctx):
d = self._description
if d is None:
d = "%r" % (self,)
if ctx == ast.Del:
msg = "can't delete %s" % (d,)
else:
msg = "can't assign to %s" % (d,)
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.Bytes, ast.Num, ast.Const):
_description = "literal"
class __extend__(ast.Compare):
_description = "comparison"
class __extend__(ast.Starred):
_description = "starred expression"
def set_context(self, ctx):
self.ctx = ctx
self.value.set_context(ctx)
class __extend__(ast.IfExp):
_description = "conditional expression"
class __extend__(ast.Const):
constant = True
def as_node_list(self, space):
try:
values_w = space.unpackiterable(self.obj)
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
class __extend__(ast.Ellipsis):
_description = "Ellipsis"
constant = True
class __extend__(ast.NameConstant):
_description = "name constant"
constant = True
|