| 1 |
""" |
| 2 |
Implementation of a part of the standard Python opcodes. |
| 3 |
|
| 4 |
The rest, dealing with variables in optimized ways, is in nestedscope.py. |
| 5 |
""" |
| 6 |
import sys |
| 7 |
|
| 8 |
from rpython.rlib import jit, rstackovf, rstring |
| 9 |
from rpython.rlib.debug import check_nonneg |
| 10 |
from rpython.rlib.objectmodel import ( |
| 11 |
we_are_translated, always_inline, dont_inline, not_rpython) |
| 12 |
from rpython.rlib.rarithmetic import r_uint, intmask |
| 13 |
from rpython.tool.sourcetools import func_with_new_name |
| 14 |
|
| 15 |
from pypy.interpreter import ( |
| 16 |
gateway, function, eval, pyframe, pytraceback, pycode |
| 17 |
) |
| 18 |
from pypy.interpreter.baseobjspace import W_Root |
| 19 |
from pypy.interpreter.error import OperationError, oefmt, oefmt_name_error, raise_import_error |
| 20 |
from pypy.interpreter.nestedscope import Cell |
| 21 |
from pypy.interpreter.pycode import PyCode, BytecodeCorruption |
| 22 |
from pypy.tool.stdlib_opcode import bytecode_spec |
| 23 |
|
| 24 |
CANNOT_CATCH_MSG = ("catching classes that do not inherit from BaseException " |
| 25 |
"is not allowed") |
| 26 |
|
| 27 |
@not_rpython |
| 28 |
def unaryoperation(operationname): |
| 29 |
def opimpl(self, *ignored): |
| 30 |
operation = getattr(self.space, operationname) |
| 31 |
w_1 = self.popvalue() |
| 32 |
w_result = operation(w_1) |
| 33 |
self.pushvalue(w_result) |
| 34 |
opimpl.unaryop = operationname |
| 35 |
|
| 36 |
return func_with_new_name(opimpl, "opcode_impl_for_%s" % operationname) |
| 37 |
|
| 38 |
@not_rpython |
| 39 |
def binaryoperation(operationname): |
| 40 |
def opimpl(self, *ignored): |
| 41 |
operation = getattr(self.space, operationname) |
| 42 |
w_2 = self.popvalue() |
| 43 |
w_1 = self.popvalue() |
| 44 |
w_result = operation(w_1, w_2) |
| 45 |
self.pushvalue(w_result) |
| 46 |
opimpl.binop = operationname |
| 47 |
|
| 48 |
return func_with_new_name(opimpl, "opcode_impl_for_%s" % operationname) |
| 49 |
|
| 50 |
|
| 51 |
opcodedesc = bytecode_spec.opcodedesc |
| 52 |
HAVE_ARGUMENT = bytecode_spec.HAVE_ARGUMENT |
| 53 |
|
| 54 |
class __extend__(pyframe.PyFrame): |
| 55 |
"""A PyFrame that knows about interpretation of standard Python opcodes |
| 56 |
minus the ones related to nested scopes.""" |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
def dispatch(self, pycode, next_instr, ec): |
| 61 |
|
| 62 |
next_instr = r_uint(next_instr) |
| 63 |
co_code = pycode.co_code |
| 64 |
try: |
| 65 |
while True: |
| 66 |
assert next_instr & 1 == 0 |
| 67 |
next_instr = self.handle_bytecode(co_code, next_instr, ec) |
| 68 |
except ExitFrame: |
| 69 |
return self.popvalue() |
| 70 |
|
| 71 |
def handle_bytecode(self, co_code, next_instr, ec): |
| 72 |
try: |
| 73 |
next_instr = self.dispatch_bytecode(co_code, next_instr, ec) |
| 74 |
except OperationError as operr: |
| 75 |
try: |
| 76 |
operr.record_context(self.space, ec) |
| 77 |
except OperationError as operr2: |
| 78 |
|
| 79 |
|
| 80 |
w_value = operr.normalize_exception(self.space) |
| 81 |
w_value2 = operr2.normalize_exception(self.space) |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
operr = oefmt( |
| 86 |
self.space.w_TypeError, |
| 87 |
"couldn't record exception context for exception '%T', got: %R", |
| 88 |
w_value, |
| 89 |
w_value2) |
| 90 |
next_instr = self.handle_operation_error(ec, operr) |
| 91 |
except RaiseWithExplicitTraceback as e: |
| 92 |
next_instr = self.handle_operation_error(ec, e.operr, |
| 93 |
attach_tb=False, |
| 94 |
reraise_lasti=e.lasti) |
| 95 |
except KeyboardInterrupt: |
| 96 |
next_instr = self.handle_asynchronous_error(ec, |
| 97 |
self.space.w_KeyboardInterrupt) |
| 98 |
except MemoryError: |
| 99 |
next_instr = self.handle_asynchronous_error(ec, |
| 100 |
self.space.w_MemoryError) |
| 101 |
except rstackovf.StackOverflow as e: |
| 102 |
|
| 103 |
rstackovf.check_stack_overflow() |
| 104 |
next_instr = self.handle_asynchronous_error(ec, |
| 105 |
self.space.w_RecursionError, |
| 106 |
self.space.newtext("maximum recursion depth exceeded")) |
| 107 |
return next_instr |
| 108 |
|
| 109 |
def handle_asynchronous_error(self, ec, w_type, w_value=None): |
| 110 |
|
| 111 |
|
| 112 |
if w_value is None: |
| 113 |
w_value = self.space.w_None |
| 114 |
operr = OperationError(w_type, w_value) |
| 115 |
return self.handle_operation_error(ec, operr) |
| 116 |
|
| 117 |
def handle_generator_error(self, operr): |
| 118 |
|
| 119 |
ec = self.space.getexecutioncontext() |
| 120 |
return self.handle_operation_error(ec, operr) |
| 121 |
|
| 122 |
def handle_operation_error(self, ec, operr, attach_tb=True, reraise_lasti=-1): |
| 123 |
if attach_tb: |
| 124 |
if 1: |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
try: |
| 137 |
trace = self.get_w_f_trace() |
| 138 |
if trace is not None: |
| 139 |
self.getorcreatedebug().w_f_trace = None |
| 140 |
try: |
| 141 |
ec.bytecode_trace_after_exception(self) |
| 142 |
finally: |
| 143 |
if trace is not None: |
| 144 |
self.getorcreatedebug().w_f_trace = trace |
| 145 |
except OperationError as e: |
| 146 |
operr = e |
| 147 |
pytraceback.record_application_traceback( |
| 148 |
self.space, operr, self, self.last_instr) |
| 149 |
ec.exception_trace(self, operr) |
| 150 |
|
| 151 |
entry = self.getcode().lookup_exceptiontable(self.last_instr) |
| 152 |
target, depth, lasti = entry |
| 153 |
if depth >= 0: |
| 154 |
|
| 155 |
target_abs_depth = self._stack_start() + depth |
| 156 |
self.dropvaluesuntil(target_abs_depth) |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
if lasti: |
| 166 |
if reraise_lasti >= 0: |
| 167 |
lasti_value = reraise_lasti |
| 168 |
else: |
| 169 |
lasti_value = intmask(self.last_instr) |
| 170 |
self.pushvalue(self.space.newint(lasti_value)) |
| 171 |
w_exc = operr.normalize_exception(self.space) |
| 172 |
self.pushvalue(w_exc) |
| 173 |
return target |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
if reraise_lasti >= 0: |
| 182 |
self.last_instr = reraise_lasti |
| 183 |
self.frame_finished_execution = True |
| 184 |
if we_are_translated(): |
| 185 |
raise operr |
| 186 |
else: |
| 187 |
|
| 188 |
import sys |
| 189 |
tb = sys.exc_info()[2] |
| 190 |
raise OperationError, operr, tb |
| 191 |
|
| 192 |
def call_contextmanager_exit_function(self, w_func, w_typ, w_val, w_tb): |
| 193 |
return self.space.call_function(w_func, w_typ, w_val, w_tb) |
| 194 |
|
| 195 |
@jit.unroll_safe |
| 196 |
def dispatch_bytecode(self, co_code, next_instr, ec): |
| 197 |
while True: |
| 198 |
assert next_instr & 1 == 0 |
| 199 |
self.last_instr = intmask(next_instr) |
| 200 |
if jit.we_are_jitted(): |
| 201 |
ec.bytecode_only_trace(self) |
| 202 |
else: |
| 203 |
ec.bytecode_trace(self) |
| 204 |
next_instr = r_uint(self.last_instr) |
| 205 |
assert next_instr & 1 == 0 |
| 206 |
opcode = ord(co_code[next_instr]) |
| 207 |
oparg = ord(co_code[next_instr + 1]) |
| 208 |
next_instr += 2 |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
while opcode == opcodedesc.EXTENDED_ARG.index: |
| 215 |
opcode = ord(co_code[next_instr]) |
| 216 |
arg = ord(co_code[next_instr + 1]) |
| 217 |
if opcode < HAVE_ARGUMENT: |
| 218 |
raise BytecodeCorruption |
| 219 |
next_instr += 2 |
| 220 |
oparg = (oparg * 256) | arg |
| 221 |
|
| 222 |
if opcode == opcodedesc.RETURN_VALUE.index: |
| 223 |
self.frame_finished_execution = True |
| 224 |
raise Return |
| 225 |
elif opcode == opcodedesc.JUMP_ABSOLUTE.index: |
| 226 |
return self.jump_absolute(oparg, next_instr, ec) |
| 227 |
elif opcode == opcodedesc.RERAISE.index: |
| 228 |
return self.RERAISE(oparg, next_instr) |
| 229 |
elif opcode == opcodedesc.FOR_ITER.index: |
| 230 |
next_instr = self.FOR_ITER(oparg, next_instr) |
| 231 |
elif opcode == opcodedesc.JUMP_FORWARD.index: |
| 232 |
next_instr = self.JUMP_FORWARD(oparg, next_instr) |
| 233 |
elif opcode == opcodedesc.JUMP_IF_FALSE_OR_POP.index: |
| 234 |
return self.JUMP_IF_FALSE_OR_POP(oparg, next_instr, ec) |
| 235 |
elif opcode == opcodedesc.JUMP_IF_TRUE_OR_POP.index: |
| 236 |
return self.JUMP_IF_TRUE_OR_POP(oparg, next_instr, ec) |
| 237 |
elif opcode == opcodedesc.POP_JUMP_IF_FALSE.index: |
| 238 |
return self.POP_JUMP_IF_FALSE(oparg, next_instr, ec) |
| 239 |
elif opcode == opcodedesc.POP_JUMP_IF_TRUE.index: |
| 240 |
return self.POP_JUMP_IF_TRUE(oparg, next_instr, ec) |
| 241 |
elif opcode == opcodedesc.POP_JUMP_FORWARD_IF_NONE.index: |
| 242 |
next_instr = self.POP_JUMP_FORWARD_IF_NONE(oparg, next_instr) |
| 243 |
elif opcode == opcodedesc.POP_JUMP_FORWARD_IF_NOT_NONE.index: |
| 244 |
next_instr = self.POP_JUMP_FORWARD_IF_NOT_NONE(oparg, next_instr) |
| 245 |
elif opcode == opcodedesc.JUMP_IF_NOT_EXC_MATCH.index: |
| 246 |
next_instr = self.JUMP_IF_NOT_EXC_MATCH(oparg, next_instr) |
| 247 |
elif opcode == opcodedesc.BINARY_ADD.index: |
| 248 |
self.BINARY_ADD(oparg, next_instr) |
| 249 |
elif opcode == opcodedesc.BINARY_AND.index: |
| 250 |
self.BINARY_AND(oparg, next_instr) |
| 251 |
elif opcode == opcodedesc.BINARY_FLOOR_DIVIDE.index: |
| 252 |
self.BINARY_FLOOR_DIVIDE(oparg, next_instr) |
| 253 |
elif opcode == opcodedesc.BINARY_MATRIX_MULTIPLY.index: |
| 254 |
self.BINARY_MATRIX_MULTIPLY(oparg, next_instr) |
| 255 |
elif opcode == opcodedesc.BINARY_LSHIFT.index: |
| 256 |
self.BINARY_LSHIFT(oparg, next_instr) |
| 257 |
elif opcode == opcodedesc.BINARY_MODULO.index: |
| 258 |
self.BINARY_MODULO(oparg, next_instr) |
| 259 |
elif opcode == opcodedesc.BINARY_MULTIPLY.index: |
| 260 |
self.BINARY_MULTIPLY(oparg, next_instr) |
| 261 |
elif opcode == opcodedesc.BINARY_OR.index: |
| 262 |
self.BINARY_OR(oparg, next_instr) |
| 263 |
elif opcode == opcodedesc.BINARY_POWER.index: |
| 264 |
self.BINARY_POWER(oparg, next_instr) |
| 265 |
elif opcode == opcodedesc.BINARY_RSHIFT.index: |
| 266 |
self.BINARY_RSHIFT(oparg, next_instr) |
| 267 |
elif opcode == opcodedesc.BINARY_SUBSCR.index: |
| 268 |
self.BINARY_SUBSCR(oparg, next_instr) |
| 269 |
elif opcode == opcodedesc.BINARY_SUBTRACT.index: |
| 270 |
self.BINARY_SUBTRACT(oparg, next_instr) |
| 271 |
elif opcode == opcodedesc.BINARY_TRUE_DIVIDE.index: |
| 272 |
self.BINARY_TRUE_DIVIDE(oparg, next_instr) |
| 273 |
elif opcode == opcodedesc.BINARY_XOR.index: |
| 274 |
self.BINARY_XOR(oparg, next_instr) |
| 275 |
elif opcode == opcodedesc.BUILD_CONST_KEY_MAP.index: |
| 276 |
self.BUILD_CONST_KEY_MAP(oparg, next_instr) |
| 277 |
elif opcode == opcodedesc.BUILD_LIST.index: |
| 278 |
self.BUILD_LIST(oparg, next_instr) |
| 279 |
elif opcode == opcodedesc.BUILD_LIST_FROM_ARG.index: |
| 280 |
self.BUILD_LIST_FROM_ARG(oparg, next_instr) |
| 281 |
elif opcode == opcodedesc.BUILD_MAP.index: |
| 282 |
self.BUILD_MAP(oparg, next_instr) |
| 283 |
elif opcode == opcodedesc.BUILD_SET.index: |
| 284 |
self.BUILD_SET(oparg, next_instr) |
| 285 |
elif opcode == opcodedesc.BUILD_SLICE.index: |
| 286 |
self.BUILD_SLICE(oparg, next_instr) |
| 287 |
elif opcode == opcodedesc.BUILD_TUPLE.index: |
| 288 |
self.BUILD_TUPLE(oparg, next_instr) |
| 289 |
elif opcode == opcodedesc.CALL_FUNCTION.index: |
| 290 |
self.CALL_FUNCTION(oparg, next_instr) |
| 291 |
elif opcode == opcodedesc.CALL_FUNCTION_KW.index: |
| 292 |
self.CALL_FUNCTION_KW(oparg, next_instr) |
| 293 |
elif opcode == opcodedesc.CALL_FUNCTION_EX.index: |
| 294 |
self.CALL_FUNCTION_EX(oparg, next_instr) |
| 295 |
elif opcode == opcodedesc.CALL_METHOD.index: |
| 296 |
self.CALL_METHOD(oparg, next_instr) |
| 297 |
elif opcode == opcodedesc.CALL_METHOD_KW.index: |
| 298 |
self.CALL_METHOD_KW(oparg, next_instr) |
| 299 |
elif opcode == opcodedesc.CHECK_EG_MATCH.index: |
| 300 |
self.CHECK_EG_MATCH(oparg, next_instr) |
| 301 |
elif opcode == opcodedesc.CHECK_EXC_MATCH.index: |
| 302 |
self.CHECK_EXC_MATCH(oparg, next_instr) |
| 303 |
elif opcode == opcodedesc.COMPARE_OP.index: |
| 304 |
self.COMPARE_OP(oparg, next_instr) |
| 305 |
elif opcode == opcodedesc.IS_OP.index: |
| 306 |
self.IS_OP(oparg, next_instr) |
| 307 |
elif opcode == opcodedesc.CONTAINS_OP.index: |
| 308 |
self.CONTAINS_OP(oparg, next_instr) |
| 309 |
elif opcode == opcodedesc.DELETE_ATTR.index: |
| 310 |
self.DELETE_ATTR(oparg, next_instr) |
| 311 |
elif opcode == opcodedesc.DELETE_DEREF.index: |
| 312 |
self.DELETE_DEREF(oparg, next_instr) |
| 313 |
elif opcode == opcodedesc.DELETE_FAST.index: |
| 314 |
self.DELETE_FAST(oparg, next_instr) |
| 315 |
elif opcode == opcodedesc.SETUP_ANNOTATIONS.index: |
| 316 |
self.SETUP_ANNOTATIONS(oparg, next_instr) |
| 317 |
elif opcode == opcodedesc.DELETE_GLOBAL.index: |
| 318 |
self.DELETE_GLOBAL(oparg, next_instr) |
| 319 |
elif opcode == opcodedesc.DELETE_NAME.index: |
| 320 |
self.DELETE_NAME(oparg, next_instr) |
| 321 |
elif opcode == opcodedesc.DELETE_SUBSCR.index: |
| 322 |
self.DELETE_SUBSCR(oparg, next_instr) |
| 323 |
elif opcode == opcodedesc.DUP_TOP.index: |
| 324 |
self.DUP_TOP(oparg, next_instr) |
| 325 |
elif opcode == opcodedesc.DUP_TOP_TWO.index: |
| 326 |
self.DUP_TOP_TWO(oparg, next_instr) |
| 327 |
elif opcode == opcodedesc.GET_ITER.index: |
| 328 |
self.GET_ITER(oparg, next_instr) |
| 329 |
elif opcode == opcodedesc.IMPORT_FROM.index: |
| 330 |
self.IMPORT_FROM(oparg, next_instr) |
| 331 |
elif opcode == opcodedesc.IMPORT_NAME.index: |
| 332 |
self.IMPORT_NAME(oparg, next_instr) |
| 333 |
elif opcode == opcodedesc.IMPORT_STAR.index: |
| 334 |
self.IMPORT_STAR(oparg, next_instr) |
| 335 |
elif opcode == opcodedesc.INPLACE_ADD.index: |
| 336 |
self.INPLACE_ADD(oparg, next_instr) |
| 337 |
elif opcode == opcodedesc.INPLACE_AND.index: |
| 338 |
self.INPLACE_AND(oparg, next_instr) |
| 339 |
elif opcode == opcodedesc.INPLACE_FLOOR_DIVIDE.index: |
| 340 |
self.INPLACE_FLOOR_DIVIDE(oparg, next_instr) |
| 341 |
elif opcode == opcodedesc.INPLACE_LSHIFT.index: |
| 342 |
self.INPLACE_LSHIFT(oparg, next_instr) |
| 343 |
elif opcode == opcodedesc.INPLACE_MATRIX_MULTIPLY.index: |
| 344 |
self.INPLACE_MATRIX_MULTIPLY(oparg, next_instr) |
| 345 |
elif opcode == opcodedesc.INPLACE_MODULO.index: |
| 346 |
self.INPLACE_MODULO(oparg, next_instr) |
| 347 |
elif opcode == opcodedesc.INPLACE_MULTIPLY.index: |
| 348 |
self.INPLACE_MULTIPLY(oparg, next_instr) |
| 349 |
elif opcode == opcodedesc.INPLACE_OR.index: |
| 350 |
self.INPLACE_OR(oparg, next_instr) |
| 351 |
elif opcode == opcodedesc.INPLACE_POWER.index: |
| 352 |
self.INPLACE_POWER(oparg, next_instr) |
| 353 |
elif opcode == opcodedesc.INPLACE_RSHIFT.index: |
| 354 |
self.INPLACE_RSHIFT(oparg, next_instr) |
| 355 |
elif opcode == opcodedesc.INPLACE_SUBTRACT.index: |
| 356 |
self.INPLACE_SUBTRACT(oparg, next_instr) |
| 357 |
elif opcode == opcodedesc.INPLACE_TRUE_DIVIDE.index: |
| 358 |
self.INPLACE_TRUE_DIVIDE(oparg, next_instr) |
| 359 |
elif opcode == opcodedesc.INPLACE_XOR.index: |
| 360 |
self.INPLACE_XOR(oparg, next_instr) |
| 361 |
elif opcode == opcodedesc.LIST_APPEND.index: |
| 362 |
self.LIST_APPEND(oparg, next_instr) |
| 363 |
elif opcode == opcodedesc.LIST_EXTEND.index: |
| 364 |
self.LIST_EXTEND(oparg, next_instr) |
| 365 |
elif opcode == opcodedesc.LIST_TO_TUPLE.index: |
| 366 |
self.LIST_TO_TUPLE(oparg, next_instr) |
| 367 |
elif opcode == opcodedesc.LOAD_ATTR.index: |
| 368 |
self.LOAD_ATTR(oparg, next_instr) |
| 369 |
elif opcode == opcodedesc.LOAD_BUILD_CLASS.index: |
| 370 |
self.LOAD_BUILD_CLASS(oparg, next_instr) |
| 371 |
elif opcode == opcodedesc.LOAD_CLOSURE.index: |
| 372 |
self.LOAD_CLOSURE(oparg, next_instr) |
| 373 |
elif opcode == opcodedesc.LOAD_CONST.index: |
| 374 |
self.LOAD_CONST(oparg, next_instr) |
| 375 |
elif opcode == opcodedesc.LOAD_DEREF.index: |
| 376 |
self.LOAD_DEREF(oparg, next_instr) |
| 377 |
elif opcode == opcodedesc.LOAD_CLASSDEREF.index: |
| 378 |
self.LOAD_CLASSDEREF(oparg, next_instr) |
| 379 |
elif opcode == opcodedesc.LOAD_FAST.index: |
| 380 |
self.LOAD_FAST(oparg, next_instr) |
| 381 |
elif opcode == opcodedesc.LOAD_GLOBAL.index: |
| 382 |
self.LOAD_GLOBAL(oparg, next_instr) |
| 383 |
elif opcode == opcodedesc.LOAD_NAME.index: |
| 384 |
self.LOAD_NAME(oparg, next_instr) |
| 385 |
elif opcode == opcodedesc.LOAD_METHOD.index: |
| 386 |
self.LOAD_METHOD(oparg, next_instr) |
| 387 |
elif opcode == opcodedesc.MAKE_FUNCTION.index: |
| 388 |
self.MAKE_FUNCTION(oparg, next_instr) |
| 389 |
elif opcode == opcodedesc.MAP_ADD.index: |
| 390 |
self.MAP_ADD(oparg, next_instr) |
| 391 |
elif opcode == opcodedesc.DICT_MERGE.index: |
| 392 |
self.DICT_MERGE(oparg, next_instr) |
| 393 |
elif opcode == opcodedesc.DICT_UPDATE.index: |
| 394 |
self.DICT_UPDATE(oparg, next_instr) |
| 395 |
elif opcode == opcodedesc.NOP.index: |
| 396 |
self.NOP(oparg, next_instr) |
| 397 |
elif opcode == opcodedesc.POP_EXCEPT.index: |
| 398 |
self.POP_EXCEPT(oparg, next_instr) |
| 399 |
elif opcode == opcodedesc.POP_TOP.index: |
| 400 |
self.POP_TOP(oparg, next_instr) |
| 401 |
elif opcode == opcodedesc.PREP_RERAISE_STAR.index: |
| 402 |
self.PREP_RERAISE_STAR(oparg, next_instr) |
| 403 |
elif opcode == opcodedesc.PRINT_EXPR.index: |
| 404 |
self.PRINT_EXPR(oparg, next_instr) |
| 405 |
elif opcode == opcodedesc.PUSH_EXC_INFO.index: |
| 406 |
self.PUSH_EXC_INFO(oparg, next_instr) |
| 407 |
elif opcode == opcodedesc.RAISE_VARARGS.index: |
| 408 |
self.RAISE_VARARGS(oparg, next_instr) |
| 409 |
elif opcode == opcodedesc.ROT_FOUR.index: |
| 410 |
self.ROT_FOUR(oparg, next_instr) |
| 411 |
elif opcode == opcodedesc.ROT_THREE.index: |
| 412 |
self.ROT_THREE(oparg, next_instr) |
| 413 |
elif opcode == opcodedesc.ROT_TWO.index: |
| 414 |
self.ROT_TWO(oparg, next_instr) |
| 415 |
elif opcode == opcodedesc.BEFORE_WITH.index: |
| 416 |
self.BEFORE_WITH(oparg, next_instr) |
| 417 |
elif opcode == opcodedesc.SET_ADD.index: |
| 418 |
self.SET_ADD(oparg, next_instr) |
| 419 |
elif opcode == opcodedesc.SET_UPDATE.index: |
| 420 |
self.SET_UPDATE(oparg, next_instr) |
| 421 |
elif opcode == opcodedesc.STORE_ATTR.index: |
| 422 |
self.STORE_ATTR(oparg, next_instr) |
| 423 |
elif opcode == opcodedesc.STORE_DEREF.index: |
| 424 |
self.STORE_DEREF(oparg, next_instr) |
| 425 |
elif opcode == opcodedesc.STORE_FAST.index: |
| 426 |
self.STORE_FAST(oparg, next_instr) |
| 427 |
elif opcode == opcodedesc.STORE_GLOBAL.index: |
| 428 |
self.STORE_GLOBAL(oparg, next_instr) |
| 429 |
elif opcode == opcodedesc.STORE_NAME.index: |
| 430 |
self.STORE_NAME(oparg, next_instr) |
| 431 |
elif opcode == opcodedesc.STORE_SUBSCR.index: |
| 432 |
self.STORE_SUBSCR(oparg, next_instr) |
| 433 |
elif opcode == opcodedesc.UNARY_INVERT.index: |
| 434 |
self.UNARY_INVERT(oparg, next_instr) |
| 435 |
elif opcode == opcodedesc.UNARY_NEGATIVE.index: |
| 436 |
self.UNARY_NEGATIVE(oparg, next_instr) |
| 437 |
elif opcode == opcodedesc.UNARY_NOT.index: |
| 438 |
self.UNARY_NOT(oparg, next_instr) |
| 439 |
elif opcode == opcodedesc.UNARY_POSITIVE.index: |
| 440 |
self.UNARY_POSITIVE(oparg, next_instr) |
| 441 |
elif opcode == opcodedesc.UNPACK_EX.index: |
| 442 |
self.UNPACK_EX(oparg, next_instr) |
| 443 |
elif opcode == opcodedesc.UNPACK_SEQUENCE.index: |
| 444 |
self.UNPACK_SEQUENCE(oparg, next_instr) |
| 445 |
elif opcode == opcodedesc.WITH_EXCEPT_START.index: |
| 446 |
self.WITH_EXCEPT_START(oparg, next_instr) |
| 447 |
elif opcode == opcodedesc.YIELD_VALUE.index: |
| 448 |
self.YIELD_VALUE(oparg, next_instr) |
| 449 |
elif opcode == opcodedesc.YIELD_FROM.index: |
| 450 |
self.YIELD_FROM(oparg, next_instr) |
| 451 |
elif opcode == opcodedesc.GET_YIELD_FROM_ITER.index: |
| 452 |
self.GET_YIELD_FROM_ITER(oparg, next_instr) |
| 453 |
elif opcode == opcodedesc.LOAD_ASSERTION_ERROR.index: |
| 454 |
self.LOAD_ASSERTION_ERROR(oparg, next_instr) |
| 455 |
elif opcode == opcodedesc.GET_AWAITABLE.index: |
| 456 |
self.GET_AWAITABLE(oparg, next_instr) |
| 457 |
elif opcode == opcodedesc.BEFORE_ASYNC_WITH.index: |
| 458 |
self.BEFORE_ASYNC_WITH(oparg, next_instr) |
| 459 |
elif opcode == opcodedesc.GET_AITER.index: |
| 460 |
self.GET_AITER(oparg, next_instr) |
| 461 |
elif opcode == opcodedesc.GET_ANEXT.index: |
| 462 |
self.GET_ANEXT(oparg, next_instr) |
| 463 |
elif opcode == opcodedesc.END_ASYNC_FOR.index: |
| 464 |
next_instr = self.END_ASYNC_FOR(oparg, next_instr) |
| 465 |
elif opcode == opcodedesc.FORMAT_VALUE.index: |
| 466 |
self.FORMAT_VALUE(oparg, next_instr) |
| 467 |
elif opcode == opcodedesc.BUILD_STRING.index: |
| 468 |
self.BUILD_STRING(oparg, next_instr) |
| 469 |
elif opcode == opcodedesc.LOAD_REVDB_VAR.index: |
| 470 |
self.LOAD_REVDB_VAR(oparg, next_instr) |
| 471 |
elif opcode == opcodedesc.GET_LEN.index: |
| 472 |
self.GET_LEN(oparg, next_instr) |
| 473 |
elif opcode == opcodedesc.MATCH_MAPPING.index: |
| 474 |
self.MATCH_MAPPING(oparg, next_instr) |
| 475 |
elif opcode == opcodedesc.MATCH_SEQUENCE.index: |
| 476 |
self.MATCH_SEQUENCE(oparg, next_instr) |
| 477 |
elif opcode == opcodedesc.MATCH_KEYS.index: |
| 478 |
self.MATCH_KEYS(oparg, next_instr) |
| 479 |
elif opcode == opcodedesc.MATCH_CLASS.index: |
| 480 |
self.MATCH_CLASS(oparg, next_instr) |
| 481 |
elif opcode == opcodedesc.COPY_DICT_WITHOUT_KEYS.index: |
| 482 |
self.COPY_DICT_WITHOUT_KEYS(oparg, next_instr) |
| 483 |
elif opcode == opcodedesc.COPY.index: |
| 484 |
self.COPY(oparg, next_instr) |
| 485 |
elif opcode == opcodedesc.SWAP.index: |
| 486 |
self.SWAP(oparg, next_instr) |
| 487 |
else: |
| 488 |
self.MISSING_OPCODE(oparg, next_instr) |
| 489 |
|
| 490 |
if jit.we_are_jitted(): |
| 491 |
return next_instr |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
def getlocalvarname(self, index): |
| 496 |
return self.getcode().co_varnames[index] |
| 497 |
|
| 498 |
def getconstant_w(self, index): |
| 499 |
return self.getcode().co_consts_w[index] |
| 500 |
|
| 501 |
def getname_u(self, index): |
| 502 |
return self.space.text_w(self.getname_w(index)) |
| 503 |
|
| 504 |
def getname_w(self, index): |
| 505 |
return self.getcode().co_names_w[index] |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
def NOP(self, oparg, next_instr): |
| 514 |
|
| 515 |
|
| 516 |
check_nonneg(oparg) |
| 517 |
|
| 518 |
@always_inline |
| 519 |
def LOAD_FAST(self, varindex, next_instr): |
| 520 |
|
| 521 |
w_value = self.locals_cells_stack_w[varindex] |
| 522 |
if w_value is None: |
| 523 |
self._load_fast_failed(varindex) |
| 524 |
self.pushvalue(w_value) |
| 525 |
|
| 526 |
@dont_inline |
| 527 |
def _load_fast_failed(self, varindex): |
| 528 |
varname = self.getlocalvarname(varindex) |
| 529 |
raise oefmt(self.space.w_UnboundLocalError, |
| 530 |
"local variable '%s' referenced before assignment", |
| 531 |
varname) |
| 532 |
|
| 533 |
def LOAD_CONST(self, constindex, next_instr): |
| 534 |
w_const = self.getconstant_w(constindex) |
| 535 |
self.pushvalue(w_const) |
| 536 |
|
| 537 |
def STORE_FAST(self, varindex, next_instr): |
| 538 |
w_newvalue = self.popvalue() |
| 539 |
assert w_newvalue is not None |
| 540 |
self.locals_cells_stack_w[varindex] = w_newvalue |
| 541 |
|
| 542 |
def getfreevarname(self, index): |
| 543 |
pycode = self.pycode |
| 544 |
if self.iscellvar(index): |
| 545 |
return pycode.co_cellvars[index] |
| 546 |
return pycode.co_freevars[index - len(pycode.co_cellvars)] |
| 547 |
|
| 548 |
def iscellvar(self, index): |
| 549 |
|
| 550 |
return index < len(self.pycode.co_cellvars) |
| 551 |
|
| 552 |
def LOAD_DEREF(self, varindex, next_instr): |
| 553 |
|
| 554 |
cell = self._getcell(varindex) |
| 555 |
try: |
| 556 |
w_value = cell.get() |
| 557 |
except ValueError: |
| 558 |
self.raise_exc_unbound(varindex) |
| 559 |
else: |
| 560 |
self.pushvalue(w_value) |
| 561 |
|
| 562 |
def LOAD_CLASSDEREF(self, varindex, next_instr): |
| 563 |
|
| 564 |
space = self.space |
| 565 |
i = varindex - len(self.pycode.co_cellvars) |
| 566 |
assert i >= 0 |
| 567 |
name = self.pycode.co_freevars[i] |
| 568 |
w_value = space.finditem(self.debugdata.w_locals, space.newtext(name)) |
| 569 |
if w_value is None: |
| 570 |
self.LOAD_DEREF(varindex, next_instr) |
| 571 |
else: |
| 572 |
self.pushvalue(w_value) |
| 573 |
|
| 574 |
def STORE_DEREF(self, varindex, next_instr): |
| 575 |
|
| 576 |
w_newvalue = self.popvalue() |
| 577 |
cell = self._getcell(varindex) |
| 578 |
cell.set(w_newvalue) |
| 579 |
|
| 580 |
def DELETE_DEREF(self, varindex, next_instr): |
| 581 |
cell = self._getcell(varindex) |
| 582 |
try: |
| 583 |
cell.get() |
| 584 |
except ValueError: |
| 585 |
self.raise_exc_unbound(varindex) |
| 586 |
else: |
| 587 |
cell.set(None) |
| 588 |
|
| 589 |
def raise_exc_unbound(self, varindex): |
| 590 |
varname = self.getfreevarname(varindex) |
| 591 |
if self.iscellvar(varindex): |
| 592 |
raise oefmt(self.space.w_UnboundLocalError, |
| 593 |
"local variable '%s' referenced before assignment", |
| 594 |
varname) |
| 595 |
else: |
| 596 |
raise oefmt(self.space.w_NameError, |
| 597 |
"free variable '%s' referenced before assignment" |
| 598 |
" in enclosing scope", varname) |
| 599 |
|
| 600 |
def LOAD_CLOSURE(self, varindex, next_instr): |
| 601 |
|
| 602 |
w_value = self._getcell(varindex) |
| 603 |
self.pushvalue(w_value) |
| 604 |
|
| 605 |
def POP_TOP(self, oparg, next_instr): |
| 606 |
self.popvalue() |
| 607 |
|
| 608 |
def ROT_TWO(self, oparg, next_instr): |
| 609 |
w_1 = self.popvalue() |
| 610 |
w_2 = self.popvalue() |
| 611 |
self.pushvalue(w_1) |
| 612 |
self.pushvalue(w_2) |
| 613 |
|
| 614 |
def ROT_THREE(self, oparg, next_instr): |
| 615 |
w_1 = self.popvalue() |
| 616 |
w_2 = self.popvalue() |
| 617 |
w_3 = self.popvalue() |
| 618 |
self.pushvalue(w_1) |
| 619 |
self.pushvalue(w_3) |
| 620 |
self.pushvalue(w_2) |
| 621 |
|
| 622 |
def ROT_FOUR(self, oparg, next_instr): |
| 623 |
w_1 = self.popvalue() |
| 624 |
w_2 = self.popvalue() |
| 625 |
w_3 = self.popvalue() |
| 626 |
w_4 = self.popvalue() |
| 627 |
self.pushvalue(w_1) |
| 628 |
self.pushvalue(w_4) |
| 629 |
self.pushvalue(w_3) |
| 630 |
self.pushvalue(w_2) |
| 631 |
|
| 632 |
def DUP_TOP(self, oparg, next_instr): |
| 633 |
w_1 = self.peekvalue() |
| 634 |
self.pushvalue(w_1) |
| 635 |
|
| 636 |
def DUP_TOP_TWO(self, oparg, next_instr): |
| 637 |
self.dupvalues(2) |
| 638 |
|
| 639 |
def COPY(self, oparg, next_instr): |
| 640 |
|
| 641 |
|
| 642 |
assert oparg >= 1 |
| 643 |
w_val = self.peekvalue(oparg - 1) |
| 644 |
self.pushvalue(w_val) |
| 645 |
|
| 646 |
def DUP_TOPX(self, itemcount, next_instr): |
| 647 |
assert 1 <= itemcount <= 5, "limitation of the current interpreter" |
| 648 |
self.dupvalues(itemcount) |
| 649 |
|
| 650 |
UNARY_POSITIVE = unaryoperation("pos") |
| 651 |
UNARY_NEGATIVE = unaryoperation("neg") |
| 652 |
UNARY_NOT = unaryoperation("not_") |
| 653 |
UNARY_CONVERT = unaryoperation("repr") |
| 654 |
UNARY_INVERT = unaryoperation("invert") |
| 655 |
|
| 656 |
def BINARY_POWER(self, oparg, next_instr): |
| 657 |
w_2 = self.popvalue() |
| 658 |
w_1 = self.popvalue() |
| 659 |
w_result = self.space.pow(w_1, w_2, self.space.w_None) |
| 660 |
self.pushvalue(w_result) |
| 661 |
|
| 662 |
BINARY_MULTIPLY = binaryoperation("mul") |
| 663 |
BINARY_TRUE_DIVIDE = binaryoperation("truediv") |
| 664 |
BINARY_FLOOR_DIVIDE = binaryoperation("floordiv") |
| 665 |
BINARY_MODULO = binaryoperation("mod") |
| 666 |
BINARY_MATRIX_MULTIPLY = binaryoperation("matmul") |
| 667 |
BINARY_ADD = binaryoperation("add") |
| 668 |
BINARY_SUBTRACT = binaryoperation("sub") |
| 669 |
BINARY_SUBSCR = binaryoperation("getitem") |
| 670 |
BINARY_LSHIFT = binaryoperation("lshift") |
| 671 |
BINARY_RSHIFT = binaryoperation("rshift") |
| 672 |
BINARY_AND = binaryoperation("and_") |
| 673 |
BINARY_XOR = binaryoperation("xor") |
| 674 |
BINARY_OR = binaryoperation("or_") |
| 675 |
|
| 676 |
def INPLACE_POWER(self, oparg, next_instr): |
| 677 |
w_2 = self.popvalue() |
| 678 |
w_1 = self.popvalue() |
| 679 |
w_result = self.space.inplace_pow(w_1, w_2) |
| 680 |
self.pushvalue(w_result) |
| 681 |
|
| 682 |
INPLACE_MULTIPLY = binaryoperation("inplace_mul") |
| 683 |
INPLACE_TRUE_DIVIDE = binaryoperation("inplace_truediv") |
| 684 |
INPLACE_FLOOR_DIVIDE = binaryoperation("inplace_floordiv") |
| 685 |
INPLACE_DIVIDE = binaryoperation("inplace_div") |
| 686 |
|
| 687 |
INPLACE_MODULO = binaryoperation("inplace_mod") |
| 688 |
INPLACE_MATRIX_MULTIPLY = binaryoperation("inplace_matmul") |
| 689 |
INPLACE_ADD = binaryoperation("inplace_add") |
| 690 |
INPLACE_SUBTRACT = binaryoperation("inplace_sub") |
| 691 |
INPLACE_LSHIFT = binaryoperation("inplace_lshift") |
| 692 |
INPLACE_RSHIFT = binaryoperation("inplace_rshift") |
| 693 |
INPLACE_AND = binaryoperation("inplace_and") |
| 694 |
INPLACE_XOR = binaryoperation("inplace_xor") |
| 695 |
INPLACE_OR = binaryoperation("inplace_or") |
| 696 |
|
| 697 |
def STORE_SUBSCR(self, oparg, next_instr): |
| 698 |
"obj[subscr] = newvalue" |
| 699 |
w_subscr = self.popvalue() |
| 700 |
w_obj = self.popvalue() |
| 701 |
w_newvalue = self.popvalue() |
| 702 |
self.space.setitem(w_obj, w_subscr, w_newvalue) |
| 703 |
|
| 704 |
def DELETE_SUBSCR(self, oparg, next_instr): |
| 705 |
"del obj[subscr]" |
| 706 |
w_subscr = self.popvalue() |
| 707 |
w_obj = self.popvalue() |
| 708 |
self.space.delitem(w_obj, w_subscr) |
| 709 |
|
| 710 |
def PRINT_EXPR(self, oparg, next_instr): |
| 711 |
w_expr = self.popvalue() |
| 712 |
print_expr(self.space, w_expr) |
| 713 |
|
| 714 |
def RAISE_VARARGS(self, nbargs, next_instr): |
| 715 |
space = self.space |
| 716 |
if nbargs > 2: |
| 717 |
raise BytecodeCorruption("bad RAISE_VARARGS oparg") |
| 718 |
if nbargs == 0: |
| 719 |
if not self.hide(): |
| 720 |
last_operr = self.space.getexecutioncontext().sys_exc_info() |
| 721 |
else: |
| 722 |
last_operr = self.getorcreatedebug().hidden_operationerr |
| 723 |
if last_operr is None: |
| 724 |
raise oefmt(space.w_RuntimeError, |
| 725 |
"No active exception to reraise") |
| 726 |
|
| 727 |
|
| 728 |
from pypy.module.exceptions.interp_exceptions import W_BaseException |
| 729 |
w_value = last_operr._w_value |
| 730 |
if w_value is not None and isinstance(w_value, W_BaseException): |
| 731 |
w_tb = w_value.w_traceback |
| 732 |
if space.is_none(w_tb): |
| 733 |
last_operr.set_traceback(None) |
| 734 |
else: |
| 735 |
from pypy.interpreter.pytraceback import PyTraceback |
| 736 |
if isinstance(w_tb, PyTraceback): |
| 737 |
last_operr.set_traceback(w_tb) |
| 738 |
|
| 739 |
raise RaiseWithExplicitTraceback(last_operr) |
| 740 |
if nbargs == 2: |
| 741 |
w_cause = self.popvalue() |
| 742 |
if space.exception_is_valid_obj_as_class_w(w_cause): |
| 743 |
w_cause = space.call_function(w_cause) |
| 744 |
else: |
| 745 |
w_cause = None |
| 746 |
w_value = self.popvalue() |
| 747 |
if space.exception_is_valid_obj_as_class_w(w_value): |
| 748 |
w_type = w_value |
| 749 |
w_value = space.call_function(w_type) |
| 750 |
else: |
| 751 |
w_type = space.type(w_value) |
| 752 |
operror = OperationError(w_type, w_value) |
| 753 |
operror.normalize_exception(space) |
| 754 |
operror.set_cause(space, w_cause) |
| 755 |
tb = space.getattr(w_value, space.newtext('__traceback__')) |
| 756 |
if not space.is_w(tb, space.w_None): |
| 757 |
operror.set_traceback(tb) |
| 758 |
raise operror |
| 759 |
|
| 760 |
def LOAD_LOCALS(self, oparg, next_instr): |
| 761 |
self.pushvalue(self.getorcreatedebug().w_locals) |
| 762 |
|
| 763 |
def exec_(self, w_prog, w_globals, w_locals, w_closure=None): |
| 764 |
"""The builtins.exec function.""" |
| 765 |
from pypy.interpreter.function import Function |
| 766 |
space = self.space |
| 767 |
ec = space.getexecutioncontext() |
| 768 |
flags = ec.compiler.getcodeflags(self.pycode) |
| 769 |
outer_func = None |
| 770 |
|
| 771 |
if space.isinstance_w(w_prog, space.gettypeobject(PyCode.typedef)): |
| 772 |
space.audit("exec", [w_prog]) |
| 773 |
code = space.interp_w(PyCode, w_prog) |
| 774 |
if code.co_freevars: |
| 775 |
needed = len(code.co_freevars) |
| 776 |
if (space.is_none(w_closure) or |
| 777 |
not space.isinstance_w(w_closure, space.w_tuple) or |
| 778 |
not space.len_w(w_closure) == needed): |
| 779 |
raise oefmt( |
| 780 |
space.w_TypeError, |
| 781 |
"code object requires a closure of exactly length %d", |
| 782 |
needed) |
| 783 |
closure_w = space.fixedview(w_closure, needed) |
| 784 |
for cell in closure_w: |
| 785 |
if not isinstance(cell, Cell): |
| 786 |
raise oefmt( |
| 787 |
space.w_TypeError, |
| 788 |
"code object requires a closure of exactly length %d", |
| 789 |
needed) |
| 790 |
outer_func = Function(space, code, closure=closure_w) |
| 791 |
else: |
| 792 |
if not space.is_none(w_closure): |
| 793 |
raise oefmt( |
| 794 |
space.w_TypeError, |
| 795 |
"cannot use a closure with this code object") |
| 796 |
else: |
| 797 |
from pypy.interpreter.astcompiler import consts |
| 798 |
if not space.is_none(w_closure): |
| 799 |
raise oefmt( |
| 800 |
space.w_TypeError, |
| 801 |
"closure can only be used when source is a code object") |
| 802 |
flags |= consts.PyCF_SOURCE_IS_UTF8 |
| 803 |
source, flags = source_as_str(space, w_prog, 'exec', |
| 804 |
"string, bytes or code", flags) |
| 805 |
code = ec.compiler.compile(source, "<string>", 'exec', flags) |
| 806 |
|
| 807 |
w_globals, w_locals = ensure_ns(space, w_globals, w_locals, 'exec', |
| 808 |
self) |
| 809 |
space.call_method(w_globals, 'setdefault', space.newtext('__builtins__'), |
| 810 |
self.get_builtin()) |
| 811 |
|
| 812 |
code.exec_code(space, w_globals, w_locals, outer_func) |
| 813 |
|
| 814 |
def POP_EXCEPT(self, oparg, next_instr): |
| 815 |
w_prev_exc = self.popvalue() |
| 816 |
|
| 817 |
self._restore_exc_info(w_prev_exc) |
| 818 |
|
| 819 |
def PUSH_EXC_INFO(self, oparg, next_instr): |
| 820 |
w_exc = self.popvalue() |
| 821 |
ec = self.space.getexecutioncontext() |
| 822 |
|
| 823 |
if not self.hide(): |
| 824 |
prev_operr = ec.current_exception() |
| 825 |
else: |
| 826 |
prev_operr = self.getorcreatedebug().hidden_operationerr |
| 827 |
if prev_operr is not None: |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
w_prev = prev_operr.get_w_value(self.space) |
| 832 |
if w_prev is None: |
| 833 |
w_prev = self.space.w_None |
| 834 |
else: |
| 835 |
w_prev = self.space.w_None |
| 836 |
|
| 837 |
from pypy.module.exceptions.interp_exceptions import W_BaseException |
| 838 |
space = self.space |
| 839 |
w_exc_val = space.interp_w(W_BaseException, w_exc) |
| 840 |
operr = OperationError(space.type(w_exc), w_exc, w_exc_val.w_traceback) |
| 841 |
if not self.hide(): |
| 842 |
ec.set_sys_exc_info(operr) |
| 843 |
else: |
| 844 |
self.getorcreatedebug().hidden_operationerr = operr |
| 845 |
self.pushvalue(w_prev) |
| 846 |
self.pushvalue(w_exc) |
| 847 |
|
| 848 |
def LOAD_BUILD_CLASS(self, oparg, next_instr): |
| 849 |
w_build_class = self.get_builtin().getdictvalue( |
| 850 |
self.space, '__build_class__') |
| 851 |
if w_build_class is None: |
| 852 |
raise oefmt(self.space.w_ImportError, "__build_class__ not found") |
| 853 |
self.pushvalue(w_build_class) |
| 854 |
|
| 855 |
def STORE_NAME(self, varindex, next_instr): |
| 856 |
varname = self.getname_u(varindex) |
| 857 |
w_newvalue = self.popvalue() |
| 858 |
self.space.setitem_str(self.getorcreatedebug().w_locals, varname, |
| 859 |
w_newvalue) |
| 860 |
|
| 861 |
def DELETE_NAME(self, varindex, next_instr): |
| 862 |
w_varname = self.getname_w(varindex) |
| 863 |
try: |
| 864 |
self.space.delitem(self.getorcreatedebug().w_locals, w_varname) |
| 865 |
except OperationError as e: |
| 866 |
|
| 867 |
if not e.match(self.space, self.space.w_KeyError): |
| 868 |
raise |
| 869 |
raise oefmt(self.space.w_NameError, |
| 870 |
"name %R is not defined", w_varname) |
| 871 |
|
| 872 |
def UNPACK_SEQUENCE(self, itemcount, next_instr): |
| 873 |
w_iterable = self.popvalue() |
| 874 |
try: |
| 875 |
items = self.space.fixedview_unroll(w_iterable, itemcount) |
| 876 |
except OperationError as e: |
| 877 |
if not e.match(self.space, self.space.w_TypeError): |
| 878 |
raise |
| 879 |
raise oefmt(self.space.w_TypeError, |
| 880 |
"cannot unpack non-iterable %T object", |
| 881 |
w_iterable) |
| 882 |
self.pushrevvalues(itemcount, items) |
| 883 |
|
| 884 |
@jit.unroll_safe |
| 885 |
def UNPACK_EX(self, oparg, next_instr): |
| 886 |
"a, *b, c = range(10)" |
| 887 |
left = oparg & 0xFF |
| 888 |
right = (oparg & 0xFF00) >> 8 |
| 889 |
w_iterable = self.popvalue() |
| 890 |
try: |
| 891 |
items = self.space.fixedview(w_iterable) |
| 892 |
except OperationError as e: |
| 893 |
if not e.match(self.space, self.space.w_TypeError): |
| 894 |
raise |
| 895 |
raise oefmt(self.space.w_TypeError, |
| 896 |
"cannot unpack non-iterable %T object", |
| 897 |
w_iterable) |
| 898 |
itemcount = len(items) |
| 899 |
count = left + right |
| 900 |
if count > itemcount: |
| 901 |
raise oefmt(self.space.w_ValueError, |
| 902 |
"not enough values to unpack (expected at least %d, got %d)", |
| 903 |
count, itemcount) |
| 904 |
right = itemcount - right |
| 905 |
assert right >= 0 |
| 906 |
|
| 907 |
i = itemcount - 1 |
| 908 |
while i >= right: |
| 909 |
self.pushvalue(items[i]) |
| 910 |
i -= 1 |
| 911 |
self.pushvalue(self.space.newlist(items[left:right])) |
| 912 |
i = left - 1 |
| 913 |
while i >= 0: |
| 914 |
self.pushvalue(items[i]) |
| 915 |
i -= 1 |
| 916 |
|
| 917 |
def STORE_ATTR(self, nameindex, next_instr): |
| 918 |
"obj.attributename = newvalue" |
| 919 |
w_obj = self.popvalue() |
| 920 |
w_newvalue = self.popvalue() |
| 921 |
if not jit.we_are_jitted(): |
| 922 |
from pypy.objspace.std.mapdict import STORE_ATTR_caching |
| 923 |
STORE_ATTR_caching(self.getcode(), w_obj, nameindex, w_newvalue) |
| 924 |
else: |
| 925 |
w_attributename = self.getname_w(nameindex) |
| 926 |
self.space.setattr(w_obj, w_attributename, w_newvalue) |
| 927 |
|
| 928 |
def DELETE_ATTR(self, nameindex, next_instr): |
| 929 |
"del obj.attributename" |
| 930 |
w_attributename = self.getname_w(nameindex) |
| 931 |
w_obj = self.popvalue() |
| 932 |
self.space.delattr(w_obj, w_attributename) |
| 933 |
|
| 934 |
def STORE_GLOBAL(self, nameindex, next_instr): |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
from pypy.objspace.std.celldict import STORE_GLOBAL_cached |
| 939 |
STORE_GLOBAL_cached(self, nameindex, next_instr) |
| 940 |
|
| 941 |
def DELETE_GLOBAL(self, nameindex, next_instr): |
| 942 |
w_varname = self.getname_w(nameindex) |
| 943 |
self.space.delitem(self.get_w_globals(), w_varname) |
| 944 |
|
| 945 |
def LOAD_NAME(self, nameindex, next_instr): |
| 946 |
from pypy.objspace.std.celldict import LOAD_GLOBAL_cached |
| 947 |
if self.getorcreatedebug().w_locals is not self.get_w_globals(): |
| 948 |
w_varname = self.getname_w(nameindex) |
| 949 |
varname = self.space.text_w(w_varname) |
| 950 |
w_value = self.space.finditem_str(self.getorcreatedebug().w_locals, |
| 951 |
varname) |
| 952 |
if w_value is not None: |
| 953 |
self.pushvalue(w_value) |
| 954 |
return |
| 955 |
LOAD_GLOBAL_cached(self, nameindex, next_instr) |
| 956 |
|
| 957 |
@always_inline |
| 958 |
def _load_global(self, varname): |
| 959 |
w_value = self.space.finditem_str(self.get_w_globals(), varname) |
| 960 |
if w_value is None: |
| 961 |
|
| 962 |
w_value = self.get_builtin().getdictvalue(self.space, varname) |
| 963 |
if w_value is None: |
| 964 |
|
| 965 |
w_varname = self.space.newtext(varname) |
| 966 |
self._load_global_failed(w_varname) |
| 967 |
return w_value |
| 968 |
|
| 969 |
@dont_inline |
| 970 |
def _load_global_failed(self, w_varname): |
| 971 |
|
| 972 |
|
| 973 |
raise oefmt_name_error(self.space, w_varname, |
| 974 |
"name %R is not defined") |
| 975 |
|
| 976 |
@always_inline |
| 977 |
def LOAD_GLOBAL(self, nameindex, next_instr): |
| 978 |
from pypy.objspace.std.celldict import LOAD_GLOBAL_cached |
| 979 |
LOAD_GLOBAL_cached(self, nameindex, next_instr) |
| 980 |
|
| 981 |
def DELETE_FAST(self, varindex, next_instr): |
| 982 |
if self.locals_cells_stack_w[varindex] is None: |
| 983 |
varname = self.getlocalvarname(varindex) |
| 984 |
raise oefmt(self.space.w_UnboundLocalError, |
| 985 |
"local variable '%s' referenced before assignment", |
| 986 |
varname) |
| 987 |
self.locals_cells_stack_w[varindex] = None |
| 988 |
|
| 989 |
def SETUP_ANNOTATIONS(self, oparg, next_instr): |
| 990 |
w_locals = self.getorcreatedebug().w_locals |
| 991 |
if not self.space.finditem_str(w_locals, '__annotations__'): |
| 992 |
w_annotations = self.space.newdict() |
| 993 |
self.space.setitem_str(w_locals, '__annotations__', w_annotations) |
| 994 |
|
| 995 |
def BUILD_TUPLE(self, itemcount, next_instr): |
| 996 |
items = self.popvalues(itemcount) |
| 997 |
w_tuple = self.space.newtuple(items) |
| 998 |
self.pushvalue(w_tuple) |
| 999 |
|
| 1000 |
def BUILD_LIST(self, itemcount, next_instr): |
| 1001 |
items = self.popvalues_mutable(itemcount) |
| 1002 |
w_list = self.space.newlist(items) |
| 1003 |
self.pushvalue(w_list) |
| 1004 |
|
| 1005 |
def BUILD_LIST_FROM_ARG(self, _, next_instr): |
| 1006 |
space = self.space |
| 1007 |
|
| 1008 |
|
| 1009 |
last_val = self.popvalue() |
| 1010 |
length_hint = 0 |
| 1011 |
try: |
| 1012 |
length_hint = space.length_hint(last_val, length_hint) |
| 1013 |
except OperationError as e: |
| 1014 |
if e.async(space): |
| 1015 |
raise |
| 1016 |
self.pushvalue(space.newlist([], sizehint=length_hint)) |
| 1017 |
self.pushvalue(last_val) |
| 1018 |
|
| 1019 |
@always_inline |
| 1020 |
def LOAD_ATTR(self, nameindex, next_instr): |
| 1021 |
"obj.attributename" |
| 1022 |
w_obj = self.popvalue() |
| 1023 |
if not jit.we_are_jitted(): |
| 1024 |
from pypy.objspace.std.mapdict import LOAD_ATTR_caching |
| 1025 |
w_value = LOAD_ATTR_caching(self.getcode(), w_obj, nameindex) |
| 1026 |
else: |
| 1027 |
w_attributename = self.getname_w(nameindex) |
| 1028 |
w_value = self.space.getattr(w_obj, w_attributename) |
| 1029 |
self.pushvalue(w_value) |
| 1030 |
|
| 1031 |
@jit.unroll_safe |
| 1032 |
def cmp_exc_match(self, w_1, w_2): |
| 1033 |
space = self.space |
| 1034 |
if space.isinstance_w(w_2, space.w_tuple): |
| 1035 |
for w_type in space.fixedview(w_2): |
| 1036 |
if not space.exception_is_valid_class_w(w_type): |
| 1037 |
raise oefmt(space.w_TypeError, CANNOT_CATCH_MSG) |
| 1038 |
elif not space.exception_is_valid_class_w(w_2): |
| 1039 |
raise oefmt(space.w_TypeError, CANNOT_CATCH_MSG) |
| 1040 |
return space.exception_match(space.type(w_1), w_2) |
| 1041 |
|
| 1042 |
def COMPARE_OP(self, testnum, next_instr): |
| 1043 |
w_2 = self.popvalue() |
| 1044 |
w_1 = self.popvalue() |
| 1045 |
if testnum == 0: |
| 1046 |
w_result = self.space.lt(w_1, w_2) |
| 1047 |
elif testnum == 1: |
| 1048 |
w_result = self.space.le(w_1, w_2) |
| 1049 |
elif testnum == 2: |
| 1050 |
w_result = self.space.eq(w_1, w_2) |
| 1051 |
elif testnum == 3: |
| 1052 |
w_result = self.space.ne(w_1, w_2) |
| 1053 |
elif testnum == 4: |
| 1054 |
w_result = self.space.gt(w_1, w_2) |
| 1055 |
elif testnum == 5: |
| 1056 |
w_result = self.space.ge(w_1, w_2) |
| 1057 |
else: |
| 1058 |
raise BytecodeCorruption |
| 1059 |
self.pushvalue(w_result) |
| 1060 |
|
| 1061 |
def IS_OP(self, oparg, next_instr): |
| 1062 |
space = self.space |
| 1063 |
w_2 = self.popvalue() |
| 1064 |
w_1 = self.popvalue() |
| 1065 |
res = self.space.is_w(w_1, w_2) |
| 1066 |
if oparg: |
| 1067 |
if res: |
| 1068 |
w_res = space.w_False |
| 1069 |
else: |
| 1070 |
w_res = space.w_True |
| 1071 |
else: |
| 1072 |
if res: |
| 1073 |
w_res = space.w_True |
| 1074 |
else: |
| 1075 |
w_res = space.w_False |
| 1076 |
self.pushvalue(w_res) |
| 1077 |
|
| 1078 |
def CONTAINS_OP(self, oparg, next_instr): |
| 1079 |
w_2 = self.popvalue() |
| 1080 |
w_1 = self.popvalue() |
| 1081 |
res = self.space.contains_w(w_2, w_1) ^ oparg |
| 1082 |
self.pushvalue(self.space.newbool(bool(res))) |
| 1083 |
|
| 1084 |
def JUMP_IF_NOT_EXC_MATCH(self, target, next_instr): |
| 1085 |
w_2 = self.popvalue() |
| 1086 |
w_1 = self.popvalue() |
| 1087 |
res = self.cmp_exc_match(w_1, w_2) |
| 1088 |
if res: |
| 1089 |
return next_instr |
| 1090 |
else: |
| 1091 |
return target * 2 |
| 1092 |
|
| 1093 |
def IMPORT_NAME(self, nameindex, next_instr): |
| 1094 |
space = self.space |
| 1095 |
w_modulename = self.getname_w(nameindex) |
| 1096 |
w_fromlist = self.popvalue() |
| 1097 |
w_flag = self.popvalue() |
| 1098 |
|
| 1099 |
w_import = self.get_builtin().getdictvalue(space, '__import__') |
| 1100 |
if w_import is None: |
| 1101 |
raise oefmt(space.w_ImportError, "__import__ not found") |
| 1102 |
d = self.getdebug() |
| 1103 |
if d is None: |
| 1104 |
w_locals = None |
| 1105 |
else: |
| 1106 |
w_locals = d.w_locals |
| 1107 |
if w_locals is None: |
| 1108 |
w_locals = space.w_None |
| 1109 |
w_globals = self.get_w_globals() |
| 1110 |
|
| 1111 |
w_obj = space.call_function(w_import, w_modulename, w_globals, |
| 1112 |
w_locals, w_fromlist, w_flag) |
| 1113 |
|
| 1114 |
self.pushvalue(w_obj) |
| 1115 |
|
| 1116 |
def IMPORT_STAR(self, oparg, next_instr): |
| 1117 |
w_module = self.popvalue() |
| 1118 |
w_locals = self.getdictscope() |
| 1119 |
import_all_from(self.space, w_module, w_locals) |
| 1120 |
self.setdictscope(w_locals) |
| 1121 |
|
| 1122 |
def IMPORT_FROM(self, nameindex, next_instr): |
| 1123 |
w_name = self.getname_w(nameindex) |
| 1124 |
w_module = self.peekvalue() |
| 1125 |
self.pushvalue(self.import_from(w_module, w_name)) |
| 1126 |
|
| 1127 |
def import_from(self, w_module, w_name): |
| 1128 |
space = self.space |
| 1129 |
try: |
| 1130 |
return space.getattr(w_module, w_name) |
| 1131 |
except OperationError as e: |
| 1132 |
if not e.match(space, space.w_AttributeError): |
| 1133 |
raise |
| 1134 |
|
| 1135 |
w_pkgname = space.newtext("<unknown module name>") |
| 1136 |
try: |
| 1137 |
w_pkgname = space.getattr( |
| 1138 |
w_module, space.newtext('__name__')) |
| 1139 |
w_fullname = space.newtext(b'%s.%s' % |
| 1140 |
(space.utf8_w(w_pkgname), space.utf8_w(w_name))) |
| 1141 |
return space.getitem(space.sys.get('modules'), w_fullname) |
| 1142 |
except OperationError: |
| 1143 |
from pypy.module.imp.importing import (is_spec_initializing, |
| 1144 |
get_path, get_spec, is_spec_uninitialized_submodule) |
| 1145 |
|
| 1146 |
w_pkgpath = get_path(space, w_module) |
| 1147 |
args = (space.utf8_w(w_name), space.utf8_w(w_pkgname), space.utf8_w(w_pkgpath)) |
| 1148 |
w_spec = get_spec(space, w_module) |
| 1149 |
if is_spec_initializing(space, w_spec): |
| 1150 |
msg = ( |
| 1151 |
"cannot import name '%s' from partially initialized module '%s' " |
| 1152 |
"(most likely due to a circular import) (%s)" % args |
| 1153 |
) |
| 1154 |
elif is_spec_uninitialized_submodule(space, w_spec, w_pkgname): |
| 1155 |
msg = ( |
| 1156 |
"cannot access submodule '%s' of module '%s' " |
| 1157 |
"(most likely due to a circular import)" % (args[1], args[0]) |
| 1158 |
) |
| 1159 |
else: |
| 1160 |
msg = "cannot import name '%s' from '%s' (%s)" % args |
| 1161 |
|
| 1162 |
raise_import_error( |
| 1163 |
space, |
| 1164 |
space.newtext(msg), |
| 1165 |
w_pkgname, |
| 1166 |
w_pkgpath, |
| 1167 |
) |
| 1168 |
|
| 1169 |
def YIELD_VALUE(self, oparg, next_instr): |
| 1170 |
if self.getcode().co_flags & pycode.CO_ASYNC_GENERATOR: |
| 1171 |
from pypy.interpreter.generator import AsyncGenValueWrapper |
| 1172 |
w_value = self.popvalue() |
| 1173 |
w_value = AsyncGenValueWrapper(w_value) |
| 1174 |
self.pushvalue(w_value) |
| 1175 |
raise Yield |
| 1176 |
|
| 1177 |
def next_yield_from(self, w_yf, w_inputvalue_or_err): |
| 1178 |
"""Fetch the next item of the current 'yield from', push it on |
| 1179 |
the frame stack, and raises Yield. If there isn't one, push |
| 1180 |
w_stopiteration_value and returns. May also just raise. |
| 1181 |
""" |
| 1182 |
from pypy.interpreter.generator import ( |
| 1183 |
GeneratorOrCoroutine, AsyncGenASend) |
| 1184 |
space = self.space |
| 1185 |
try: |
| 1186 |
if isinstance(w_yf, GeneratorOrCoroutine): |
| 1187 |
w_retval = w_yf.send_ex(w_inputvalue_or_err) |
| 1188 |
elif isinstance(w_yf, AsyncGenASend): |
| 1189 |
w_retval = w_yf.do_send(w_inputvalue_or_err) |
| 1190 |
elif space.is_w(w_inputvalue_or_err, space.w_None): |
| 1191 |
w_retval = space.next(w_yf) |
| 1192 |
else: |
| 1193 |
w_retval = delegate_to_nongen(space, w_yf, w_inputvalue_or_err) |
| 1194 |
except OperationError as e: |
| 1195 |
if not e.match(space, space.w_StopIteration): |
| 1196 |
raise |
| 1197 |
self._report_stopiteration_sometimes(w_yf, e) |
| 1198 |
try: |
| 1199 |
w_stop_value = space.getattr(e.get_w_value(space), |
| 1200 |
space.newtext("value")) |
| 1201 |
except OperationError as e: |
| 1202 |
if not e.match(space, space.w_AttributeError): |
| 1203 |
raise |
| 1204 |
w_stop_value = space.w_None |
| 1205 |
self.pushvalue(w_stop_value) |
| 1206 |
return |
| 1207 |
else: |
| 1208 |
self.pushvalue(w_retval) |
| 1209 |
self.w_yielding_from = w_yf |
| 1210 |
raise Yield |
| 1211 |
|
| 1212 |
def YIELD_FROM(self, oparg, next_instr): |
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
w_inputvalue = self.popvalue() |
| 1219 |
w_gen = self.popvalue() |
| 1220 |
|
| 1221 |
self.next_yield_from(w_gen, w_inputvalue) |
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
def _revdb_jump_backward(self, jumpto): |
| 1228 |
|
| 1229 |
from pypy.interpreter.reverse_debugging import jump_backward |
| 1230 |
jump_backward(self, jumpto) |
| 1231 |
|
| 1232 |
def jump_absolute(self, jumpto, next_instr, ec): |
| 1233 |
|
| 1234 |
check_nonneg(jumpto) |
| 1235 |
if self.space.reverse_debugging: |
| 1236 |
self._revdb_jump_backward(jumpto * 2) |
| 1237 |
return jumpto * 2 |
| 1238 |
|
| 1239 |
def JUMP_FORWARD(self, jumpby, next_instr): |
| 1240 |
next_instr += jumpby * 2 |
| 1241 |
return next_instr |
| 1242 |
|
| 1243 |
def POP_JUMP_IF_FALSE(self, target, next_instr, ec): |
| 1244 |
w_value = self.popvalue() |
| 1245 |
if not self.space.is_true(w_value): |
| 1246 |
return self.jump_absolute(target, next_instr, ec) |
| 1247 |
return next_instr |
| 1248 |
|
| 1249 |
def POP_JUMP_IF_TRUE(self, target, next_instr, ec): |
| 1250 |
w_value = self.popvalue() |
| 1251 |
if self.space.is_true(w_value): |
| 1252 |
return self.jump_absolute(target, next_instr, ec) |
| 1253 |
return next_instr |
| 1254 |
|
| 1255 |
def POP_JUMP_FORWARD_IF_NONE(self, jumpby, next_instr): |
| 1256 |
w_value = self.popvalue() |
| 1257 |
if self.space.is_w(w_value, self.space.w_None): |
| 1258 |
next_instr += jumpby * 2 |
| 1259 |
return next_instr |
| 1260 |
|
| 1261 |
def POP_JUMP_FORWARD_IF_NOT_NONE(self, jumpby, next_instr): |
| 1262 |
w_value = self.popvalue() |
| 1263 |
if not self.space.is_w(w_value, self.space.w_None): |
| 1264 |
next_instr += jumpby * 2 |
| 1265 |
return next_instr |
| 1266 |
|
| 1267 |
def JUMP_IF_FALSE_OR_POP(self, target, next_instr, ec): |
| 1268 |
w_value = self.peekvalue() |
| 1269 |
if not self.space.is_true(w_value): |
| 1270 |
return self.jump_absolute(target, next_instr, ec) |
| 1271 |
self.popvalue() |
| 1272 |
return next_instr |
| 1273 |
|
| 1274 |
def JUMP_IF_TRUE_OR_POP(self, target, next_instr, ec): |
| 1275 |
w_value = self.peekvalue() |
| 1276 |
if self.space.is_true(w_value): |
| 1277 |
return self.jump_absolute(target, next_instr, ec) |
| 1278 |
self.popvalue() |
| 1279 |
return next_instr |
| 1280 |
|
| 1281 |
def GET_ITER(self, oparg, next_instr): |
| 1282 |
w_iterable = self.popvalue() |
| 1283 |
w_iterator = self.space.iter(w_iterable) |
| 1284 |
self.pushvalue(w_iterator) |
| 1285 |
|
| 1286 |
def FOR_ITER(self, jumpby, next_instr): |
| 1287 |
w_iterator = self.peekvalue() |
| 1288 |
try: |
| 1289 |
w_nextitem = self.space.next(w_iterator) |
| 1290 |
except OperationError as e: |
| 1291 |
if not e.match(self.space, self.space.w_StopIteration): |
| 1292 |
raise |
| 1293 |
|
| 1294 |
self._report_stopiteration_sometimes(w_iterator, e) |
| 1295 |
self.popvalue() |
| 1296 |
next_instr += jumpby * 2 |
| 1297 |
else: |
| 1298 |
self.pushvalue(w_nextitem) |
| 1299 |
return next_instr |
| 1300 |
|
| 1301 |
def _report_stopiteration_sometimes(self, w_iterator, operr): |
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
from pypy.interpreter.generator import GeneratorOrCoroutine, AsyncGenASend |
| 1315 |
if (isinstance(w_iterator, GeneratorOrCoroutine) or |
| 1316 |
isinstance(w_iterator, AsyncGenASend) or |
| 1317 |
operr.has_any_traceback()): |
| 1318 |
self.space.getexecutioncontext().exception_trace(self, operr) |
| 1319 |
|
| 1320 |
def BEFORE_WITH(self, oparg, next_instr): |
| 1321 |
w_manager = self.peekvalue() |
| 1322 |
w_enter = self.space.lookup(w_manager, "__enter__") |
| 1323 |
w_descr = self.space.lookup(w_manager, "__exit__") |
| 1324 |
if w_enter is None or w_descr is None: |
| 1325 |
raise oefmt(self.space.w_TypeError, |
| 1326 |
"'%T' object does not support the context manager " |
| 1327 |
"protocol (no __enter__/__exit__ method)", w_manager) |
| 1328 |
w_exit = self.space.get(w_descr, w_manager) |
| 1329 |
self.settopvalue(w_exit) |
| 1330 |
w_result = self.space.get_and_call_function(w_enter, w_manager) |
| 1331 |
self.pushvalue(w_result) |
| 1332 |
|
| 1333 |
def WITH_EXCEPT_START(self, oparg, next_instr): |
| 1334 |
|
| 1335 |
w_exc = self.peekvalue(0) |
| 1336 |
w_exitfunc = self.peekvalue(3) |
| 1337 |
space = self.space |
| 1338 |
from pypy.module.exceptions.interp_exceptions import W_BaseException |
| 1339 |
w_value = space.interp_w(W_BaseException, w_exc) |
| 1340 |
w_type = space.type(w_exc) |
| 1341 |
w_traceback = w_value.w_traceback |
| 1342 |
if w_traceback is None: |
| 1343 |
w_traceback = space.w_None |
| 1344 |
w_res = self.call_contextmanager_exit_function( |
| 1345 |
w_exitfunc, w_type, w_exc, w_traceback) |
| 1346 |
self.pushvalue(w_res) |
| 1347 |
|
| 1348 |
def RERAISE(self, oparg, next_instr): |
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
if oparg: |
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
reraise_lasti = self.space.int_w(self.peekvalue(oparg)) |
| 1362 |
else: |
| 1363 |
reraise_lasti = -1 |
| 1364 |
w_exc = self.popvalue() |
| 1365 |
from pypy.module.exceptions.interp_exceptions import W_BaseException |
| 1366 |
space = self.space |
| 1367 |
w_value = space.interp_w(W_BaseException, w_exc) |
| 1368 |
w_type = space.type(w_exc) |
| 1369 |
operr = OperationError(w_type, w_exc, w_value.w_traceback) |
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
raise RaiseWithExplicitTraceback(operr, reraise_lasti) |
| 1377 |
|
| 1378 |
def CALL_FUNCTION(self, oparg, next_instr): |
| 1379 |
|
| 1380 |
nargs = oparg & 0xff |
| 1381 |
w_function = self.peekvalue(nargs) |
| 1382 |
w_result = self.space.call_valuestack(w_function, nargs, self, dropvalues=nargs+1) |
| 1383 |
self.pushvalue(w_result) |
| 1384 |
|
| 1385 |
@jit.unroll_safe |
| 1386 |
def CALL_FUNCTION_KW(self, n_arguments, next_instr): |
| 1387 |
from pypy.objspace.std.tupleobject import W_AbstractTupleObject |
| 1388 |
space = self.space |
| 1389 |
|
| 1390 |
|
| 1391 |
w_tup_varnames = space.interp_w(W_AbstractTupleObject, self.popvalue()) |
| 1392 |
n_keywords = space.len_w(w_tup_varnames) |
| 1393 |
keyword_names_w = [None] * n_keywords |
| 1394 |
keywords_w = [None] * n_keywords |
| 1395 |
for i in range(n_keywords): |
| 1396 |
keyword_names_w[i] = w_tup_varnames.getitem(space, i) |
| 1397 |
w_value = self.peekvalue(n_keywords - 1 - i) |
| 1398 |
keywords_w[i] = w_value |
| 1399 |
self.dropvalues(n_keywords) |
| 1400 |
n_arguments -= n_keywords |
| 1401 |
arguments = self.popvalues(n_arguments) |
| 1402 |
w_function = self.popvalue() |
| 1403 |
args = self.argument_factory(arguments, keyword_names_w, keywords_w, None, None, |
| 1404 |
w_function=w_function) |
| 1405 |
if self.get_is_being_profiled() and function.is_builtin_code(w_function): |
| 1406 |
w_result = self.space.call_args_and_c_profile(self, w_function, |
| 1407 |
args) |
| 1408 |
else: |
| 1409 |
w_result = self.space.call_args(w_function, args) |
| 1410 |
self.pushvalue(w_result) |
| 1411 |
|
| 1412 |
def CALL_FUNCTION_EX(self, has_kwarg, next_instr): |
| 1413 |
w_kwargs = None |
| 1414 |
if has_kwarg: |
| 1415 |
w_kwargs = self.popvalue() |
| 1416 |
w_args = self.popvalue() |
| 1417 |
w_function = self.popvalue() |
| 1418 |
args = self.argument_factory( |
| 1419 |
[], None, None, w_star=w_args, w_starstar=w_kwargs, w_function=w_function) |
| 1420 |
if self.get_is_being_profiled() and function.is_builtin_code(w_function): |
| 1421 |
w_result = self.space.call_args_and_c_profile(self, w_function, |
| 1422 |
args) |
| 1423 |
else: |
| 1424 |
w_result = self.space.call_args(w_function, args) |
| 1425 |
self.pushvalue(w_result) |
| 1426 |
|
| 1427 |
@jit.unroll_safe |
| 1428 |
def MAKE_FUNCTION(self, oparg, next_instr): |
| 1429 |
space = self.space |
| 1430 |
w_codeobj = self.popvalue() |
| 1431 |
codeobj = self.space.interp_w(PyCode, w_codeobj) |
| 1432 |
assert 0 <= oparg <= 0x0F |
| 1433 |
if oparg & 0x08: |
| 1434 |
w_freevarstuple = self.popvalue() |
| 1435 |
|
| 1436 |
freevars = [self.space.interp_w(Cell, cell) |
| 1437 |
for cell in self.space.fixedview(w_freevarstuple)] |
| 1438 |
else: |
| 1439 |
freevars = None |
| 1440 |
if oparg & 0x04: |
| 1441 |
w_ann = self.popvalue() |
| 1442 |
else: |
| 1443 |
w_ann = None |
| 1444 |
if oparg & 0x02: |
| 1445 |
w_kw_defs = self.popvalue() |
| 1446 |
|
| 1447 |
kw_defs_w = [space.unpackiterable(w_tup) |
| 1448 |
for w_tup in space.fixedview( |
| 1449 |
space.call_method(w_kw_defs, 'items'))] |
| 1450 |
else: |
| 1451 |
kw_defs_w = None |
| 1452 |
if oparg & 0x01: |
| 1453 |
defaultarguments = space.fixedview(self.popvalue()) |
| 1454 |
else: |
| 1455 |
defaultarguments = [] |
| 1456 |
|
| 1457 |
fn = function.Function(space, codeobj, self.get_w_globals(), |
| 1458 |
defaultarguments, |
| 1459 |
kw_defs_w, freevars, w_ann, |
| 1460 |
qualname=codeobj.co_qualname) |
| 1461 |
self.pushvalue(fn) |
| 1462 |
|
| 1463 |
def BUILD_SLICE(self, numargs, next_instr): |
| 1464 |
if numargs == 3: |
| 1465 |
w_step = self.popvalue() |
| 1466 |
elif numargs == 2: |
| 1467 |
w_step = self.space.w_None |
| 1468 |
else: |
| 1469 |
raise BytecodeCorruption |
| 1470 |
w_end = self.popvalue() |
| 1471 |
w_start = self.popvalue() |
| 1472 |
w_slice = self.space.newslice(w_start, w_end, w_step) |
| 1473 |
self.pushvalue(w_slice) |
| 1474 |
|
| 1475 |
def LIST_APPEND(self, oparg, next_instr): |
| 1476 |
w = self.popvalue() |
| 1477 |
v = self.peekvalue(oparg - 1) |
| 1478 |
self.space.call_method(v, 'append', w) |
| 1479 |
|
| 1480 |
def LIST_EXTEND(self, oparg, next_instr): |
| 1481 |
w = self.popvalue() |
| 1482 |
v = self.peekvalue(oparg - 1) |
| 1483 |
space = self.space |
| 1484 |
try: |
| 1485 |
space.call_method(v, 'extend', w) |
| 1486 |
except OperationError as e: |
| 1487 |
if space.is_iterable(w): |
| 1488 |
raise |
| 1489 |
raise oefmt(space.w_TypeError, |
| 1490 |
"Value after * must be an iterable, not %T", |
| 1491 |
w) |
| 1492 |
|
| 1493 |
def LIST_TO_TUPLE(self, oparg, next_instr): |
| 1494 |
w_l = self.popvalue() |
| 1495 |
w_res = self.space.call_function(self.space.w_tuple, w_l) |
| 1496 |
self.pushvalue(w_res) |
| 1497 |
|
| 1498 |
def SET_ADD(self, oparg, next_instr): |
| 1499 |
w_value = self.popvalue() |
| 1500 |
w_set = self.peekvalue(oparg - 1) |
| 1501 |
self.space.call_method(w_set, 'add', w_value) |
| 1502 |
|
| 1503 |
def SET_UPDATE(self, oparg, next_instr): |
| 1504 |
w_update = self.popvalue() |
| 1505 |
w_set = self.peekvalue(oparg - 1) |
| 1506 |
self.space.call_method(w_set, 'update', w_update) |
| 1507 |
|
| 1508 |
def MAP_ADD(self, oparg, next_instr): |
| 1509 |
w_value = self.popvalue() |
| 1510 |
w_key = self.popvalue() |
| 1511 |
w_dict = self.peekvalue(oparg - 1) |
| 1512 |
self.space.setitem(w_dict, w_key, w_value) |
| 1513 |
|
| 1514 |
def DICT_MERGE(self, oparg, next_instr): |
| 1515 |
from pypy.objspace.std.dictmultiobject import W_DictMultiObject |
| 1516 |
space = self.space |
| 1517 |
w_dict = self.peekvalue(1) |
| 1518 |
w_item = self.popvalue() |
| 1519 |
|
| 1520 |
|
| 1521 |
w_function = self.peekvalue(oparg+2) |
| 1522 |
_dict_merge(space, w_dict, w_item, w_function) |
| 1523 |
|
| 1524 |
def DICT_UPDATE(self, oparg, next_instr): |
| 1525 |
w_item = self.popvalue() |
| 1526 |
space = self.space |
| 1527 |
if not space.ismapping_w(w_item): |
| 1528 |
raise oefmt(space.w_TypeError, |
| 1529 |
"'%T' object is not a mapping", |
| 1530 |
w_item) |
| 1531 |
w_dict = self.peekvalue() |
| 1532 |
space.call_method(w_dict, "update", w_item) |
| 1533 |
|
| 1534 |
|
| 1535 |
LOAD_METHOD = LOAD_ATTR |
| 1536 |
CALL_METHOD = CALL_FUNCTION |
| 1537 |
CALL_METHOD_KW = CALL_FUNCTION_KW |
| 1538 |
|
| 1539 |
def MISSING_OPCODE(self, oparg, next_instr): |
| 1540 |
ofs = self.last_instr |
| 1541 |
c = self.pycode.co_code[ofs] |
| 1542 |
name = self.pycode.co_name |
| 1543 |
raise BytecodeCorruption("unknown opcode, ofs=%d, code=%d, name=%s" % |
| 1544 |
(ofs, ord(c), name) ) |
| 1545 |
|
| 1546 |
@jit.unroll_safe |
| 1547 |
def BUILD_MAP(self, itemcount, next_instr): |
| 1548 |
w_dict = self.space.newdict() |
| 1549 |
for i in range(itemcount-1, -1, -1): |
| 1550 |
w_value = self.peekvalue(2 * i) |
| 1551 |
w_key = self.peekvalue(2 * i + 1) |
| 1552 |
self.space.setitem(w_dict, w_key, w_value) |
| 1553 |
self.dropvalues(2 * itemcount) |
| 1554 |
self.pushvalue(w_dict) |
| 1555 |
|
| 1556 |
@jit.unroll_safe |
| 1557 |
def BUILD_CONST_KEY_MAP(self, itemcount, next_instr): |
| 1558 |
from pypy.objspace.std.tupleobject import W_AbstractTupleObject |
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
w_keys = self.space.interp_w(W_AbstractTupleObject, self.popvalue()) |
| 1563 |
w_dict = self.space.newdict() |
| 1564 |
for i in range(itemcount): |
| 1565 |
w_value = self.peekvalue(itemcount - 1 - i) |
| 1566 |
w_key = w_keys.getitem(self.space, i) |
| 1567 |
self.space.setitem(w_dict, w_key, w_value) |
| 1568 |
self.dropvalues(itemcount) |
| 1569 |
self.pushvalue(w_dict) |
| 1570 |
|
| 1571 |
@jit.unroll_safe |
| 1572 |
def BUILD_SET(self, itemcount, next_instr): |
| 1573 |
w_set = self.space.newset() |
| 1574 |
for i in range(itemcount-1, -1, -1): |
| 1575 |
w_item = self.peekvalue(i) |
| 1576 |
self.space.call_method(w_set, 'add', w_item) |
| 1577 |
self.dropvalues(itemcount) |
| 1578 |
self.pushvalue(w_set) |
| 1579 |
|
| 1580 |
def GET_YIELD_FROM_ITER(self, oparg, next_instr): |
| 1581 |
from pypy.interpreter.astcompiler import consts |
| 1582 |
from pypy.interpreter.generator import GeneratorIterator, Coroutine |
| 1583 |
w_iterable = self.peekvalue() |
| 1584 |
if isinstance(w_iterable, Coroutine): |
| 1585 |
if not self.pycode.co_flags & (consts.CO_COROUTINE | |
| 1586 |
consts.CO_ITERABLE_COROUTINE): |
| 1587 |
|
| 1588 |
|
| 1589 |
raise oefmt(self.space.w_TypeError, |
| 1590 |
"cannot 'yield from' a coroutine object " |
| 1591 |
"in a non-coroutine generator") |
| 1592 |
elif not isinstance(w_iterable, GeneratorIterator): |
| 1593 |
w_iterator = self.space.iter(w_iterable) |
| 1594 |
self.settopvalue(w_iterator) |
| 1595 |
|
| 1596 |
def LOAD_ASSERTION_ERROR(self, oparg, next_instr): |
| 1597 |
self.pushvalue(self.space.w_AssertionError) |
| 1598 |
|
| 1599 |
def GET_AWAITABLE(self, oparg, next_instr): |
| 1600 |
from pypy.interpreter.generator import get_awaitable_iter |
| 1601 |
from pypy.interpreter.generator import Coroutine |
| 1602 |
w_iterable = self.popvalue() |
| 1603 |
w_iter = get_awaitable_iter(self.space, w_iterable, oparg) |
| 1604 |
if isinstance(w_iter, Coroutine): |
| 1605 |
if w_iter.get_delegate() is not None: |
| 1606 |
|
| 1607 |
|
| 1608 |
raise oefmt(self.space.w_RuntimeError, |
| 1609 |
"coroutine is being awaited already") |
| 1610 |
self.pushvalue(w_iter) |
| 1611 |
|
| 1612 |
def BEFORE_ASYNC_WITH(self, oparg, next_instr): |
| 1613 |
space = self.space |
| 1614 |
w_manager = self.peekvalue() |
| 1615 |
w_enter = space.lookup(w_manager, "__aenter__") |
| 1616 |
w_descr = space.lookup(w_manager, "__aexit__") |
| 1617 |
if w_enter is None or w_descr is None: |
| 1618 |
raise oefmt(space.w_TypeError, |
| 1619 |
"'%T' object does not support the asynchronous context " |
| 1620 |
"manager protocol (no __aenter__/__aexit__ method)", |
| 1621 |
w_manager) |
| 1622 |
w_exit = space.get(w_descr, w_manager) |
| 1623 |
self.settopvalue(w_exit) |
| 1624 |
w_result = space.get_and_call_function(w_enter, w_manager) |
| 1625 |
self.pushvalue(w_result) |
| 1626 |
|
| 1627 |
def GET_AITER(self, oparg, next_instr): |
| 1628 |
from pypy.interpreter.generator import AIterWrapper, get_awaitable_iter |
| 1629 |
|
| 1630 |
space = self.space |
| 1631 |
w_obj = self.popvalue() |
| 1632 |
w_func = space.lookup(w_obj, "__aiter__") |
| 1633 |
if w_func is None: |
| 1634 |
raise oefmt(space.w_TypeError, |
| 1635 |
"'async for' requires an object with " |
| 1636 |
"__aiter__ method, got %T", |
| 1637 |
w_obj) |
| 1638 |
w_iter = space.get_and_call_function(w_func, w_obj) |
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
if space.lookup(w_iter, "__anext__") is not None: |
| 1643 |
self.pushvalue(w_iter) |
| 1644 |
else: |
| 1645 |
new_error = oefmt(space.w_TypeError, |
| 1646 |
"'async for' received a object from __aiter__ that" |
| 1647 |
" does not implement __anext__: %T", w_iter) |
| 1648 |
raise new_error |
| 1649 |
|
| 1650 |
def GET_ANEXT(self, oparg, next_instr): |
| 1651 |
from pypy.interpreter.generator import get_awaitable_iter |
| 1652 |
|
| 1653 |
|
| 1654 |
space = self.space |
| 1655 |
w_aiter = self.peekvalue() |
| 1656 |
w_func = space.lookup(w_aiter, "__anext__") |
| 1657 |
if w_func is None: |
| 1658 |
raise oefmt(space.w_TypeError, |
| 1659 |
"'async for' requires an iterator with " |
| 1660 |
"__anext__ method, got %T", |
| 1661 |
w_aiter) |
| 1662 |
w_next_iter = space.get_and_call_function(w_func, w_aiter) |
| 1663 |
try: |
| 1664 |
w_awaitable = get_awaitable_iter(space, w_next_iter) |
| 1665 |
except OperationError as e: |
| 1666 |
if e.async(space): |
| 1667 |
raise |
| 1668 |
new_error = oefmt(space.w_TypeError, |
| 1669 |
"'async for' received an invalid object " |
| 1670 |
"from __anext__: %T", w_next_iter) |
| 1671 |
w_cause = e.normalize_exception(space) |
| 1672 |
new_error.normalize_exception(space) |
| 1673 |
new_error.set_cause(space, w_cause) |
| 1674 |
raise new_error |
| 1675 |
self.pushvalue(w_awaitable) |
| 1676 |
|
| 1677 |
def END_ASYNC_FOR(self, oparg, next_instr): |
| 1678 |
|
| 1679 |
w_exc = self.popvalue() |
| 1680 |
w_prev = self.popvalue() |
| 1681 |
if self.space.exception_match(self.space.type(w_exc), self.space.w_StopAsyncIteration): |
| 1682 |
self._restore_exc_info(w_prev) |
| 1683 |
self.popvalue() |
| 1684 |
return next_instr |
| 1685 |
else: |
| 1686 |
|
| 1687 |
|
| 1688 |
from pypy.module.exceptions.interp_exceptions import W_BaseException |
| 1689 |
space = self.space |
| 1690 |
w_value = space.interp_w(W_BaseException, w_exc) |
| 1691 |
w_type = space.type(w_exc) |
| 1692 |
operr = OperationError(w_type, w_exc, w_value.w_traceback) |
| 1693 |
raise RaiseWithExplicitTraceback(operr) |
| 1694 |
|
| 1695 |
def FORMAT_VALUE(self, oparg, next_instr): |
| 1696 |
from pypy.interpreter.astcompiler import consts |
| 1697 |
space = self.space |
| 1698 |
|
| 1699 |
if (oparg & consts.FVS_MASK) == consts.FVS_HAVE_SPEC: |
| 1700 |
w_spec = self.popvalue() |
| 1701 |
else: |
| 1702 |
w_spec = space.newtext('') |
| 1703 |
w_value = self.popvalue() |
| 1704 |
|
| 1705 |
conversion = oparg & consts.FVC_MASK |
| 1706 |
if conversion == consts.FVC_STR: |
| 1707 |
w_value = space.str(w_value) |
| 1708 |
elif conversion == consts.FVC_REPR: |
| 1709 |
w_value = space.repr(w_value) |
| 1710 |
elif conversion == consts.FVC_ASCII: |
| 1711 |
from pypy.objspace.std.unicodeobject import ascii_from_object |
| 1712 |
w_value = ascii_from_object(space, w_value) |
| 1713 |
|
| 1714 |
w_res = space.format(w_value, w_spec) |
| 1715 |
self.pushvalue(w_res) |
| 1716 |
|
| 1717 |
@jit.unroll_safe |
| 1718 |
def BUILD_STRING(self, itemcount, next_instr): |
| 1719 |
from rpython.rlib import rutf8 |
| 1720 |
space = self.space |
| 1721 |
builder = rutf8.Utf8StringBuilder() |
| 1722 |
for i in range(itemcount-1, -1, -1): |
| 1723 |
w_item = self.peekvalue(i) |
| 1724 |
utf8, length = space.utf8_len_w(w_item) |
| 1725 |
builder.append_utf8(utf8, length) |
| 1726 |
self.dropvalues(itemcount) |
| 1727 |
w_res = space.newutf8(builder.build(), builder.getlength()) |
| 1728 |
self.pushvalue(w_res) |
| 1729 |
|
| 1730 |
def _revdb_load_var(self, oparg): |
| 1731 |
|
| 1732 |
from pypy.interpreter.reverse_debugging import load_metavar |
| 1733 |
w_var = load_metavar(oparg) |
| 1734 |
self.pushvalue(w_var) |
| 1735 |
|
| 1736 |
def LOAD_REVDB_VAR(self, oparg, next_instr): |
| 1737 |
if self.space.reverse_debugging: |
| 1738 |
self._revdb_load_var(oparg) |
| 1739 |
else: |
| 1740 |
self.MISSING_OPCODE(oparg, next_instr) |
| 1741 |
|
| 1742 |
def MATCH_SEQUENCE(self, oparg, next_instr): |
| 1743 |
w_sequence = self.peekvalue() |
| 1744 |
space = self.space |
| 1745 |
flag_patma_collection = space.type(w_sequence).flag_patma_collection |
| 1746 |
if flag_patma_collection == "M": |
| 1747 |
is_sequence = False |
| 1748 |
else: |
| 1749 |
is_sequence = flag_patma_collection == "S" |
| 1750 |
if not is_sequence: |
| 1751 |
is_sequence = space.issequence_w(w_sequence) |
| 1752 |
|
| 1753 |
is_sequence = (is_sequence and not |
| 1754 |
space.isinstance_w(w_sequence, space.w_unicode) and not |
| 1755 |
space.isinstance_w(w_sequence, space.w_bytes) and not |
| 1756 |
space.isinstance_w(w_sequence, space.w_bytearray)) |
| 1757 |
self.pushvalue(space.newbool(is_sequence)) |
| 1758 |
|
| 1759 |
def MATCH_MAPPING(self, oparg, next_instr): |
| 1760 |
w_mapping = self.peekvalue() |
| 1761 |
flag_patma_collection = self.space.type(w_mapping).flag_patma_collection |
| 1762 |
if flag_patma_collection == "S": |
| 1763 |
is_mapping = False |
| 1764 |
else: |
| 1765 |
is_mapping = flag_patma_collection == "M" |
| 1766 |
if not is_mapping: |
| 1767 |
is_mapping = self.space.ismapping_w(w_mapping) |
| 1768 |
self.pushvalue(self.space.newbool(is_mapping)) |
| 1769 |
|
| 1770 |
def MATCH_CLASS(self, oparg, next_instr): |
| 1771 |
space = self.space |
| 1772 |
nargs = oparg |
| 1773 |
w_names = self.popvalue() |
| 1774 |
w_type = self.popvalue() |
| 1775 |
w_subject = self.popvalue() |
| 1776 |
|
| 1777 |
w_attribute_tuple = _match_class(self.space, nargs, w_names, w_type, w_subject) |
| 1778 |
if w_attribute_tuple is None: |
| 1779 |
self.pushvalue(space.w_None) |
| 1780 |
self.pushvalue(space.w_False) |
| 1781 |
else: |
| 1782 |
self.pushvalue(w_attribute_tuple) |
| 1783 |
self.pushvalue(space.w_True) |
| 1784 |
|
| 1785 |
|
| 1786 |
def GET_LEN(self, oparg, next_instr): |
| 1787 |
w_sequence = self.peekvalue() |
| 1788 |
w_len = self.space.len(w_sequence) |
| 1789 |
self.pushvalue(w_len) |
| 1790 |
|
| 1791 |
@jit.unroll_safe |
| 1792 |
def MATCH_KEYS(self, oparg, next_instr): |
| 1793 |
w_keys = self.peekvalue() |
| 1794 |
w_dict = self.peekvalue(1) |
| 1795 |
length = self.space.len_w(w_keys) |
| 1796 |
values_w = [None] * length |
| 1797 |
w_seen = self.space.newset() |
| 1798 |
w_iter = self.space.iter(w_keys) |
| 1799 |
w_sentinel = self.space.call_method(self.space.builtin, "object") |
| 1800 |
try: |
| 1801 |
i = 0 |
| 1802 |
while True: |
| 1803 |
w_key = self.space.next(w_iter) |
| 1804 |
if self.space.contains_w(w_seen, w_key): |
| 1805 |
raise oefmt(self.space.w_ValueError, "mapping pattern checks duplicate key (%R)", w_key) |
| 1806 |
self.space.call_method(w_seen, "add", w_key) |
| 1807 |
w_value = self.space.call_method(w_dict, 'get', w_key, w_sentinel) |
| 1808 |
if not self.space.is_w(w_value, w_sentinel): |
| 1809 |
values_w[i] = w_value |
| 1810 |
else: |
| 1811 |
self.pushvalue(self.space.w_None) |
| 1812 |
return |
| 1813 |
i += 1 |
| 1814 |
except OperationError as e: |
| 1815 |
if not e.match(self.space, self.space.w_StopIteration): |
| 1816 |
raise |
| 1817 |
|
| 1818 |
self.pushvalue(self.space.newtuple(values_w)) |
| 1819 |
|
| 1820 |
def COPY_DICT_WITHOUT_KEYS(self, oparg, next_instr): |
| 1821 |
w_keys = self.popvalue() |
| 1822 |
w_subject = self.peekvalue() |
| 1823 |
w_dict = _copy_dict_without_keys(self.space, w_keys, w_subject) |
| 1824 |
self.pushvalue(w_dict) |
| 1825 |
|
| 1826 |
@jit.unroll_safe |
| 1827 |
def SWAP(self, oparg, next_instr): |
| 1828 |
|
| 1829 |
|
| 1830 |
assert oparg >= 2 |
| 1831 |
w_top = self.peekvalue(0) |
| 1832 |
w_i = self.peekvalue(oparg - 1) |
| 1833 |
self.settopvalue(w_i, 0) |
| 1834 |
self.settopvalue(w_top, oparg - 1) |
| 1835 |
|
| 1836 |
def CHECK_EG_MATCH(self, oparg, next_instr): |
| 1837 |
space = self.space |
| 1838 |
w_typ = self.popvalue() |
| 1839 |
check_except_star_type_valid(space, w_typ) |
| 1840 |
w_eg = self.peekvalue() |
| 1841 |
w_match, w_rest = exception_group_match(space, w_eg, w_typ) |
| 1842 |
if space.is_w(w_match, space.w_None): |
| 1843 |
self.pushvalue(w_match) |
| 1844 |
else: |
| 1845 |
self.settopvalue(w_rest) |
| 1846 |
self.pushvalue(w_match) |
| 1847 |
ec = space.getexecutioncontext() |
| 1848 |
ec.set_sys_exc_info3(w_match) |
| 1849 |
|
| 1850 |
def CHECK_EXC_MATCH(self, oparg, next_instr): |
| 1851 |
w_right = self.popvalue() |
| 1852 |
w_left = self.peekvalue(0) |
| 1853 |
res = self.cmp_exc_match(w_left, w_right) |
| 1854 |
self.pushvalue(self.space.newbool(res)) |
| 1855 |
|
| 1856 |
def PREP_RERAISE_STAR(self, oparg, next_instr): |
| 1857 |
space = self.space |
| 1858 |
w_res = self.popvalue() |
| 1859 |
w_orig = self.popvalue() |
| 1860 |
from pypy.module.exceptions.interp_group import prep_reraise_star |
| 1861 |
w_eg_or_None = prep_reraise_star(space, w_orig, w_res) |
| 1862 |
self.pushvalue(w_eg_or_None) |
| 1863 |
|
| 1864 |
|
| 1865 |
def delegate_to_nongen(space, w_yf, w_inputvalue_or_err): |
| 1866 |
|
| 1867 |
if isinstance(w_inputvalue_or_err, SApplicationException): |
| 1868 |
operr = w_inputvalue_or_err.operr |
| 1869 |
try: |
| 1870 |
w_meth = space.getattr(w_yf, space.newtext("throw")) |
| 1871 |
except OperationError as e: |
| 1872 |
if not e.match(space, space.w_AttributeError): |
| 1873 |
raise |
| 1874 |
raise operr |
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
w_val = operr.normalize_exception(space) |
| 1879 |
w_exc = operr.w_type |
| 1880 |
w_tb = operr.get_w_traceback(space) |
| 1881 |
return space.call_function(w_meth, w_exc, w_val, w_tb) |
| 1882 |
else: |
| 1883 |
return space.call_method(w_yf, "send", w_inputvalue_or_err) |
| 1884 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
|
| 1888 |
class ExitFrame(Exception): |
| 1889 |
pass |
| 1890 |
|
| 1891 |
|
| 1892 |
class Return(ExitFrame): |
| 1893 |
"""Raised when exiting a frame via a 'return' statement.""" |
| 1894 |
|
| 1895 |
|
| 1896 |
class Yield(ExitFrame): |
| 1897 |
"""Raised when exiting a frame via a 'yield' statement.""" |
| 1898 |
|
| 1899 |
|
| 1900 |
class RaiseWithExplicitTraceback(Exception): |
| 1901 |
"""Raised at interp-level by a 0-argument 'raise' statement.""" |
| 1902 |
def __init__(self, operr, lasti=-1): |
| 1903 |
self.operr = operr |
| 1904 |
self.lasti = lasti |
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
class SApplicationException(W_Root): |
| 1910 |
"""Signals an application-level exception |
| 1911 |
(i.e. an OperationException).""" |
| 1912 |
_immutable_ = True |
| 1913 |
def __init__(self, operr): |
| 1914 |
self.operr = operr |
| 1915 |
def reraise(self): |
| 1916 |
raise RaiseWithExplicitTraceback(self.operr) |
| 1917 |
|
| 1918 |
|
| 1919 |
def source_as_str(space, w_source, funcname, what, flags): |
| 1920 |
"""Return source code as str0 with adjusted compiler flags |
| 1921 |
|
| 1922 |
w_source must be a str or support the buffer interface |
| 1923 |
""" |
| 1924 |
from pypy.interpreter.astcompiler import consts |
| 1925 |
|
| 1926 |
if space.isinstance_w(w_source, space.w_unicode): |
| 1927 |
from pypy.interpreter.unicodehelper import encode |
| 1928 |
w_source = encode(space, w_source) |
| 1929 |
source = space.bytes_w(w_source) |
| 1930 |
flags |= consts.PyCF_IGNORE_COOKIE |
| 1931 |
elif space.isinstance_w(w_source, space.w_bytes): |
| 1932 |
source = space.bytes_w(w_source) |
| 1933 |
else: |
| 1934 |
try: |
| 1935 |
buf = space.buffer_w(w_source, space.BUF_SIMPLE) |
| 1936 |
except OperationError as e: |
| 1937 |
if not e.match(space, space.w_TypeError): |
| 1938 |
raise |
| 1939 |
raise oefmt(space.w_TypeError, |
| 1940 |
"%s() arg 1 must be a %s object", funcname, what) |
| 1941 |
source = buf.as_str() |
| 1942 |
|
| 1943 |
if not (flags & consts.PyCF_ACCEPT_NULL_BYTES): |
| 1944 |
if '\x00' in source: |
| 1945 |
raise oefmt(space.w_SyntaxError, |
| 1946 |
"source code string cannot contain null bytes") |
| 1947 |
source = rstring.assert_str0(source) |
| 1948 |
return source, flags |
| 1949 |
|
| 1950 |
|
| 1951 |
def ensure_ns(space, w_globals, w_locals, funcname, caller=None): |
| 1952 |
"""Ensure globals/locals exist and are of the correct type""" |
| 1953 |
if (not space.is_none(w_globals) and |
| 1954 |
not space.isinstance_w(w_globals, space.w_dict)): |
| 1955 |
raise oefmt(space.w_TypeError, |
| 1956 |
'%s() arg 2 must be a dict, not %T', funcname, w_globals) |
| 1957 |
if (not space.is_none(w_locals) and |
| 1958 |
space.lookup(w_locals, '__getitem__') is None): |
| 1959 |
raise oefmt(space.w_TypeError, |
| 1960 |
'%s() arg 3 must be a mapping or None, not %T', |
| 1961 |
funcname, w_locals) |
| 1962 |
|
| 1963 |
if space.is_none(w_globals): |
| 1964 |
if caller is None: |
| 1965 |
caller = space.getexecutioncontext().gettopframe_nohidden() |
| 1966 |
if caller is None: |
| 1967 |
w_globals = space.newdict(module=True) |
| 1968 |
if space.is_none(w_locals): |
| 1969 |
w_locals = w_globals |
| 1970 |
else: |
| 1971 |
w_globals = caller.get_w_globals() |
| 1972 |
if space.is_none(w_locals): |
| 1973 |
w_locals = caller.getdictscope() |
| 1974 |
elif space.is_none(w_locals): |
| 1975 |
w_locals = w_globals |
| 1976 |
|
| 1977 |
return w_globals, w_locals |
| 1978 |
|
| 1979 |
def _dict_merge(space, w_dict, w_item, w_function): |
| 1980 |
from pypy.objspace.std.dictmultiobject import W_DictMultiObject, update1 |
| 1981 |
l1 = space.len_w(w_dict) |
| 1982 |
unroll_safe = jit.isvirtual(w_dict) and l1 < 10 |
| 1983 |
if not space.isinstance_w(w_dict, space.w_dict): |
| 1984 |
raise oefmt(space.w_RuntimeError, |
| 1985 |
"expected a dict, got %T", w_dict) |
| 1986 |
assert isinstance(w_dict, W_DictMultiObject) |
| 1987 |
if not space.isinstance_w(w_item, space.w_dict): |
| 1988 |
unroll_safe = False |
| 1989 |
if not space.ismapping_w(w_item): |
| 1990 |
raise oefmt(space.w_TypeError, |
| 1991 |
"%s argument after ** must be a mapping, not %T", |
| 1992 |
space.object_functionstr(w_function), w_item) |
| 1993 |
else: |
| 1994 |
l2 = space.len_w(w_item) |
| 1995 |
if l1 == 0: |
| 1996 |
update1(space, w_dict, w_item) |
| 1997 |
return |
| 1998 |
l2 = space.len_w(w_item) |
| 1999 |
if l2 == 0: |
| 2000 |
return |
| 2001 |
unroll_safe = unroll_safe and jit.isvirtual(w_item) and l2 < 10 |
| 2002 |
_dict_merge_loop(space, w_dict, w_item, unroll_safe, w_function) |
| 2003 |
|
| 2004 |
@jit.look_inside_iff(lambda space, w_dict, w_item, unroll_safe, w_function: unroll_safe) |
| 2005 |
def _dict_merge_loop(space, w_dict, w_item, unroll_safe, w_function): |
| 2006 |
try: |
| 2007 |
w_iterator = space.iter(space.call_method(w_item, "keys")) |
| 2008 |
except OperationError as e: |
| 2009 |
if not e.match(space, space.w_AttributeError): |
| 2010 |
raise |
| 2011 |
raise oefmt(space.w_TypeError, |
| 2012 |
"%s argument after ** must be a mapping, not %T", |
| 2013 |
space.object_functionstr(w_function), w_item) |
| 2014 |
while True: |
| 2015 |
try: |
| 2016 |
w_key = space.next(w_iterator) |
| 2017 |
except OperationError as e: |
| 2018 |
if not e.match(space, space.w_StopIteration): |
| 2019 |
raise |
| 2020 |
break |
| 2021 |
w_value = space.getitem(w_item, w_key) |
| 2022 |
if space.contains_w(w_dict, w_key): |
| 2023 |
raise oefmt(space.w_TypeError, |
| 2024 |
"%s got multiple values for keyword argument '%S'", |
| 2025 |
space.object_functionstr(w_function), w_key) |
| 2026 |
space.setitem(w_dict, w_key, w_value) |
| 2027 |
|
| 2028 |
def _copy_dict_without_keys(space, w_keys, w_subject): |
| 2029 |
w_dict = space.newdict() |
| 2030 |
space.call_method(w_dict, 'update', w_subject) |
| 2031 |
for i in range(space.len_w(w_keys)): |
| 2032 |
w_key = space.getitem(w_keys, space.newint(i)) |
| 2033 |
space.delitem(w_dict, w_key) |
| 2034 |
return w_dict |
| 2035 |
|
| 2036 |
def match_class_attr(space, w_subject, w_name, w_type, seen): |
| 2037 |
name = space.text_w(w_name) |
| 2038 |
if name in seen: |
| 2039 |
raise oefmt(space.w_TypeError, |
| 2040 |
"%N() got multiple sub-patterns for attribute %R", w_type, w_name) |
| 2041 |
seen[name] = None |
| 2042 |
return space.findattr(w_subject, w_name) |
| 2043 |
|
| 2044 |
@jit.unroll_safe |
| 2045 |
def _match_class(space, nargs, w_names, w_type, w_subject): |
| 2046 |
w_res = space.call_method(space.builtin, "isinstance", w_subject, w_type) |
| 2047 |
if not space.is_true(w_res): |
| 2048 |
return None |
| 2049 |
|
| 2050 |
seen = {} |
| 2051 |
attrs_w = [] |
| 2052 |
if nargs: |
| 2053 |
try: |
| 2054 |
w_match_args = space.getattr(w_type, space.newtext('__match_args__')) |
| 2055 |
match_self = False |
| 2056 |
except OperationError as e: |
| 2057 |
if not e.match(space, space.w_AttributeError): |
| 2058 |
raise e |
| 2059 |
|
| 2060 |
w_match_args = space.newtuple([]) |
| 2061 |
match_self = \ |
| 2062 |
space.isinstance_w(w_subject, space.w_float) or \ |
| 2063 |
space.isinstance_w(w_subject, space.w_tuple) or \ |
| 2064 |
space.isinstance_w(w_subject, space.w_dict) or \ |
| 2065 |
space.isinstance_w(w_subject, space.w_long) or \ |
| 2066 |
space.isinstance_w(w_subject, space.w_bytes) or \ |
| 2067 |
space.isinstance_w(w_subject, space.w_list) or \ |
| 2068 |
space.isinstance_w(w_subject, space.w_bytearray) or \ |
| 2069 |
space.isinstance_w(w_subject, space.w_unicode) or \ |
| 2070 |
space.isinstance_w(w_subject, space.w_set) or \ |
| 2071 |
space.isinstance_w(w_subject, space.w_frozenset) |
| 2072 |
if not space.isinstance_w(w_match_args, space.w_tuple): |
| 2073 |
raise oefmt(space.w_TypeError, |
| 2074 |
"%N.__match_args__ must be a tuple (got '%T')", |
| 2075 |
w_type, w_match_args) |
| 2076 |
|
| 2077 |
allowed = 1 if match_self else space.len_w(w_match_args) |
| 2078 |
if allowed < nargs: |
| 2079 |
plural = "" if allowed == 1 else "s"; |
| 2080 |
raise oefmt(space.w_TypeError, |
| 2081 |
"%N() accepts %d positional sub-pattern%s (%d given)", w_type, allowed, plural, nargs) |
| 2082 |
|
| 2083 |
if match_self: |
| 2084 |
attrs_w.append(w_subject) |
| 2085 |
else: |
| 2086 |
for i in range(nargs): |
| 2087 |
w_name = space.getitem(w_match_args, space.newint(i)) |
| 2088 |
if not space.isinstance_w(w_name, space.w_unicode): |
| 2089 |
raise oefmt(space.w_TypeError, |
| 2090 |
"__match_args__ elements must be strings (got '%T')", w_name) |
| 2091 |
w_attr = match_class_attr(space, w_subject, w_name, w_type, seen) |
| 2092 |
if w_attr is None: |
| 2093 |
return None |
| 2094 |
attrs_w.append(w_attr) |
| 2095 |
|
| 2096 |
w_iter = space.iter(w_names) |
| 2097 |
try: |
| 2098 |
while True: |
| 2099 |
w_name = space.next(w_iter) |
| 2100 |
w_attr = match_class_attr(space, w_subject, w_name, w_type, seen) |
| 2101 |
if w_attr is None: |
| 2102 |
return None |
| 2103 |
attrs_w.append(w_attr) |
| 2104 |
except OperationError as e: |
| 2105 |
if not e.match(space, space.w_StopIteration): |
| 2106 |
raise |
| 2107 |
|
| 2108 |
return space.newtuple(attrs_w[:]) |
| 2109 |
|
| 2110 |
|
| 2111 |
|
| 2112 |
@jit.unroll_safe |
| 2113 |
def check_except_star_type_valid(space, w_typ): |
| 2114 |
def check(space, w_typ, w_BaseExceptionGroup): |
| 2115 |
if not space.exception_is_valid_class_w(w_typ): |
| 2116 |
raise oefmt(space.w_TypeError, CANNOT_CATCH_MSG) |
| 2117 |
if space.issubtype_w(w_typ, w_BaseExceptionGroup): |
| 2118 |
raise oefmt(space.w_TypeError, "catching ExceptionGroup with except* is not allowed. Use except instead.") |
| 2119 |
w_BaseExceptionGroup = space.getattr(space.builtin, space.newtext('BaseExceptionGroup')) |
| 2120 |
if space.isinstance_w(w_typ, space.w_tuple): |
| 2121 |
for w_sub in space.fixedview(w_typ): |
| 2122 |
check(space, w_sub, w_BaseExceptionGroup) |
| 2123 |
else: |
| 2124 |
check(space, w_typ, w_BaseExceptionGroup) |
| 2125 |
|
| 2126 |
def exception_group_match(space, w_eg, w_typ): |
| 2127 |
if space.is_w(w_eg, space.w_None): |
| 2128 |
return space.w_None, space.w_None |
| 2129 |
assert space.isinstance_w(w_eg, space.w_Exception) |
| 2130 |
w_BaseExceptionGroup = space.getattr(space.builtin, space.newtext('BaseExceptionGroup')) |
| 2131 |
w_ExceptionGroup = space.getattr(space.builtin, space.newtext('ExceptionGroup')) |
| 2132 |
if space.exception_match(space.type(w_eg), w_typ): |
| 2133 |
if space.isinstance_w(w_eg, w_BaseExceptionGroup): |
| 2134 |
return w_eg, space.w_None |
| 2135 |
w_list = space.newlist([w_eg]) |
| 2136 |
w_wrapped = space.call_function(w_ExceptionGroup, space.newtext(''), w_list) |
| 2137 |
return w_wrapped, space.w_None |
| 2138 |
elif space.isinstance_w(w_eg, w_BaseExceptionGroup): |
| 2139 |
w_tup = space.call_method(w_eg, 'split', w_typ) |
| 2140 |
w_match, w_rest = space.unpackiterable(w_tup, 2) |
| 2141 |
return w_match, w_rest |
| 2142 |
else: |
| 2143 |
return space.w_None, space.w_None |
| 2144 |
|
| 2145 |
|
| 2146 |
|
| 2147 |
|
| 2148 |
|
| 2149 |
|
| 2150 |
|
| 2151 |
|
| 2152 |
|
| 2153 |
app = gateway.applevel(r''' |
| 2154 |
""" applevel implementation of certain system properties, imports |
| 2155 |
and other helpers""" |
| 2156 |
import sys |
| 2157 |
|
| 2158 |
def print_expr(obj): |
| 2159 |
try: |
| 2160 |
displayhook = sys.displayhook |
| 2161 |
except AttributeError: |
| 2162 |
raise RuntimeError("lost sys.displayhook") |
| 2163 |
displayhook(obj) |
| 2164 |
''', filename=__file__) |
| 2165 |
|
| 2166 |
print_expr = app.interphook('print_expr') |
| 2167 |
|
| 2168 |
app = gateway.applevel(r''' |
| 2169 |
def import_all_from(module, into_locals): |
| 2170 |
try: |
| 2171 |
all = module.__all__ |
| 2172 |
except AttributeError: |
| 2173 |
try: |
| 2174 |
dict = module.__dict__ |
| 2175 |
except AttributeError: |
| 2176 |
raise ImportError("from-import-* object has no __dict__ " |
| 2177 |
"and no __all__") |
| 2178 |
all = dict.keys() |
| 2179 |
skip_leading_underscores = True |
| 2180 |
else: |
| 2181 |
skip_leading_underscores = False |
| 2182 |
|
| 2183 |
module_name = module.__name__ |
| 2184 |
if not isinstance(module_name, str): |
| 2185 |
raise TypeError("module __name__ must be a string, not %s", type(module_name).__name__) |
| 2186 |
|
| 2187 |
for name in all: |
| 2188 |
if not isinstance(name, str): |
| 2189 |
if skip_leading_underscores: |
| 2190 |
container = "__dict__" |
| 2191 |
accessor = "Key" |
| 2192 |
else: |
| 2193 |
container = "__all__" |
| 2194 |
accessor = "Item" |
| 2195 |
|
| 2196 |
raise TypeError( |
| 2197 |
"%s in %s.%s must be str, not %s" % ( |
| 2198 |
accessor, |
| 2199 |
module_name, |
| 2200 |
container, |
| 2201 |
type(name).__name__ |
| 2202 |
) |
| 2203 |
) |
| 2204 |
if skip_leading_underscores and name and name[0] == '_': |
| 2205 |
continue |
| 2206 |
into_locals[name] = getattr(module, name) |
| 2207 |
''', filename=__file__) |
| 2208 |
|
| 2209 |
import_all_from = app.interphook('import_all_from') |