Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(6)

Delta Between Two Patch Sets: Python/ceval.c

Issue 4806: Function calls taking a generator as star argument can mask TypeErrors in the generator
Left Patch Set: Created 5 years, 1 month ago
Right Patch Set: Created 4 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Misc/NEWS ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 1
2 /* Execute compiled code */ 2 /* Execute compiled code */
3 3
4 /* XXX TO DO: 4 /* XXX TO DO:
5 XXX speed up searching for keywords by using a dictionary 5 XXX speed up searching for keywords by using a dictionary
6 XXX document it! 6 XXX document it!
7 */ 7 */
8 8
9 /* enable more aggressive intra-module optimizations, where available */ 9 /* enable more aggressive intra-module optimizations, where available */
10 #define PY_LOCAL_AGGRESSIVE 10 #define PY_LOCAL_AGGRESSIVE
11 11
12 #include "Python.h" 12 #include "Python.h"
13 13
14 #include "code.h" 14 #include "code.h"
15 #include "frameobject.h" 15 #include "frameobject.h"
16 #include "eval.h"
16 #include "opcode.h" 17 #include "opcode.h"
17 #include "structmember.h" 18 #include "structmember.h"
18 19
19 #include <ctype.h> 20 #include <ctype.h>
20 21
21 #ifndef WITH_TSC 22 #ifndef WITH_TSC
22 23
23 #define READ_TIMESTAMP(var) 24 #define READ_TIMESTAMP(var)
24 25
25 #else 26 #else
26 27
27 typedef unsigned long long uint64; 28 typedef unsigned long long uint64;
28 29
29 /* PowerPC support. 30 /* PowerPC support.
30 "__ppc__" appears to be the preprocessor definition to detect on OS X, wherea s 31 "__ppc__" appears to be the preprocessor definition to detect on OS X, wherea s
31 "__powerpc__" appears to be the correct one for Linux with GCC 32 "__powerpc__" appears to be the correct one for Linux with GCC
32 */ 33 */
33 #if defined(__ppc__) || defined (__powerpc__) 34 #if defined(__ppc__) || defined (__powerpc__)
34 35
35 #define READ_TIMESTAMP(var) ppc_getcounter(&var) 36 #define READ_TIMESTAMP(var) ppc_getcounter(&var)
36 37
37 static void 38 static void
38 ppc_getcounter(uint64 *v) 39 ppc_getcounter(uint64 *v)
39 { 40 {
40 unsigned long tbu, tb, tbu2; 41 register unsigned long tbu, tb, tbu2;
41 42
42 loop: 43 loop:
43 asm volatile ("mftbu %0" : "=r" (tbu) ); 44 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) ); 45 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2)); 46 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop; 47 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
47 48
48 /* The slightly peculiar way of writing the next lines is 49 /* The slightly peculiar way of writing the next lines is
49 compiled better by GCC than any other way I tried. */ 50 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu; 51 ((long*)(v))[0] = tbu;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 PyObject *); 119 PyObject *);
119 static PyObject * update_star_args(int, int, PyObject *, PyObject ***); 120 static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
120 static PyObject * load_args(PyObject ***, int); 121 static PyObject * load_args(PyObject ***, int);
121 #define CALL_FLAG_VAR 1 122 #define CALL_FLAG_VAR 1
122 #define CALL_FLAG_KW 2 123 #define CALL_FLAG_KW 2
123 124
124 #ifdef LLTRACE 125 #ifdef LLTRACE
125 static int lltrace; 126 static int lltrace;
126 static int prtrace(PyObject *, char *); 127 static int prtrace(PyObject *, char *);
127 #endif 128 #endif
128 static int call_trace(Py_tracefunc, PyObject *, 129 static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
129 PyThreadState *, PyFrameObject *,
130 int, PyObject *); 130 int, PyObject *);
131 static int call_trace_protected(Py_tracefunc, PyObject *, 131 static int call_trace_protected(Py_tracefunc, PyObject *,
132 PyThreadState *, PyFrameObject *, 132 PyFrameObject *, int, PyObject *);
133 int, PyObject *); 133 static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
134 static void call_exc_trace(Py_tracefunc, PyObject *,
135 PyThreadState *, PyFrameObject *);
136 static int maybe_call_line_trace(Py_tracefunc, PyObject *, 134 static int maybe_call_line_trace(Py_tracefunc, PyObject *,
137 PyThreadState *, PyFrameObject *, int *, int *, int *); 135 PyFrameObject *, int *, int *, int *);
138 136
137 static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
138 static int assign_slice(PyObject *, PyObject *,
139 PyObject *, PyObject *);
139 static PyObject * cmp_outcome(int, PyObject *, PyObject *); 140 static PyObject * cmp_outcome(int, PyObject *, PyObject *);
140 static PyObject * import_from(PyObject *, PyObject *); 141 static PyObject * import_from(PyObject *, PyObject *);
141 static int import_all_from(PyObject *, PyObject *); 142 static int import_all_from(PyObject *, PyObject *);
142 static void format_exc_check_arg(PyObject *, const char *, PyObject *); 143 static PyObject * build_class(PyObject *, PyObject *, PyObject *);
143 static void format_exc_unbound(PyCodeObject *co, int oparg); 144 static int exec_statement(PyFrameObject *,
144 static PyObject * unicode_concatenate(PyObject *, PyObject *, 145 PyObject *, PyObject *, PyObject *);
145 PyFrameObject *, unsigned char *); 146 static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
146 static PyObject * special_lookup(PyObject *, _Py_Identifier *); 147 static void reset_exc_info(PyThreadState *);
148 static void format_exc_check_arg(PyObject *, char *, PyObject *);
149 static PyObject * string_concatenate(PyObject *, PyObject *,
150 PyFrameObject *, unsigned char *);
151 static PyObject * kwd_as_string(PyObject *);
152 static PyObject * special_lookup(PyObject *, char *, PyObject **);
147 153
148 #define NAME_ERROR_MSG \ 154 #define NAME_ERROR_MSG \
149 "name '%.200s' is not defined" 155 "name '%.200s' is not defined"
156 #define GLOBAL_NAME_ERROR_MSG \
157 "global name '%.200s' is not defined"
150 #define UNBOUNDLOCAL_ERROR_MSG \ 158 #define UNBOUNDLOCAL_ERROR_MSG \
151 "local variable '%.200s' referenced before assignment" 159 "local variable '%.200s' referenced before assignment"
152 #define UNBOUNDFREE_ERROR_MSG \ 160 #define UNBOUNDFREE_ERROR_MSG \
153 "free variable '%.200s' referenced before assignment" \ 161 "free variable '%.200s' referenced before assignment" \
154 " in enclosing scope" 162 " in enclosing scope"
155 163
156 /* Dynamic execution profile */ 164 /* Dynamic execution profile */
157 #ifdef DYNAMIC_EXECUTION_PROFILE 165 #ifdef DYNAMIC_EXECUTION_PROFILE
158 #ifdef DXPAIRS 166 #ifdef DXPAIRS
159 static long dxpairs[257][256]; 167 static long dxpairs[257][256];
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 PyObject * 222 PyObject *
215 PyEval_GetCallStats(PyObject *self) 223 PyEval_GetCallStats(PyObject *self)
216 { 224 {
217 Py_INCREF(Py_None); 225 Py_INCREF(Py_None);
218 return Py_None; 226 return Py_None;
219 } 227 }
220 #endif 228 #endif
221 229
222 230
223 #ifdef WITH_THREAD 231 #ifdef WITH_THREAD
224 #define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
225 #else
226 #define GIL_REQUEST 0
227 #endif
228
229 /* This can set eval_breaker to 0 even though gil_drop_request became
230 1. We believe this is all right because the eval loop will release
231 the GIL eventually anyway. */
232 #define COMPUTE_EVAL_BREAKER() \
233 _Py_atomic_store_relaxed( \
234 &eval_breaker, \
235 GIL_REQUEST | \
236 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
237 pending_async_exc)
238
239 #ifdef WITH_THREAD
240
241 #define SET_GIL_DROP_REQUEST() \
242 do { \
243 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
244 _Py_atomic_store_relaxed(&eval_breaker, 1); \
245 } while (0)
246
247 #define RESET_GIL_DROP_REQUEST() \
248 do { \
249 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
250 COMPUTE_EVAL_BREAKER(); \
251 } while (0)
252
253 #endif
254
255 /* Pending calls are only modified under pending_lock */
256 #define SIGNAL_PENDING_CALLS() \
257 do { \
258 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
259 _Py_atomic_store_relaxed(&eval_breaker, 1); \
260 } while (0)
261
262 #define UNSIGNAL_PENDING_CALLS() \
263 do { \
264 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
265 COMPUTE_EVAL_BREAKER(); \
266 } while (0)
267
268 #define SIGNAL_ASYNC_EXC() \
269 do { \
270 pending_async_exc = 1; \
271 _Py_atomic_store_relaxed(&eval_breaker, 1); \
272 } while (0)
273
274 #define UNSIGNAL_ASYNC_EXC() \
275 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
276
277
278 #ifdef WITH_THREAD
279 232
280 #ifdef HAVE_ERRNO_H 233 #ifdef HAVE_ERRNO_H
281 #include <errno.h> 234 #include <errno.h>
282 #endif 235 #endif
283 #include "pythread.h" 236 #include "pythread.h"
284 237
238 static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
285 static PyThread_type_lock pending_lock = 0; /* for pending calls */ 239 static PyThread_type_lock pending_lock = 0; /* for pending calls */
286 static long main_thread = 0; 240 static long main_thread = 0;
287 /* This single variable consolidates all requests to break out of the fast path
288 in the eval loop. */
289 static _Py_atomic_int eval_breaker = {0};
290 /* Request for dropping the GIL */
291 static _Py_atomic_int gil_drop_request = {0};
292 /* Request for running pending calls. */
293 static _Py_atomic_int pendingcalls_to_do = {0};
294 /* Request for looking at the `async_exc` field of the current thread state.
295 Guarded by the GIL. */
296 static int pending_async_exc = 0;
297
298 #include "ceval_gil.h"
299 241
300 int 242 int
301 PyEval_ThreadsInitialized(void) 243 PyEval_ThreadsInitialized(void)
302 { 244 {
303 return gil_created(); 245 return interpreter_lock != 0;
304 } 246 }
305 247
306 void 248 void
307 PyEval_InitThreads(void) 249 PyEval_InitThreads(void)
308 { 250 {
309 if (gil_created()) 251 if (interpreter_lock)
310 return; 252 return;
311 create_gil(); 253 interpreter_lock = PyThread_allocate_lock();
312 take_gil(PyThreadState_GET()); 254 PyThread_acquire_lock(interpreter_lock, 1);
313 main_thread = PyThread_get_thread_ident(); 255 main_thread = PyThread_get_thread_ident();
314 if (!pending_lock)
315 pending_lock = PyThread_allocate_lock();
316 }
317
318 void
319 _PyEval_FiniThreads(void)
320 {
321 if (!gil_created())
322 return;
323 destroy_gil();
324 assert(!gil_created());
325 } 256 }
326 257
327 void 258 void
328 PyEval_AcquireLock(void) 259 PyEval_AcquireLock(void)
329 { 260 {
330 PyThreadState *tstate = PyThreadState_GET(); 261 PyThread_acquire_lock(interpreter_lock, 1);
331 if (tstate == NULL)
332 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
333 take_gil(tstate);
334 } 262 }
335 263
336 void 264 void
337 PyEval_ReleaseLock(void) 265 PyEval_ReleaseLock(void)
338 { 266 {
339 /* This function must succeed when the current thread state is NULL. 267 PyThread_release_lock(interpreter_lock);
340 We therefore avoid PyThreadState_GET() which dumps a fatal error
341 in debug mode.
342 */
343 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
344 &_PyThreadState_Current));
345 } 268 }
346 269
347 void 270 void
348 PyEval_AcquireThread(PyThreadState *tstate) 271 PyEval_AcquireThread(PyThreadState *tstate)
349 { 272 {
350 if (tstate == NULL) 273 if (tstate == NULL)
351 Py_FatalError("PyEval_AcquireThread: NULL new thread state"); 274 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
352 /* Check someone has called PyEval_InitThreads() to create the lock */ 275 /* Check someone has called PyEval_InitThreads() to create the lock */
353 assert(gil_created()); 276 assert(interpreter_lock);
354 take_gil(tstate); 277 PyThread_acquire_lock(interpreter_lock, 1);
355 if (PyThreadState_Swap(tstate) != NULL) 278 if (PyThreadState_Swap(tstate) != NULL)
356 Py_FatalError( 279 Py_FatalError(
357 "PyEval_AcquireThread: non-NULL old thread state"); 280 "PyEval_AcquireThread: non-NULL old thread state");
358 } 281 }
359 282
360 void 283 void
361 PyEval_ReleaseThread(PyThreadState *tstate) 284 PyEval_ReleaseThread(PyThreadState *tstate)
362 { 285 {
363 if (tstate == NULL) 286 if (tstate == NULL)
364 Py_FatalError("PyEval_ReleaseThread: NULL thread state"); 287 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
365 if (PyThreadState_Swap(NULL) != tstate) 288 if (PyThreadState_Swap(NULL) != tstate)
366 Py_FatalError("PyEval_ReleaseThread: wrong thread state"); 289 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
367 drop_gil(tstate); 290 PyThread_release_lock(interpreter_lock);
368 } 291 }
369 292
370 /* This function is called from PyOS_AfterFork to destroy all threads which are 293 /* This function is called from PyOS_AfterFork to ensure that newly
371 * not running in the child process, and clear internal locks which might be 294 created child processes don't hold locks referring to threads which
372 * held by those threads. (This could also be done using pthread_atfork 295 are not running in the child process. (This could also be done using
373 * mechanism, at least for the pthreads implementation.) */ 296 pthread_atfork mechanism, at least for the pthreads implementation.) */
374 297
375 void 298 void
376 PyEval_ReInitThreads(void) 299 PyEval_ReInitThreads(void)
377 { 300 {
378 _Py_IDENTIFIER(_after_fork);
379 PyObject *threading, *result; 301 PyObject *threading, *result;
380 PyThreadState *current_tstate = PyThreadState_GET(); 302 PyThreadState *tstate;
381 303
382 if (!gil_created()) 304 if (!interpreter_lock)
383 return; 305 return;
384 recreate_gil(); 306 /*XXX Can't use PyThread_free_lock here because it does too
307 much error-checking. Doing this cleanly would require
308 adding a new function to each thread_*.h. Instead, just
309 create a new lock and waste a little bit of memory */
310 interpreter_lock = PyThread_allocate_lock();
385 pending_lock = PyThread_allocate_lock(); 311 pending_lock = PyThread_allocate_lock();
386 take_gil(current_tstate); 312 PyThread_acquire_lock(interpreter_lock, 1);
387 main_thread = PyThread_get_thread_ident(); 313 main_thread = PyThread_get_thread_ident();
388 314
389 /* Update the threading module with the new state. 315 /* Update the threading module with the new state.
390 */ 316 */
391 threading = PyMapping_GetItemString(current_tstate->interp->modules, 317 tstate = PyThreadState_GET();
318 threading = PyMapping_GetItemString(tstate->interp->modules,
392 "threading"); 319 "threading");
393 if (threading == NULL) { 320 if (threading == NULL) {
394 /* threading not imported */ 321 /* threading not imported */
395 PyErr_Clear(); 322 PyErr_Clear();
396 return; 323 return;
397 } 324 }
398 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL); 325 result = PyObject_CallMethod(threading, "_after_fork", NULL);
399 if (result == NULL) 326 if (result == NULL)
400 PyErr_WriteUnraisable(threading); 327 PyErr_WriteUnraisable(threading);
401 else 328 else
402 Py_DECREF(result); 329 Py_DECREF(result);
403 Py_DECREF(threading); 330 Py_DECREF(threading);
404 331 }
405 /* Destroy all threads except the current one */ 332 #endif
406 _PyThreadState_DeleteExcept(current_tstate);
407 }
408
409 #else
410 static _Py_atomic_int eval_breaker = {0};
411 static int pending_async_exc = 0;
412 #endif /* WITH_THREAD */
413
414 /* This function is used to signal that async exceptions are waiting to be
415 raised, therefore it is also useful in non-threaded builds. */
416
417 void
418 _PyEval_SignalAsyncExc(void)
419 {
420 SIGNAL_ASYNC_EXC();
421 }
422 333
423 /* Functions save_thread and restore_thread are always defined so 334 /* Functions save_thread and restore_thread are always defined so
424 dynamically loaded modules needn't be compiled separately for use 335 dynamically loaded modules needn't be compiled separately for use
425 with and without threads: */ 336 with and without threads: */
426 337
427 PyThreadState * 338 PyThreadState *
428 PyEval_SaveThread(void) 339 PyEval_SaveThread(void)
429 { 340 {
430 PyThreadState *tstate = PyThreadState_Swap(NULL); 341 PyThreadState *tstate = PyThreadState_Swap(NULL);
431 if (tstate == NULL) 342 if (tstate == NULL)
432 Py_FatalError("PyEval_SaveThread: NULL tstate"); 343 Py_FatalError("PyEval_SaveThread: NULL tstate");
433 #ifdef WITH_THREAD 344 #ifdef WITH_THREAD
434 if (gil_created()) 345 if (interpreter_lock)
435 drop_gil(tstate); 346 PyThread_release_lock(interpreter_lock);
436 #endif 347 #endif
437 return tstate; 348 return tstate;
438 } 349 }
439 350
440 void 351 void
441 PyEval_RestoreThread(PyThreadState *tstate) 352 PyEval_RestoreThread(PyThreadState *tstate)
442 { 353 {
443 if (tstate == NULL) 354 if (tstate == NULL)
444 Py_FatalError("PyEval_RestoreThread: NULL tstate"); 355 Py_FatalError("PyEval_RestoreThread: NULL tstate");
445 #ifdef WITH_THREAD 356 #ifdef WITH_THREAD
446 if (gil_created()) { 357 if (interpreter_lock) {
447 int err = errno; 358 int err = errno;
448 take_gil(tstate); 359 PyThread_acquire_lock(interpreter_lock, 1);
449 /* _Py_Finalizing is protected by the GIL */
450 if (_Py_Finalizing && tstate != _Py_Finalizing) {
451 drop_gil(tstate);
452 PyThread_exit_thread();
453 assert(0); /* unreachable */
454 }
455 errno = err; 360 errno = err;
456 } 361 }
457 #endif 362 #endif
458 PyThreadState_Swap(tstate); 363 PyThreadState_Swap(tstate);
459 } 364 }
460 365
461 366
462 /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX 367 /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
463 signal handlers or Mac I/O completion routines) can schedule calls 368 signal handlers or Mac I/O completion routines) can schedule calls
464 to a function to be called synchronously. 369 to a function to be called synchronously.
(...skipping 25 matching lines...) Expand all
490 callback. 395 callback.
491 */ 396 */
492 397
493 #define NPENDINGCALLS 32 398 #define NPENDINGCALLS 32
494 static struct { 399 static struct {
495 int (*func)(void *); 400 int (*func)(void *);
496 void *arg; 401 void *arg;
497 } pendingcalls[NPENDINGCALLS]; 402 } pendingcalls[NPENDINGCALLS];
498 static int pendingfirst = 0; 403 static int pendingfirst = 0;
499 static int pendinglast = 0; 404 static int pendinglast = 0;
405 static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
406 static char pendingbusy = 0;
500 407
501 int 408 int
502 Py_AddPendingCall(int (*func)(void *), void *arg) 409 Py_AddPendingCall(int (*func)(void *), void *arg)
503 { 410 {
504 int i, j, result=0; 411 int i, j, result=0;
505 PyThread_type_lock lock = pending_lock; 412 PyThread_type_lock lock = pending_lock;
506 413
507 /* try a few times for the lock. Since this mechanism is used 414 /* try a few times for the lock. Since this mechanism is used
508 * for signal handling (on the main thread), there is a (slim) 415 * for signal handling (on the main thread), there is a (slim)
509 * chance that a signal is delivered on the same thread while we 416 * chance that a signal is delivered on the same thread while we
(...skipping 17 matching lines...) Expand all
527 i = pendinglast; 434 i = pendinglast;
528 j = (i + 1) % NPENDINGCALLS; 435 j = (i + 1) % NPENDINGCALLS;
529 if (j == pendingfirst) { 436 if (j == pendingfirst) {
530 result = -1; /* Queue full */ 437 result = -1; /* Queue full */
531 } else { 438 } else {
532 pendingcalls[i].func = func; 439 pendingcalls[i].func = func;
533 pendingcalls[i].arg = arg; 440 pendingcalls[i].arg = arg;
534 pendinglast = j; 441 pendinglast = j;
535 } 442 }
536 /* signal main loop */ 443 /* signal main loop */
537 SIGNAL_PENDING_CALLS(); 444 _Py_Ticker = 0;
445 pendingcalls_to_do = 1;
538 if (lock != NULL) 446 if (lock != NULL)
539 PyThread_release_lock(lock); 447 PyThread_release_lock(lock);
540 return result; 448 return result;
541 } 449 }
542 450
543 int 451 int
544 Py_MakePendingCalls(void) 452 Py_MakePendingCalls(void)
545 { 453 {
546 static int busy = 0;
547 int i; 454 int i;
548 int r = 0; 455 int r = 0;
549 456
550 if (!pending_lock) { 457 if (!pending_lock) {
551 /* initial allocation of the lock */ 458 /* initial allocation of the lock */
552 pending_lock = PyThread_allocate_lock(); 459 pending_lock = PyThread_allocate_lock();
553 if (pending_lock == NULL) 460 if (pending_lock == NULL)
554 return -1; 461 return -1;
555 } 462 }
556 463
557 /* only service pending calls on main thread */ 464 /* only service pending calls on main thread */
558 if (main_thread && PyThread_get_thread_ident() != main_thread) 465 if (main_thread && PyThread_get_thread_ident() != main_thread)
559 return 0; 466 return 0;
560 /* don't perform recursive pending calls */ 467 /* don't perform recursive pending calls */
561 if (busy) 468 if (pendingbusy)
562 return 0; 469 return 0;
563 busy = 1; 470 pendingbusy = 1;
564 /* perform a bounded number of calls, in case of recursion */ 471 /* perform a bounded number of calls, in case of recursion */
565 for (i=0; i<NPENDINGCALLS; i++) { 472 for (i=0; i<NPENDINGCALLS; i++) {
566 int j; 473 int j;
567 int (*func)(void *); 474 int (*func)(void *);
568 void *arg = NULL; 475 void *arg = NULL;
569 476
570 /* pop one item off the queue while holding the lock */ 477 /* pop one item off the queue while holding the lock */
571 PyThread_acquire_lock(pending_lock, WAIT_LOCK); 478 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
572 j = pendingfirst; 479 j = pendingfirst;
573 if (j == pendinglast) { 480 if (j == pendinglast) {
574 func = NULL; /* Queue empty */ 481 func = NULL; /* Queue empty */
575 } else { 482 } else {
576 func = pendingcalls[j].func; 483 func = pendingcalls[j].func;
577 arg = pendingcalls[j].arg; 484 arg = pendingcalls[j].arg;
578 pendingfirst = (j + 1) % NPENDINGCALLS; 485 pendingfirst = (j + 1) % NPENDINGCALLS;
579 } 486 }
580 if (pendingfirst != pendinglast) 487 pendingcalls_to_do = pendingfirst != pendinglast;
581 SIGNAL_PENDING_CALLS();
582 else
583 UNSIGNAL_PENDING_CALLS();
584 PyThread_release_lock(pending_lock); 488 PyThread_release_lock(pending_lock);
585 /* having released the lock, perform the callback */ 489 /* having released the lock, perform the callback */
586 if (func == NULL) 490 if (func == NULL)
587 break; 491 break;
588 r = func(arg); 492 r = func(arg);
589 if (r) 493 if (r)
590 break; 494 break;
591 } 495 }
592 busy = 0; 496 pendingbusy = 0;
593 return r; 497 return r;
594 } 498 }
595 499
596 #else /* if ! defined WITH_THREAD */ 500 #else /* if ! defined WITH_THREAD */
597 501
598 /* 502 /*
599 WARNING! ASYNCHRONOUSLY EXECUTING CODE! 503 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
600 This code is used for signal handling in python that isn't built 504 This code is used for signal handling in python that isn't built
601 with WITH_THREAD. 505 with WITH_THREAD.
602 Don't use this implementation when Py_AddPendingCalls() can happen 506 Don't use this implementation when Py_AddPendingCalls() can happen
(...skipping 15 matching lines...) Expand all
618 The two threads could theoretically wiggle around the "busy" variable. 522 The two threads could theoretically wiggle around the "busy" variable.
619 */ 523 */
620 524
621 #define NPENDINGCALLS 32 525 #define NPENDINGCALLS 32
622 static struct { 526 static struct {
623 int (*func)(void *); 527 int (*func)(void *);
624 void *arg; 528 void *arg;
625 } pendingcalls[NPENDINGCALLS]; 529 } pendingcalls[NPENDINGCALLS];
626 static volatile int pendingfirst = 0; 530 static volatile int pendingfirst = 0;
627 static volatile int pendinglast = 0; 531 static volatile int pendinglast = 0;
628 static _Py_atomic_int pendingcalls_to_do = {0}; 532 static volatile int pendingcalls_to_do = 0;
629 533
630 int 534 int
631 Py_AddPendingCall(int (*func)(void *), void *arg) 535 Py_AddPendingCall(int (*func)(void *), void *arg)
632 { 536 {
633 static volatile int busy = 0; 537 static volatile int busy = 0;
634 int i, j; 538 int i, j;
635 /* XXX Begin critical section */ 539 /* XXX Begin critical section */
636 if (busy) 540 if (busy)
637 return -1; 541 return -1;
638 busy = 1; 542 busy = 1;
639 i = pendinglast; 543 i = pendinglast;
640 j = (i + 1) % NPENDINGCALLS; 544 j = (i + 1) % NPENDINGCALLS;
641 if (j == pendingfirst) { 545 if (j == pendingfirst) {
642 busy = 0; 546 busy = 0;
643 return -1; /* Queue full */ 547 return -1; /* Queue full */
644 } 548 }
645 pendingcalls[i].func = func; 549 pendingcalls[i].func = func;
646 pendingcalls[i].arg = arg; 550 pendingcalls[i].arg = arg;
647 pendinglast = j; 551 pendinglast = j;
648 552
649 SIGNAL_PENDING_CALLS(); 553 _Py_Ticker = 0;
554 pendingcalls_to_do = 1; /* Signal main loop */
650 busy = 0; 555 busy = 0;
651 /* XXX End critical section */ 556 /* XXX End critical section */
652 return 0; 557 return 0;
653 } 558 }
654 559
655 int 560 int
656 Py_MakePendingCalls(void) 561 Py_MakePendingCalls(void)
657 { 562 {
658 static int busy = 0; 563 static int busy = 0;
659 if (busy) 564 if (busy)
660 return 0; 565 return 0;
661 busy = 1; 566 busy = 1;
662 UNSIGNAL_PENDING_CALLS(); 567 pendingcalls_to_do = 0;
663 for (;;) { 568 for (;;) {
664 int i; 569 int i;
665 int (*func)(void *); 570 int (*func)(void *);
666 void *arg; 571 void *arg;
667 i = pendingfirst; 572 i = pendingfirst;
668 if (i == pendinglast) 573 if (i == pendinglast)
669 break; /* Queue empty */ 574 break; /* Queue empty */
670 func = pendingcalls[i].func; 575 func = pendingcalls[i].func;
671 arg = pendingcalls[i].arg; 576 arg = pendingcalls[i].arg;
672 pendingfirst = (i + 1) % NPENDINGCALLS; 577 pendingfirst = (i + 1) % NPENDINGCALLS;
673 if (func(arg) < 0) { 578 if (func(arg) < 0) {
674 busy = 0; 579 busy = 0;
675 SIGNAL_PENDING_CALLS(); /* We're not done yet */ 580 pendingcalls_to_do = 1; /* We're not done yet */
676 return -1; 581 return -1;
677 } 582 }
678 } 583 }
679 busy = 0; 584 busy = 0;
680 return 0; 585 return 0;
681 } 586 }
682 587
683 #endif /* WITH_THREAD */ 588 #endif /* WITH_THREAD */
684 589
685 590
(...skipping 17 matching lines...) Expand all
703 recursion_limit = new_limit; 608 recursion_limit = new_limit;
704 _Py_CheckRecursionLimit = recursion_limit; 609 _Py_CheckRecursionLimit = recursion_limit;
705 } 610 }
706 611
707 /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() 612 /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
708 if the recursion_depth reaches _Py_CheckRecursionLimit. 613 if the recursion_depth reaches _Py_CheckRecursionLimit.
709 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit 614 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
710 to guarantee that _Py_CheckRecursiveCall() is regularly called. 615 to guarantee that _Py_CheckRecursiveCall() is regularly called.
711 Without USE_STACKCHECK, there is no need for this. */ 616 Without USE_STACKCHECK, there is no need for this. */
712 int 617 int
713 _Py_CheckRecursiveCall(char *where) 618 _Py_CheckRecursiveCall(const char *where)
714 { 619 {
715 PyThreadState *tstate = PyThreadState_GET(); 620 PyThreadState *tstate = PyThreadState_GET();
716 621
717 #ifdef USE_STACKCHECK 622 #ifdef USE_STACKCHECK
718 if (PyOS_CheckStack()) { 623 if (PyOS_CheckStack()) {
719 --tstate->recursion_depth; 624 --tstate->recursion_depth;
720 PyErr_SetString(PyExc_MemoryError, "Stack overflow"); 625 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
721 return -1; 626 return -1;
722 } 627 }
723 #endif 628 #endif
724 _Py_CheckRecursionLimit = recursion_limit;
725 if (tstate->recursion_critical)
726 /* Somebody asked that we don't check for recursion. */
727 return 0;
728 if (tstate->overflowed) {
729 if (tstate->recursion_depth > recursion_limit + 50) {
730 /* Overflowing while handling an overflow. Give up. */
731 Py_FatalError("Cannot recover from stack overflow.");
732 }
733 return 0;
734 }
735 if (tstate->recursion_depth > recursion_limit) { 629 if (tstate->recursion_depth > recursion_limit) {
736 --tstate->recursion_depth; 630 --tstate->recursion_depth;
737 tstate->overflowed = 1;
738 PyErr_Format(PyExc_RuntimeError, 631 PyErr_Format(PyExc_RuntimeError,
739 "maximum recursion depth exceeded%s", 632 "maximum recursion depth exceeded%s",
740 where); 633 where);
741 return -1; 634 return -1;
742 } 635 }
636 _Py_CheckRecursionLimit = recursion_limit;
743 return 0; 637 return 0;
744 } 638 }
745 639
746 /* Status code for main loop (reason for stack unwind) */ 640 /* Status code for main loop (reason for stack unwind) */
747 enum why_code { 641 enum why_code {
748 WHY_NOT = 0x0001, /* No error */ 642 WHY_NOT = 0x0001, /* No error */
749 WHY_EXCEPTION = 0x0002, /* Exception occurred */ 643 WHY_EXCEPTION = 0x0002, /* Exception occurred */
644 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
750 WHY_RETURN = 0x0008, /* 'return' statement */ 645 WHY_RETURN = 0x0008, /* 'return' statement */
751 WHY_BREAK = 0x0010, /* 'break' statement */ 646 WHY_BREAK = 0x0010, /* 'break' statement */
752 WHY_CONTINUE = 0x0020, /* 'continue' statement */ 647 WHY_CONTINUE = 0x0020, /* 'continue' statement */
753 WHY_YIELD = 0x0040, /* 'yield' operator */ 648 WHY_YIELD = 0x0040 /* 'yield' operator */
754 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
755 }; 649 };
756 650
757 static void save_exc_state(PyThreadState *, PyFrameObject *); 651 static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
758 static void swap_exc_state(PyThreadState *, PyFrameObject *); 652 static int unpack_iterable(PyObject *, int, PyObject **);
759 static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
760 static int do_raise(PyObject *, PyObject *);
761 static int unpack_iterable(PyObject *, int, int, PyObject **);
762 653
763 /* Records whether tracing is on for any thread. Counts the number of 654 /* Records whether tracing is on for any thread. Counts the number of
764 threads for which tstate->c_tracefunc is non-NULL, so if the value 655 threads for which tstate->c_tracefunc is non-NULL, so if the value
765 is 0, we know we don't have to check this thread's c_tracefunc. 656 is 0, we know we don't have to check this thread's c_tracefunc.
766 This speeds up the if statement in PyEval_EvalFrameEx() after 657 This speeds up the if statement in PyEval_EvalFrameEx() after
767 fast_next_opcode*/ 658 fast_next_opcode*/
768 static int _Py_TracingPossible = 0; 659 static int _Py_TracingPossible = 0;
769 660
770 661 /* for manipulating the thread switch and periodic "stuff" - used to be
662 per thread, now just a pair o' globals */
663 int _Py_CheckInterval = 100;
664 volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
771 665
772 PyObject * 666 PyObject *
773 PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) 667 PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
774 { 668 {
775 return PyEval_EvalCodeEx(co, 669 return PyEval_EvalCodeEx(co,
776 globals, locals, 670 globals, locals,
777 (PyObject **)NULL, 0, 671 (PyObject **)NULL, 0,
778 (PyObject **)NULL, 0, 672 (PyObject **)NULL, 0,
779 (PyObject **)NULL, 0, 673 (PyObject **)NULL, 0,
780 NULL, NULL); 674 NULL);
781 } 675 }
782 676
783 677
784 /* Interpreter main loop */ 678 /* Interpreter main loop */
785 679
786 PyObject * 680 PyObject *
787 PyEval_EvalFrame(PyFrameObject *f) { 681 PyEval_EvalFrame(PyFrameObject *f) {
788 /* This is for backward compatibility with extension modules that 682 /* This is for backward compatibility with extension modules that
789 used this API; core interpreter code should call 683 used this API; core interpreter code should call
790 PyEval_EvalFrameEx() */ 684 PyEval_EvalFrameEx() */
791 return PyEval_EvalFrameEx(f, 0); 685 return PyEval_EvalFrameEx(f, 0);
792 } 686 }
793 687
794 PyObject * 688 PyObject *
795 PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) 689 PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
796 { 690 {
797 #ifdef DXPAIRS
798 int lastopcode = 0;
799 #endif
800 PyObject **stack_pointer; /* Next free slot in value stack */
801 unsigned char *next_instr;
802 int opcode; /* Current opcode */
803 int oparg; /* Current opcode argument, if any */
804 enum why_code why; /* Reason for block stack unwind */
805 PyObject **fastlocals, **freevars;
806 PyObject *retval = NULL; /* Return value */
807 PyThreadState *tstate = PyThreadState_GET();
808 PyCodeObject *co;
809
810 /* when tracing we set things up so that
811
812 not (instr_lb <= current_bytecode_offset < instr_ub)
813
814 is true when the line being executed has changed. The
815 initial values are such as to make this false the first
816 time it is tested. */
817 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
818
819 unsigned char *first_instr;
820 PyObject *names;
821 PyObject *consts;
822
823 #ifdef LLTRACE
824 _Py_IDENTIFIER(__ltrace__);
825 #endif
826
827 /* Computed GOTOs, or
828 the-optimization-commonly-but-improperly-known-as-"threaded code"
829 using gcc's labels-as-values extension
830 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
831
832 The traditional bytecode evaluation loop uses a "switch" statement, which
833 decent compilers will optimize as a single indirect branch instruction
834 combined with a lookup table of jump addresses. However, since the
835 indirect jump instruction is shared by all opcodes, the CPU will have a
836 hard time making the right prediction for where to jump next (actually,
837 it will be always wrong except in the uncommon case of a sequence of
838 several identical opcodes).
839
840 "Threaded code" in contrast, uses an explicit jump table and an explicit
841 indirect jump instruction at the end of each opcode. Since the jump
842 instruction is at a different address for each opcode, the CPU will make a
843 separate prediction for each of these instructions, which is equivalent to
844 predicting the second opcode of each opcode pair. These predictions have
845 a much better chance to turn out valid, especially in small bytecode loops.
846
847 A mispredicted branch on a modern CPU flushes the whole pipeline and
848 can cost several CPU cycles (depending on the pipeline depth),
849 and potentially many more instructions (depending on the pipeline width).
850 A correctly predicted branch, however, is nearly free.
851
852 At the time of this writing, the "threaded code" version is up to 15-20%
853 faster than the normal "switch" version, depending on the compiler and the
854 CPU architecture.
855
856 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
857 because it would render the measurements invalid.
858
859
860 NOTE: care must be taken that the compiler doesn't try to "optimize" the
861 indirect jumps by sharing them between all opcodes. Such optimizations
862 can be disabled on gcc by using the -fno-gcse flag (or possibly
863 -fno-crossjumping).
864 */
865
866 #ifdef DYNAMIC_EXECUTION_PROFILE 691 #ifdef DYNAMIC_EXECUTION_PROFILE
867 #undef USE_COMPUTED_GOTOS 692 #undef USE_COMPUTED_GOTOS
868 #define USE_COMPUTED_GOTOS 0 693 #endif
869 #endif
870
871 #ifdef HAVE_COMPUTED_GOTOS 694 #ifdef HAVE_COMPUTED_GOTOS
872 #ifndef USE_COMPUTED_GOTOS 695 #ifndef USE_COMPUTED_GOTOS
873 #define USE_COMPUTED_GOTOS 1 696 #define USE_COMPUTED_GOTOS 1
874 #endif 697 #endif
875 #else 698 #else
876 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS 699 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
877 #error "Computed gotos are not supported on this compiler." 700 #error "Computed gotos are not supported on this compiler."
878 #endif 701 #endif
879 #undef USE_COMPUTED_GOTOS 702 #undef USE_COMPUTED_GOTOS
880 #define USE_COMPUTED_GOTOS 0 703 #define USE_COMPUTED_GOTOS 0
881 #endif 704 #endif
882
883 #if USE_COMPUTED_GOTOS 705 #if USE_COMPUTED_GOTOS
884 /* Import the static jump table */ 706 /* Import the static jump table */
885 #include "opcode_targets.h" 707 #include "opcode_targets.h"
886 708
887 /* This macro is used when several opcodes defer to the same implementation 709 /* This macro is used when several opcodes defer to the same implementation
888 (e.g. SETUP_LOOP, SETUP_FINALLY) */ 710 (e.g. SETUP_LOOP, SETUP_FINALLY) */
889 #define TARGET_WITH_IMPL(op, impl) \ 711 #define TARGET_WITH_IMPL(op, impl) \
890 TARGET_##op: \ 712 TARGET_##op: \
891 opcode = op; \ 713 opcode = op; \
892 if (HAS_ARG(op)) \ 714 oparg = NEXTARG(); \
893 oparg = NEXTARG(); \ 715 case op: \
894 case op: \
895 goto impl; \ 716 goto impl; \
896 717
718 #define TARGET_WITH_IMPL_NOARG(op, impl) \
719 TARGET_##op: \
720 opcode = op; \
721 case op: \
722 goto impl; \
723
724 #define TARGET_NOARG(op) \
725 TARGET_##op: \
726 opcode = op; \
727 case op:\
728
897 #define TARGET(op) \ 729 #define TARGET(op) \
898 TARGET_##op: \ 730 TARGET_##op: \
899 opcode = op; \ 731 opcode = op; \
900 if (HAS_ARG(op)) \ 732 oparg = NEXTARG(); \
901 oparg = NEXTARG(); \ 733 case op:\
902 case op:
903 734
904 735
905 #define DISPATCH() \ 736 #define DISPATCH() \
906 { \ 737 { \
907 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \ 738 int _tick = _Py_Ticker - 1; \
908 FAST_DISPATCH(); \ 739 _Py_Ticker = _tick; \
909 } \ 740 if (_tick >= 0) { \
910 continue; \ 741 FAST_DISPATCH(); \
911 } 742 } \
743 continue; \
744 }
912 745
913 #ifdef LLTRACE 746 #ifdef LLTRACE
914 #define FAST_DISPATCH() \ 747 #define FAST_DISPATCH() \
915 { \ 748 { \
916 if (!lltrace && !_Py_TracingPossible) { \ 749 if (!lltrace && !_Py_TracingPossible) { \
917 f->f_lasti = INSTR_OFFSET(); \ 750 f->f_lasti = INSTR_OFFSET(); \
918 goto *opcode_targets[*next_instr++]; \ 751 goto *opcode_targets[*next_instr++]; \
919 } \ 752 } \
920 goto fast_next_opcode; \ 753 goto fast_next_opcode; \
921 } 754 }
922 #else 755 #else
923 #define FAST_DISPATCH() \ 756 #define FAST_DISPATCH() { \
924 { \
925 if (!_Py_TracingPossible) { \ 757 if (!_Py_TracingPossible) { \
926 f->f_lasti = INSTR_OFFSET(); \ 758 f->f_lasti = INSTR_OFFSET(); \
927 goto *opcode_targets[*next_instr++]; \ 759 goto *opcode_targets[*next_instr++]; \
928 } \ 760 } \
929 goto fast_next_opcode; \ 761 goto fast_next_opcode;\
930 } 762 }
931 #endif 763 #endif
932 764
933 #else 765 #else
934 #define TARGET(op) \ 766 #define TARGET(op) \
935 case op: 767 case op:
936 #define TARGET_WITH_IMPL(op, impl) \ 768 #define TARGET_WITH_IMPL(op, impl) \
937 /* silence compiler warnings about `impl` unused */ \ 769 /* silence compiler warnings about `impl` unused */ \
938 if (0) goto impl; \ 770 if (0) goto impl; \
939 case op: 771 case op:\
772
773 #define TARGET_NOARG(op) \
774 case op:\
775
776 #define TARGET_WITH_IMPL_NOARG(op, impl) \
777 if (0) goto impl; \
778 case op:\
779
940 #define DISPATCH() continue 780 #define DISPATCH() continue
941 #define FAST_DISPATCH() goto fast_next_opcode 781 #define FAST_DISPATCH() goto fast_next_opcode
942 #endif 782 #endif
943 783
784
785 #ifdef DXPAIRS
786 int lastopcode = 0;
787 #endif
788 register PyObject **stack_pointer; /* Next free slot in value stack */
789 register unsigned char *next_instr;
790 register int opcode; /* Current opcode */
791 register int oparg; /* Current opcode argument, if any */
792 register enum why_code why; /* Reason for block stack unwind */
793 register int err; /* Error status -- nonzero if error */
794 register PyObject *x; /* Result object -- NULL if error */
795 register PyObject *v; /* Temporary objects popped off stack */
796 register PyObject *w;
797 register PyObject *u;
798 register PyObject *t;
799 register PyObject *stream = NULL; /* for PRINT opcodes */
800 register PyObject **fastlocals, **freevars;
801 PyObject *retval = NULL; /* Return value */
802 PyThreadState *tstate = PyThreadState_GET();
803 PyCodeObject *co;
804
805 /* when tracing we set things up so that
806
807 not (instr_lb <= current_bytecode_offset < instr_ub)
808
809 is true when the line being executed has changed. The
810 initial values are such as to make this false the first
811 time it is tested. */
812 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
813
814 unsigned char *first_instr;
815 PyObject *names;
816 PyObject *consts;
817 #if defined(Py_DEBUG) || defined(LLTRACE)
818 /* Make it easier to find out where we are with a debugger */
819 char *filename;
820 #endif
944 821
945 /* Tuple access macros */ 822 /* Tuple access macros */
946 823
947 #ifndef Py_DEBUG 824 #ifndef Py_DEBUG
948 #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) 825 #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
949 #else 826 #else
950 #define GETITEM(v, i) PyTuple_GetItem((v), (i)) 827 #define GETITEM(v, i) PyTuple_GetItem((v), (i))
951 #endif 828 #endif
952 829
953 #ifdef WITH_TSC 830 #ifdef WITH_TSC
(...skipping 12 matching lines...) Expand all
966 calls another Python function; there's no point in charge all the 843 calls another Python function; there's no point in charge all the
967 bytecode executed by the called function to the caller. 844 bytecode executed by the called function to the caller.
968 845
969 It's hard to make a useful judgement statically. In the presence 846 It's hard to make a useful judgement statically. In the presence
970 of operator overloading, it's impossible to tell if a call will 847 of operator overloading, it's impossible to tell if a call will
971 execute new Python code or not. 848 execute new Python code or not.
972 849
973 It's a case-by-case judgement. I'll use intr1 for the following 850 It's a case-by-case judgement. I'll use intr1 for the following
974 cases: 851 cases:
975 852
853 EXEC_STMT
976 IMPORT_STAR 854 IMPORT_STAR
977 IMPORT_FROM 855 IMPORT_FROM
978 CALL_FUNCTION (and friends) 856 CALL_FUNCTION (and friends)
979 857
980 */ 858 */
981 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0; 859 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
982 int ticked = 0; 860 int ticked = 0;
983 861
984 READ_TIMESTAMP(inst0); 862 READ_TIMESTAMP(inst0);
985 READ_TIMESTAMP(inst1); 863 READ_TIMESTAMP(inst1);
986 READ_TIMESTAMP(loop0); 864 READ_TIMESTAMP(loop0);
987 READ_TIMESTAMP(loop1); 865 READ_TIMESTAMP(loop1);
988 866
989 /* shut up the compiler */ 867 /* shut up the compiler */
990 opcode = 0; 868 opcode = 0;
991 #endif 869 #endif
992 870
993 /* Code access macros */ 871 /* Code access macros */
994 872
995 #define INSTR_OFFSET() ((int)(next_instr - first_instr)) 873 #define INSTR_OFFSET() ((int)(next_instr - first_instr))
996 #define NEXTOP() (*next_instr++) 874 #define NEXTOP() (*next_instr++)
997 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2]) 875 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
998 #define PEEKARG() ((next_instr[2]<<8) + next_instr[1]) 876 #define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
999 #define JUMPTO(x) (next_instr = first_instr + (x)) 877 #define JUMPTO(x) (next_instr = first_instr + (x))
1000 #define JUMPBY(x) (next_instr += (x)) 878 #define JUMPBY(x) (next_instr += (x))
1001 879
1002 /* OpCode prediction macros 880 /* OpCode prediction macros
1003 Some opcodes tend to come in pairs thus making it possible to 881 Some opcodes tend to come in pairs thus making it possible to
1004 predict the second code when the first is run. For example, 882 predict the second code when the first is run. For example,
1005 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, 883 GET_ITER is often followed by FOR_ITER. And FOR_ITER is often
1006 those opcodes are often followed by a POP_TOP. 884 followed by STORE_FAST or UNPACK_SEQUENCE.
1007 885
1008 Verifying the prediction costs a single high-speed test of a register 886 Verifying the prediction costs a single high-speed test of a register
1009 variable against a constant. If the pairing was good, then the 887 variable against a constant. If the pairing was good, then the
1010 processor's own internal branch predication has a high likelihood of 888 processor's own internal branch predication has a high likelihood of
1011 success, resulting in a nearly zero-overhead transition to the 889 success, resulting in a nearly zero-overhead transition to the
1012 next opcode. A successful prediction saves a trip through the eval-loop 890 next opcode. A successful prediction saves a trip through the eval-loop
1013 including its two unpredictable branches, the HAS_ARG test and the 891 including its two unpredictable branches, the HAS_ARG test and the
1014 switch-case. Combined with the processor's internal branch prediction, 892 switch-case. Combined with the processor's internal branch prediction,
1015 a successful PREDICT has the effect of making the two opcodes run as if 893 a successful PREDICT has the effect of making the two opcodes run as if
1016 they were a single new opcode with the bodies combined. 894 they were a single new opcode with the bodies combined.
1017 895
1018 If collecting opcode statistics, your choices are to either keep the 896 If collecting opcode statistics, your choices are to either keep the
1019 predictions turned-on and interpret the results as if some opcodes 897 predictions turned-on and interpret the results as if some opcodes
1020 had been combined or turn-off predictions so that the opcode frequency 898 had been combined or turn-off predictions so that the opcode frequency
1021 counter updates for both opcodes. 899 counter updates for both opcodes.
1022
1023 Opcode prediction is disabled with threaded code, since the latter allows
1024 the CPU to record separate branch prediction information for each
1025 opcode.
1026
1027 */ 900 */
901
1028 902
1029 #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS 903 #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
1030 #define PREDICT(op) if (0) goto PRED_##op 904 #define PREDICT(op) if (0) goto PRED_##op
1031 #define PREDICTED(op) PRED_##op: 905 #define PREDICTED(op) PRED_##op:
1032 #define PREDICTED_WITH_ARG(op) PRED_##op: 906 #define PREDICTED_WITH_ARG(op) PRED_##op:
1033 #else 907 #else
1034 #define PREDICT(op) if (*next_instr == op) goto PRED_##op 908 #define PREDICT(op) if (*next_instr == op) goto PRED_##op
1035 #define PREDICTED(op) PRED_##op: next_instr++ 909 #define PREDICTED(op) PRED_##op: next_instr++
1036 #define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3 910 #define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
1037 #endif 911 #endif
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1083 /* The SETLOCAL() macro must not DECREF the local variable in-place and 957 /* The SETLOCAL() macro must not DECREF the local variable in-place and
1084 then store the new value; it must copy the old value to a temporary 958 then store the new value; it must copy the old value to a temporary
1085 value, then store the new value, and then DECREF the temporary value. 959 value, then store the new value, and then DECREF the temporary value.
1086 This is because it is possible that during the DECREF the frame is 960 This is because it is possible that during the DECREF the frame is
1087 accessed by other code (e.g. a __del__ method or gc.collect()) and the 961 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1088 variable would be pointing to already-freed memory. */ 962 variable would be pointing to already-freed memory. */
1089 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ 963 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
1090 GETLOCAL(i) = value; \ 964 GETLOCAL(i) = value; \
1091 Py_XDECREF(tmp); } while (0) 965 Py_XDECREF(tmp); } while (0)
1092 966
1093
1094 #define UNWIND_BLOCK(b) \
1095 while (STACK_LEVEL() > (b)->b_level) { \
1096 PyObject *v = POP(); \
1097 Py_XDECREF(v); \
1098 }
1099
1100 #define UNWIND_EXCEPT_HANDLER(b) \
1101 { \
1102 PyObject *type, *value, *traceback; \
1103 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1104 while (STACK_LEVEL() > (b)->b_level + 3) { \
1105 value = POP(); \
1106 Py_XDECREF(value); \
1107 } \
1108 type = tstate->exc_type; \
1109 value = tstate->exc_value; \
1110 traceback = tstate->exc_traceback; \
1111 tstate->exc_type = POP(); \
1112 tstate->exc_value = POP(); \
1113 tstate->exc_traceback = POP(); \
1114 Py_XDECREF(type); \
1115 Py_XDECREF(value); \
1116 Py_XDECREF(traceback); \
1117 }
1118
1119 /* Start of code */ 967 /* Start of code */
968
969 if (f == NULL)
970 return NULL;
1120 971
1121 /* push frame */ 972 /* push frame */
1122 if (Py_EnterRecursiveCall("")) 973 if (Py_EnterRecursiveCall(""))
1123 return NULL; 974 return NULL;
1124 975
1125 tstate->frame = f; 976 tstate->frame = f;
1126 977
1127 if (tstate->use_tracing) { 978 if (tstate->use_tracing) {
1128 if (tstate->c_tracefunc != NULL) { 979 if (tstate->c_tracefunc != NULL) {
1129 /* tstate->c_tracefunc, if defined, is a 980 /* tstate->c_tracefunc, if defined, is a
1130 function that will be called on *every* entry 981 function that will be called on *every* entry
1131 to a code block. Its return value, if not 982 to a code block. Its return value, if not
1132 None, is a function that will be called at 983 None, is a function that will be called at
1133 the start of each executed line of code. 984 the start of each executed line of code.
1134 (Actually, the function must return itself 985 (Actually, the function must return itself
1135 in order to continue tracing.) The trace 986 in order to continue tracing.) The trace
1136 functions are called with three arguments: 987 functions are called with three arguments:
1137 a pointer to the current frame, a string 988 a pointer to the current frame, a string
1138 indicating why the function is called, and 989 indicating why the function is called, and
1139 an argument which depends on the situation. 990 an argument which depends on the situation.
1140 The global trace function is also called 991 The global trace function is also called
1141 whenever an exception is detected. */ 992 whenever an exception is detected. */
1142 if (call_trace_protected(tstate->c_tracefunc, 993 if (call_trace_protected(tstate->c_tracefunc,
1143 tstate->c_traceobj, 994 tstate->c_traceobj,
1144 tstate, f, PyTrace_CALL, Py_None)) { 995 f, PyTrace_CALL, Py_None)) {
1145 /* Trace function raised an error */ 996 /* Trace function raised an error */
1146 goto exit_eval_frame; 997 goto exit_eval_frame;
1147 } 998 }
1148 } 999 }
1149 if (tstate->c_profilefunc != NULL) { 1000 if (tstate->c_profilefunc != NULL) {
1150 /* Similar for c_profilefunc, except it needn't 1001 /* Similar for c_profilefunc, except it needn't
1151 return itself and isn't called for "line" events */ 1002 return itself and isn't called for "line" events */
1152 if (call_trace_protected(tstate->c_profilefunc, 1003 if (call_trace_protected(tstate->c_profilefunc,
1153 tstate->c_profileobj, 1004 tstate->c_profileobj,
1154 tstate, f, PyTrace_CALL, Py_None)) { 1005 f, PyTrace_CALL, Py_None)) {
1155 /* Profile function raised an error */ 1006 /* Profile function raised an error */
1156 goto exit_eval_frame; 1007 goto exit_eval_frame;
1157 } 1008 }
1158 } 1009 }
1159 } 1010 }
1160 1011
1161 co = f->f_code; 1012 co = f->f_code;
1162 names = co->co_names; 1013 names = co->co_names;
1163 consts = co->co_consts; 1014 consts = co->co_consts;
1164 fastlocals = f->f_localsplus; 1015 fastlocals = f->f_localsplus;
1165 freevars = f->f_localsplus + co->co_nlocals; 1016 freevars = f->f_localsplus + co->co_nlocals;
1166 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code); 1017 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
1167 /* An explanation is in order for the next line. 1018 /* An explanation is in order for the next line.
1168 1019
1169 f->f_lasti now refers to the index of the last instruction 1020 f->f_lasti now refers to the index of the last instruction
1170 executed. You might think this was obvious from the name, but 1021 executed. You might think this was obvious from the name, but
1171 this wasn't always true before 2.3! PyFrame_New now sets 1022 this wasn't always true before 2.3! PyFrame_New now sets
1172 f->f_lasti to -1 (i.e. the index *before* the first instruction) 1023 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1173 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this 1024 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1174 does work. Promise. 1025 does work. Promise.
1175 YIELD_FROM sets f_lasti to itself, in order to repeated yield
1176 multiple values.
1177 1026
1178 When the PREDICT() macros are enabled, some opcode pairs follow in 1027 When the PREDICT() macros are enabled, some opcode pairs follow in
1179 direct succession without updating f->f_lasti. A successful 1028 direct succession without updating f->f_lasti. A successful
1180 prediction effectively links the two codes together as if they 1029 prediction effectively links the two codes together as if they
1181 were a single new opcode; accordingly,f->f_lasti will point to 1030 were a single new opcode; accordingly,f->f_lasti will point to
1182 the first code in the pair (for instance, GET_ITER followed by 1031 the first code in the pair (for instance, GET_ITER followed by
1183 FOR_ITER is effectively a single opcode and f->f_lasti will point 1032 FOR_ITER is effectively a single opcode and f->f_lasti will point
1184 at to the beginning of the combined pair.) 1033 at to the beginning of the combined pair.)
1185 */ 1034 */
1186 next_instr = first_instr + f->f_lasti + 1; 1035 next_instr = first_instr + f->f_lasti + 1;
1187 stack_pointer = f->f_stacktop; 1036 stack_pointer = f->f_stacktop;
1188 assert(stack_pointer != NULL); 1037 assert(stack_pointer != NULL);
1189 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ 1038 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
1190 f->f_executing = 1;
1191
1192 if (co->co_flags & CO_GENERATOR) {
1193 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1194 /* We were in an except handler when we left,
1195 restore the exception state which was put aside
1196 (see YIELD_VALUE). */
1197 swap_exc_state(tstate, f);
1198 }
1199 else
1200 save_exc_state(tstate, f);
1201 }
1202 1039
1203 #ifdef LLTRACE 1040 #ifdef LLTRACE
1204 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL; 1041 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
1042 #endif
1043 #if defined(Py_DEBUG) || defined(LLTRACE)
1044 filename = PyString_AsString(co->co_filename);
1205 #endif 1045 #endif
1206 1046
1207 why = WHY_NOT; 1047 why = WHY_NOT;
1208 1048 err = 0;
1209 if (throwflag) /* support for generator.throw() */ 1049 x = Py_None; /* Not a reference, just anything non-NULL */
1210 goto error; 1050 w = NULL;
1211 1051
1212 #ifdef Py_DEBUG 1052 if (throwflag) { /* support for generator.throw() */
1213 /* PyEval_EvalFrameEx() must not be called with an exception set, 1053 why = WHY_EXCEPTION;
1214 because it may clear it (directly or indirectly) and so the 1054 goto on_error;
1215 caller looses its exception */ 1055 }
1216 assert(!PyErr_Occurred());
1217 #endif
1218 1056
1219 for (;;) { 1057 for (;;) {
1220 #ifdef WITH_TSC 1058 #ifdef WITH_TSC
1221 if (inst1 == 0) { 1059 if (inst1 == 0) {
1222 /* Almost surely, the opcode executed a break 1060 /* Almost surely, the opcode executed a break
1223 or a continue, preventing inst1 from being set 1061 or a continue, preventing inst1 from being set
1224 on the way out of the loop. 1062 on the way out of the loop.
1225 */ 1063 */
1226 READ_TIMESTAMP(inst1); 1064 READ_TIMESTAMP(inst1);
1227 loop1 = inst1; 1065 loop1 = inst1;
1228 } 1066 }
1229 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1, 1067 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1230 intr0, intr1); 1068 intr0, intr1);
1231 ticked = 0; 1069 ticked = 0;
1232 inst1 = 0; 1070 inst1 = 0;
1233 intr0 = 0; 1071 intr0 = 0;
1234 intr1 = 0; 1072 intr1 = 0;
1235 READ_TIMESTAMP(loop0); 1073 READ_TIMESTAMP(loop0);
1236 #endif 1074 #endif
1237 assert(stack_pointer >= f->f_valuestack); /* else underflow */ 1075 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1238 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ 1076 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
1239 assert(!PyErr_Occurred());
1240 1077
1241 /* Do periodic things. Doing this every time through 1078 /* Do periodic things. Doing this every time through
1242 the loop would add too much overhead, so we do it 1079 the loop would add too much overhead, so we do it
1243 only every Nth instruction. We also do it if 1080 only every Nth instruction. We also do it if
1244 ``pendingcalls_to_do'' is set, i.e. when an asynchronous 1081 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1245 event needs attention (e.g. a signal handler or 1082 event needs attention (e.g. a signal handler or
1246 async I/O handler); see Py_AddPendingCall() and 1083 async I/O handler); see Py_AddPendingCall() and
1247 Py_MakePendingCalls() above. */ 1084 Py_MakePendingCalls() above. */
1248 1085
1249 if (_Py_atomic_load_relaxed(&eval_breaker)) { 1086 if (--_Py_Ticker < 0) {
1250 if (*next_instr == SETUP_FINALLY) { 1087 if (*next_instr == SETUP_FINALLY) {
1251 /* Make the last opcode before 1088 /* Make the last opcode before
1252 a try: finally: block uninterruptible. */ 1089 a try: finally: block uninterruptible. */
1253 goto fast_next_opcode; 1090 goto fast_next_opcode;
1254 } 1091 }
1092 _Py_Ticker = _Py_CheckInterval;
1093 tstate->tick_counter++;
1255 #ifdef WITH_TSC 1094 #ifdef WITH_TSC
1256 ticked = 1; 1095 ticked = 1;
1257 #endif 1096 #endif
1258 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) { 1097 if (pendingcalls_to_do) {
1259 if (Py_MakePendingCalls() < 0) 1098 if (Py_MakePendingCalls() < 0) {
1260 goto error; 1099 why = WHY_EXCEPTION;
1100 goto on_error;
1101 }
1102 if (pendingcalls_to_do)
1103 /* MakePendingCalls() didn't succeed.
1104 Force early re-execution of this
1105 "periodic" code, possibly after
1106 a thread switch */
1107 _Py_Ticker = 0;
1261 } 1108 }
1262 #ifdef WITH_THREAD 1109 #ifdef WITH_THREAD
1263 if (_Py_atomic_load_relaxed(&gil_drop_request)) { 1110 if (interpreter_lock) {
1264 /* Give another thread a chance */ 1111 /* Give another thread a chance */
1112
1265 if (PyThreadState_Swap(NULL) != tstate) 1113 if (PyThreadState_Swap(NULL) != tstate)
1266 Py_FatalError("ceval: tstate mix-up"); 1114 Py_FatalError("ceval: tstate mix-up");
1267 drop_gil(tstate); 1115 PyThread_release_lock(interpreter_lock);
1268 1116
1269 /* Other threads may run now */ 1117 /* Other threads may run now */
1270 1118
1271 take_gil(tstate); 1119 PyThread_acquire_lock(interpreter_lock, 1);
1272
1273 /* Check if we should make a quick exit. */
1274 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1275 drop_gil(tstate);
1276 PyThread_exit_thread();
1277 }
1278 1120
1279 if (PyThreadState_Swap(tstate) != NULL) 1121 if (PyThreadState_Swap(tstate) != NULL)
1280 Py_FatalError("ceval: orphan tstate"); 1122 Py_FatalError("ceval: orphan tstate");
1281 } 1123
1282 #endif 1124 /* Check for thread interrupts */
1283 /* Check for asynchronous exceptions. */ 1125
1284 if (tstate->async_exc != NULL) { 1126 if (tstate->async_exc != NULL) {
1285 PyObject *exc = tstate->async_exc; 1127 x = tstate->async_exc;
1286 tstate->async_exc = NULL; 1128 tstate->async_exc = NULL;
1287 UNSIGNAL_ASYNC_EXC(); 1129 PyErr_SetNone(x);
1288 PyErr_SetNone(exc); 1130 Py_DECREF(x);
1289 Py_DECREF(exc); 1131 why = WHY_EXCEPTION;
1290 goto error; 1132 goto on_error;
1291 } 1133 }
1134 }
1135 #endif
1292 } 1136 }
1293 1137
1294 fast_next_opcode: 1138 fast_next_opcode:
1295 f->f_lasti = INSTR_OFFSET(); 1139 f->f_lasti = INSTR_OFFSET();
1296 1140
1297 /* line-by-line tracing support */ 1141 /* line-by-line tracing support */
1298 1142
1299 if (_Py_TracingPossible && 1143 if (_Py_TracingPossible &&
1300 tstate->c_tracefunc != NULL && !tstate->tracing) { 1144 tstate->c_tracefunc != NULL && !tstate->tracing) {
1301 int err;
1302 /* see maybe_call_line_trace 1145 /* see maybe_call_line_trace
1303 for expository comments */ 1146 for expository comments */
1304 f->f_stacktop = stack_pointer; 1147 f->f_stacktop = stack_pointer;
1305 1148
1306 err = maybe_call_line_trace(tstate->c_tracefunc, 1149 err = maybe_call_line_trace(tstate->c_tracefunc,
1307 tstate->c_traceobj, 1150 tstate->c_traceobj,
1308 tstate, f, 1151 f, &instr_lb, &instr_ub,
1309 &instr_lb, &instr_ub, &instr_prev); 1152 &instr_prev);
1310 /* Reload possibly changed frame fields */ 1153 /* Reload possibly changed frame fields */
1311 JUMPTO(f->f_lasti); 1154 JUMPTO(f->f_lasti);
1312 if (f->f_stacktop != NULL) { 1155 if (f->f_stacktop != NULL) {
1313 stack_pointer = f->f_stacktop; 1156 stack_pointer = f->f_stacktop;
1314 f->f_stacktop = NULL; 1157 f->f_stacktop = NULL;
1315 } 1158 }
1316 if (err) 1159 if (err) {
1317 /* trace function raised an exception */ 1160 /* trace function raised an exception */
1318 goto error; 1161 goto on_error;
1162 }
1319 } 1163 }
1320 1164
1321 /* Extract opcode and argument */ 1165 /* Extract opcode and argument */
1322 1166
1323 opcode = NEXTOP(); 1167 opcode = NEXTOP();
1324 oparg = 0; /* allows oparg to be stored in a register because 1168 oparg = 0; /* allows oparg to be stored in a register because
1325 it doesn't have to be remembered across a full loop */ 1169 it doesn't have to be remembered across a full loop */
1326 if (HAS_ARG(opcode)) 1170 if (HAS_ARG(opcode))
1327 oparg = NEXTARG(); 1171 oparg = NEXTARG();
1328 dispatch_opcode: 1172 dispatch_opcode:
(...skipping 23 matching lines...) Expand all
1352 /* Main switch on opcode */ 1196 /* Main switch on opcode */
1353 READ_TIMESTAMP(inst0); 1197 READ_TIMESTAMP(inst0);
1354 1198
1355 switch (opcode) { 1199 switch (opcode) {
1356 1200
1357 /* BEWARE! 1201 /* BEWARE!
1358 It is essential that any operation that fails sets either 1202 It is essential that any operation that fails sets either
1359 x to NULL, err to nonzero, or why to anything but WHY_NOT, 1203 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1360 and that no operation that succeeds does this! */ 1204 and that no operation that succeeds does this! */
1361 1205
1362 TARGET(NOP) 1206 /* case STOP_CODE: this is an error! */
1207
1208 TARGET_NOARG(NOP)
1209 {
1363 FAST_DISPATCH(); 1210 FAST_DISPATCH();
1364 1211 }
1365 TARGET(LOAD_FAST) { 1212
1366 PyObject *value = GETLOCAL(oparg); 1213 TARGET(LOAD_FAST)
1367 if (value == NULL) { 1214 {
1368 format_exc_check_arg(PyExc_UnboundLocalError, 1215 x = GETLOCAL(oparg);
1369 UNBOUNDLOCAL_ERROR_MSG, 1216 if (x != NULL) {
1370 PyTuple_GetItem(co->co_varnames, oparg)); 1217 Py_INCREF(x);
1371 goto error; 1218 PUSH(x);
1372 } 1219 FAST_DISPATCH();
1373 Py_INCREF(value); 1220 }
1374 PUSH(value); 1221 format_exc_check_arg(PyExc_UnboundLocalError,
1222 UNBOUNDLOCAL_ERROR_MSG,
1223 PyTuple_GetItem(co->co_varnames, oparg));
1224 break;
1225 }
1226
1227 TARGET(LOAD_CONST)
1228 {
1229 x = GETITEM(consts, oparg);
1230 Py_INCREF(x);
1231 PUSH(x);
1375 FAST_DISPATCH(); 1232 FAST_DISPATCH();
1376 } 1233 }
1377 1234
1378 TARGET(LOAD_CONST) { 1235 PREDICTED_WITH_ARG(STORE_FAST);
1379 PyObject *value = GETITEM(consts, oparg); 1236 TARGET(STORE_FAST)
1380 Py_INCREF(value); 1237 {
1381 PUSH(value); 1238 v = POP();
1239 SETLOCAL(oparg, v);
1382 FAST_DISPATCH(); 1240 FAST_DISPATCH();
1383 } 1241 }
1384 1242
1385 PREDICTED_WITH_ARG(STORE_FAST); 1243 TARGET_NOARG(POP_TOP)
1386 TARGET(STORE_FAST) { 1244 {
1387 PyObject *value = POP(); 1245 v = POP();
1388 SETLOCAL(oparg, value); 1246 Py_DECREF(v);
1389 FAST_DISPATCH(); 1247 FAST_DISPATCH();
1390 } 1248 }
1391 1249
1392 TARGET(POP_TOP) { 1250 TARGET_NOARG(ROT_TWO)
1393 PyObject *value = POP(); 1251 {
1394 Py_DECREF(value); 1252 v = TOP();
1253 w = SECOND();
1254 SET_TOP(w);
1255 SET_SECOND(v);
1395 FAST_DISPATCH(); 1256 FAST_DISPATCH();
1396 } 1257 }
1397 1258
1398 TARGET(ROT_TWO) { 1259 TARGET_NOARG(ROT_THREE)
1399 PyObject *top = TOP(); 1260 {
1400 PyObject *second = SECOND(); 1261 v = TOP();
1401 SET_TOP(second); 1262 w = SECOND();
1402 SET_SECOND(top); 1263 x = THIRD();
1264 SET_TOP(w);
1265 SET_SECOND(x);
1266 SET_THIRD(v);
1403 FAST_DISPATCH(); 1267 FAST_DISPATCH();
1404 } 1268 }
1405 1269
1406 TARGET(ROT_THREE) { 1270 TARGET_NOARG(ROT_FOUR)
1407 PyObject *top = TOP(); 1271 {
1408 PyObject *second = SECOND(); 1272 u = TOP();
1409 PyObject *third = THIRD(); 1273 v = SECOND();
1410 SET_TOP(second); 1274 w = THIRD();
1411 SET_SECOND(third); 1275 x = FOURTH();
1412 SET_THIRD(top); 1276 SET_TOP(v);
1277 SET_SECOND(w);
1278 SET_THIRD(x);
1279 SET_FOURTH(u);
1413 FAST_DISPATCH(); 1280 FAST_DISPATCH();
1414 } 1281 }
1415 1282
1416 TARGET(DUP_TOP) { 1283
1417 PyObject *top = TOP(); 1284 TARGET_NOARG(DUP_TOP)
1418 Py_INCREF(top); 1285 {
1419 PUSH(top); 1286 v = TOP();
1287 Py_INCREF(v);
1288 PUSH(v);
1420 FAST_DISPATCH(); 1289 FAST_DISPATCH();
1421 } 1290 }
1422 1291
1423 TARGET(DUP_TOP_TWO) { 1292
1424 PyObject *top = TOP(); 1293 TARGET(DUP_TOPX)
1425 PyObject *second = SECOND(); 1294 {
1426 Py_INCREF(top); 1295 if (oparg == 2) {
1427 Py_INCREF(second); 1296 x = TOP();
1428 STACKADJ(2); 1297 Py_INCREF(x);
1429 SET_TOP(top); 1298 w = SECOND();
1430 SET_SECOND(second); 1299 Py_INCREF(w);
1431 FAST_DISPATCH(); 1300 STACKADJ(2);
1432 } 1301 SET_TOP(x);
1433 1302 SET_SECOND(w);
1434 TARGET(UNARY_POSITIVE) { 1303 FAST_DISPATCH();
1435 PyObject *value = TOP(); 1304 } else if (oparg == 3) {
1436 PyObject *res = PyNumber_Positive(value); 1305 x = TOP();
1437 Py_DECREF(value); 1306 Py_INCREF(x);
1438 SET_TOP(res); 1307 w = SECOND();
1439 if (res == NULL) 1308 Py_INCREF(w);
1440 goto error; 1309 v = THIRD();
1441 DISPATCH(); 1310 Py_INCREF(v);
1442 } 1311 STACKADJ(3);
1443 1312 SET_TOP(x);
1444 TARGET(UNARY_NEGATIVE) { 1313 SET_SECOND(w);
1445 PyObject *value = TOP(); 1314 SET_THIRD(v);
1446 PyObject *res = PyNumber_Negative(value); 1315 FAST_DISPATCH();
1447 Py_DECREF(value); 1316 }
1448 SET_TOP(res); 1317 Py_FatalError("invalid argument to DUP_TOPX"
1449 if (res == NULL) 1318 " (bytecode corruption?)");
1450 goto error; 1319 /* Never returns, so don't bother to set why. */
1451 DISPATCH(); 1320 break;
1452 } 1321 }
1453 1322
1454 TARGET(UNARY_NOT) { 1323 TARGET_NOARG(UNARY_POSITIVE)
1455 PyObject *value = TOP(); 1324 {
1456 int err = PyObject_IsTrue(value); 1325 v = TOP();
1457 Py_DECREF(value); 1326 x = PyNumber_Positive(v);
1327 Py_DECREF(v);
1328 SET_TOP(x);
1329 if (x != NULL) DISPATCH();
1330 break;
1331 }
1332
1333 TARGET_NOARG( UNARY_NEGATIVE)
1334 {
1335 v = TOP();
1336 x = PyNumber_Negative(v);
1337 Py_DECREF(v);
1338 SET_TOP(x);
1339 if (x != NULL) DISPATCH();
1340 break;
1341 }
1342
1343 TARGET_NOARG(UNARY_NOT)
1344 {
1345 v = TOP();
1346 err = PyObject_IsTrue(v);
1347 Py_DECREF(v);
1458 if (err == 0) { 1348 if (err == 0) {
1459 Py_INCREF(Py_True); 1349 Py_INCREF(Py_True);
1460 SET_TOP(Py_True); 1350 SET_TOP(Py_True);
1461 DISPATCH(); 1351 DISPATCH();
1462 } 1352 }
1463 else if (err > 0) { 1353 else if (err > 0) {
1464 Py_INCREF(Py_False); 1354 Py_INCREF(Py_False);
1465 SET_TOP(Py_False); 1355 SET_TOP(Py_False);
1466 err = 0; 1356 err = 0;
1467 DISPATCH(); 1357 DISPATCH();
1468 } 1358 }
1469 STACKADJ(-1); 1359 STACKADJ(-1);
1470 goto error; 1360 break;
1471 } 1361 }
1472 1362
1473 TARGET(UNARY_INVERT) { 1363 TARGET_NOARG(UNARY_CONVERT)
1474 PyObject *value = TOP(); 1364 {
1475 PyObject *res = PyNumber_Invert(value); 1365 v = TOP();
1476 Py_DECREF(value); 1366 x = PyObject_Repr(v);
1477 SET_TOP(res); 1367 Py_DECREF(v);
1478 if (res == NULL) 1368 SET_TOP(x);
1479 goto error; 1369 if (x != NULL) DISPATCH();
1480 DISPATCH(); 1370 break;
1481 } 1371 }
1482 1372
1483 TARGET(BINARY_POWER) { 1373 TARGET_NOARG(UNARY_INVERT)
1484 PyObject *exp = POP(); 1374 {
1485 PyObject *base = TOP(); 1375 v = TOP();
1486 PyObject *res = PyNumber_Power(base, exp, Py_None); 1376 x = PyNumber_Invert(v);
1487 Py_DECREF(base); 1377 Py_DECREF(v);
1488 Py_DECREF(exp); 1378 SET_TOP(x);
1489 SET_TOP(res); 1379 if (x != NULL) DISPATCH();
1490 if (res == NULL) 1380 break;
1491 goto error; 1381 }
1492 DISPATCH(); 1382
1493 } 1383 TARGET_NOARG(BINARY_POWER)
1494 1384 {
1495 TARGET(BINARY_MULTIPLY) { 1385 w = POP();
1496 PyObject *right = POP(); 1386 v = TOP();
1497 PyObject *left = TOP(); 1387 x = PyNumber_Power(v, w, Py_None);
1498 PyObject *res = PyNumber_Multiply(left, right); 1388 Py_DECREF(v);
1499 Py_DECREF(left); 1389 Py_DECREF(w);
1500 Py_DECREF(right); 1390 SET_TOP(x);
1501 SET_TOP(res); 1391 if (x != NULL) DISPATCH();
1502 if (res == NULL) 1392 break;
1503 goto error; 1393 }
1504 DISPATCH(); 1394
1505 } 1395 TARGET_NOARG(BINARY_MULTIPLY)
1506 1396 {
1507 TARGET(BINARY_MATRIX_MULTIPLY) { 1397 w = POP();
1508 PyObject *right = POP(); 1398 v = TOP();
1509 PyObject *left = TOP(); 1399 x = PyNumber_Multiply(v, w);
1510 PyObject *res = PyNumber_MatrixMultiply(left, right); 1400 Py_DECREF(v);
1511 Py_DECREF(left); 1401 Py_DECREF(w);
1512 Py_DECREF(right); 1402 SET_TOP(x);
1513 SET_TOP(res); 1403 if(x!=NULL) DISPATCH();
1514 if (res == NULL) 1404 break;
1515 goto error; 1405 }
1516 DISPATCH(); 1406
1517 } 1407 TARGET_NOARG(BINARY_DIVIDE)
1518 1408 {
1519 TARGET(BINARY_TRUE_DIVIDE) { 1409 if (!_Py_QnewFlag) {
1520 PyObject *divisor = POP(); 1410 w = POP();
1521 PyObject *dividend = TOP(); 1411 v = TOP();
1522 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor); 1412 x = PyNumber_Divide(v, w);
1523 Py_DECREF(dividend); 1413 Py_DECREF(v);
1524 Py_DECREF(divisor); 1414 Py_DECREF(w);
1525 SET_TOP(quotient); 1415 SET_TOP(x);
1526 if (quotient == NULL) 1416 if (x != NULL) DISPATCH();
1527 goto error; 1417 break;
1528 DISPATCH(); 1418 }
1529 } 1419 }
1530 1420 /* -Qnew is in effect: fall through to BINARY_TRUE_DIVIDE */
1531 TARGET(BINARY_FLOOR_DIVIDE) { 1421 TARGET_NOARG(BINARY_TRUE_DIVIDE)
1532 PyObject *divisor = POP(); 1422 {
1533 PyObject *dividend = TOP(); 1423 w = POP();
1534 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor); 1424 v = TOP();
1535 Py_DECREF(dividend); 1425 x = PyNumber_TrueDivide(v, w);
1536 Py_DECREF(divisor); 1426 Py_DECREF(v);
1537 SET_TOP(quotient); 1427 Py_DECREF(w);
1538 if (quotient == NULL) 1428 SET_TOP(x);
1539 goto error; 1429 if (x != NULL) DISPATCH();
1540 DISPATCH(); 1430 break;
1541 } 1431 }
1542 1432
1543 TARGET(BINARY_MODULO) { 1433 TARGET_NOARG(BINARY_FLOOR_DIVIDE)
1544 PyObject *divisor = POP(); 1434 {
1545 PyObject *dividend = TOP(); 1435 w = POP();
1546 PyObject *res = PyUnicode_CheckExact(dividend) ? 1436 v = TOP();
1547 PyUnicode_Format(dividend, divisor) : 1437 x = PyNumber_FloorDivide(v, w);
1548 PyNumber_Remainder(dividend, divisor); 1438 Py_DECREF(v);
1549 Py_DECREF(divisor); 1439 Py_DECREF(w);
1550 Py_DECREF(dividend); 1440 SET_TOP(x);
1551 SET_TOP(res); 1441 if (x != NULL) DISPATCH();
1552 if (res == NULL) 1442 break;
1553 goto error; 1443 }
1554 DISPATCH(); 1444
1555 } 1445 TARGET_NOARG(BINARY_MODULO)
1556 1446 {
1557 TARGET(BINARY_ADD) { 1447 w = POP();
1558 PyObject *right = POP(); 1448 v = TOP();
1559 PyObject *left = TOP(); 1449 if (PyString_CheckExact(v))
1560 PyObject *sum; 1450 x = PyString_Format(v, w);
1561 if (PyUnicode_CheckExact(left) && 1451 else
1562 PyUnicode_CheckExact(right)) { 1452 x = PyNumber_Remainder(v, w);
1563 sum = unicode_concatenate(left, right, f, next_instr); 1453 Py_DECREF(v);
1564 /* unicode_concatenate consumed the ref to v */ 1454 Py_DECREF(w);
1455 SET_TOP(x);
1456 if (x != NULL) DISPATCH();
1457 break;
1458 }
1459
1460 TARGET_NOARG(BINARY_ADD)
1461 {
1462 w = POP();
1463 v = TOP();
1464 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1465 /* INLINE: int + int */
1466 register long a, b, i;
1467 a = PyInt_AS_LONG(v);
1468 b = PyInt_AS_LONG(w);
1469 /* cast to avoid undefined behaviour
1470 on overflow */
1471 i = (long)((unsigned long)a + b);
1472 if ((i^a) < 0 && (i^b) < 0)
1473 goto slow_add;
1474 x = PyInt_FromLong(i);
1475 }
1476 else if (PyString_CheckExact(v) &&
1477 PyString_CheckExact(w)) {
1478 x = string_concatenate(v, w, f, next_instr);
1479 /* string_concatenate consumed the ref to v */
1480 goto skip_decref_vx;
1565 } 1481 }
1566 else { 1482 else {
1567 sum = PyNumber_Add(left, right); 1483 slow_add:
1568 Py_DECREF(left); 1484 x = PyNumber_Add(v, w);
1569 } 1485 }
1570 Py_DECREF(right);
1571 SET_TOP(sum);
1572 if (sum == NULL)
1573 goto error;
1574 DISPATCH();
1575 }
1576
1577 TARGET(BINARY_SUBTRACT) {
1578 PyObject *right = POP();
1579 PyObject *left = TOP();
1580 PyObject *diff = PyNumber_Subtract(left, right);
1581 Py_DECREF(right);
1582 Py_DECREF(left);
1583 SET_TOP(diff);
1584 if (diff == NULL)
1585 goto error;
1586 DISPATCH();
1587 }
1588
1589 TARGET(BINARY_SUBSCR) {
1590 PyObject *sub = POP();
1591 PyObject *container = TOP();
1592 PyObject *res = PyObject_GetItem(container, sub);
1593 Py_DECREF(container);
1594 Py_DECREF(sub);
1595 SET_TOP(res);
1596 if (res == NULL)
1597 goto error;
1598 DISPATCH();
1599 }
1600
1601 TARGET(BINARY_LSHIFT) {
1602 PyObject *right = POP();
1603 PyObject *left = TOP();
1604 PyObject *res = PyNumber_Lshift(left, right);
1605 Py_DECREF(left);
1606 Py_DECREF(right);
1607 SET_TOP(res);
1608 if (res == NULL)
1609 goto error;
1610 DISPATCH();
1611 }
1612
1613 TARGET(BINARY_RSHIFT) {
1614 PyObject *right = POP();
1615 PyObject *left = TOP();
1616 PyObject *res = PyNumber_Rshift(left, right);
1617 Py_DECREF(left);
1618 Py_DECREF(right);
1619 SET_TOP(res);
1620 if (res == NULL)
1621 goto error;
1622 DISPATCH();
1623 }
1624
1625 TARGET(BINARY_AND) {
1626 PyObject *right = POP();
1627 PyObject *left = TOP();
1628 PyObject *res = PyNumber_And(left, right);
1629 Py_DECREF(left);
1630 Py_DECREF(right);
1631 SET_TOP(res);
1632 if (res == NULL)
1633 goto error;
1634 DISPATCH();
1635 }
1636
1637 TARGET(BINARY_XOR) {
1638 PyObject *right = POP();
1639 PyObject *left = TOP();
1640 PyObject *res = PyNumber_Xor(left, right);
1641 Py_DECREF(left);
1642 Py_DECREF(right);
1643 SET_TOP(res);
1644 if (res == NULL)
1645 goto error;
1646 DISPATCH();
1647 }
1648
1649 TARGET(BINARY_OR) {
1650 PyObject *right = POP();
1651 PyObject *left = TOP();
1652 PyObject *res = PyNumber_Or(left, right);
1653 Py_DECREF(left);
1654 Py_DECREF(right);
1655 SET_TOP(res);
1656 if (res == NULL)
1657 goto error;
1658 DISPATCH();
1659 }
1660
1661 TARGET(LIST_APPEND) {
1662 PyObject *v = POP();
1663 PyObject *list = PEEK(oparg);
1664 int err;
1665 err = PyList_Append(list, v);
1666 Py_DECREF(v); 1486 Py_DECREF(v);
1667 if (err != 0) 1487 skip_decref_vx:
1668 goto error; 1488 Py_DECREF(w);
1669 PREDICT(JUMP_ABSOLUTE); 1489 SET_TOP(x);
1670 DISPATCH(); 1490 if (x != NULL) DISPATCH();
1671 } 1491 break;
1672 1492 }
1673 TARGET(SET_ADD) { 1493
1674 PyObject *v = POP(); 1494 TARGET_NOARG(BINARY_SUBTRACT)
1675 PyObject *set = stack_pointer[-oparg]; 1495 {
1676 int err; 1496 w = POP();
1677 err = PySet_Add(set, v); 1497 v = TOP();
1498 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1499 /* INLINE: int - int */
1500 register long a, b, i;
1501 a = PyInt_AS_LONG(v);
1502 b = PyInt_AS_LONG(w);
1503 /* cast to avoid undefined behaviour
1504 on overflow */
1505 i = (long)((unsigned long)a - b);
1506 if ((i^a) < 0 && (i^~b) < 0)
1507 goto slow_sub;
1508 x = PyInt_FromLong(i);
1509 }
1510 else {
1511 slow_sub:
1512 x = PyNumber_Subtract(v, w);
1513 }
1678 Py_DECREF(v); 1514 Py_DECREF(v);
1679 if (err != 0) 1515 Py_DECREF(w);
1680 goto error; 1516 SET_TOP(x);
1681 PREDICT(JUMP_ABSOLUTE); 1517 if (x != NULL) DISPATCH();
1682 DISPATCH(); 1518 break;
1683 } 1519 }
1684 1520
1685 TARGET(INPLACE_POWER) { 1521 TARGET_NOARG(BINARY_SUBSCR)
1686 PyObject *exp = POP(); 1522 {
1687 PyObject *base = TOP(); 1523 w = POP();
1688 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None); 1524 v = TOP();
1689 Py_DECREF(base); 1525 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
1690 Py_DECREF(exp); 1526 /* INLINE: list[int] */
1691 SET_TOP(res); 1527 Py_ssize_t i = PyInt_AsSsize_t(w);
1692 if (res == NULL) 1528 if (i < 0)
1693 goto error; 1529 i += PyList_GET_SIZE(v);
1694 DISPATCH(); 1530 if (i >= 0 && i < PyList_GET_SIZE(v)) {
1695 } 1531 x = PyList_GET_ITEM(v, i);
1696 1532 Py_INCREF(x);
1697 TARGET(INPLACE_MULTIPLY) { 1533 }
1698 PyObject *right = POP(); 1534 else
1699 PyObject *left = TOP(); 1535 goto slow_get;
1700 PyObject *res = PyNumber_InPlaceMultiply(left, right); 1536 }
1701 Py_DECREF(left); 1537 else
1702 Py_DECREF(right); 1538 slow_get:
1703 SET_TOP(res); 1539 x = PyObject_GetItem(v, w);
1704 if (res == NULL) 1540 Py_DECREF(v);
1705 goto error; 1541 Py_DECREF(w);
1706 DISPATCH(); 1542 SET_TOP(x);
1707 } 1543 if (x != NULL) DISPATCH();
1708 1544 break;
1709 TARGET(INPLACE_MATRIX_MULTIPLY) { 1545 }
1710 PyObject *right = POP(); 1546
1711 PyObject *left = TOP(); 1547 TARGET_NOARG(BINARY_LSHIFT)
1712 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right); 1548 {
1713 Py_DECREF(left); 1549 w = POP();
1714 Py_DECREF(right); 1550 v = TOP();
1715 SET_TOP(res); 1551 x = PyNumber_Lshift(v, w);
1716 if (res == NULL) 1552 Py_DECREF(v);
1717 goto error; 1553 Py_DECREF(w);
1718 DISPATCH(); 1554 SET_TOP(x);
1719 } 1555 if (x != NULL) DISPATCH();
1720 1556 break;
1721 TARGET(INPLACE_TRUE_DIVIDE) { 1557 }
1722 PyObject *divisor = POP(); 1558
1723 PyObject *dividend = TOP(); 1559 TARGET_NOARG(BINARY_RSHIFT)
1724 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor); 1560 {
1725 Py_DECREF(dividend); 1561 w = POP();
1726 Py_DECREF(divisor); 1562 v = TOP();
1727 SET_TOP(quotient); 1563 x = PyNumber_Rshift(v, w);
1728 if (quotient == NULL) 1564 Py_DECREF(v);
1729 goto error; 1565 Py_DECREF(w);
1730 DISPATCH(); 1566 SET_TOP(x);
1731 } 1567 if (x != NULL) DISPATCH();
1732 1568 break;
1733 TARGET(INPLACE_FLOOR_DIVIDE) { 1569 }
1734 PyObject *divisor = POP(); 1570
1735 PyObject *dividend = TOP(); 1571 TARGET_NOARG(BINARY_AND)
1736 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor); 1572 {
1737 Py_DECREF(dividend); 1573 w = POP();
1738 Py_DECREF(divisor); 1574 v = TOP();
1739 SET_TOP(quotient); 1575 x = PyNumber_And(v, w);
1740 if (quotient == NULL) 1576 Py_DECREF(v);
1741 goto error; 1577 Py_DECREF(w);
1742 DISPATCH(); 1578 SET_TOP(x);
1743 } 1579 if (x != NULL) DISPATCH();
1744 1580 break;
1745 TARGET(INPLACE_MODULO) { 1581 }
1746 PyObject *right = POP(); 1582
1747 PyObject *left = TOP(); 1583 TARGET_NOARG(BINARY_XOR)
1748 PyObject *mod = PyNumber_InPlaceRemainder(left, right); 1584 {
1749 Py_DECREF(left); 1585 w = POP();
1750 Py_DECREF(right); 1586 v = TOP();
1751 SET_TOP(mod); 1587 x = PyNumber_Xor(v, w);
1752 if (mod == NULL) 1588 Py_DECREF(v);
1753 goto error; 1589 Py_DECREF(w);
1754 DISPATCH(); 1590 SET_TOP(x);
1755 } 1591 if (x != NULL) DISPATCH();
1756 1592 break;
1757 TARGET(INPLACE_ADD) { 1593 }
1758 PyObject *right = POP(); 1594
1759 PyObject *left = TOP(); 1595 TARGET_NOARG(BINARY_OR)
1760 PyObject *sum; 1596 {
1761 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) { 1597 w = POP();
1762 sum = unicode_concatenate(left, right, f, next_instr); 1598 v = TOP();
1763 /* unicode_concatenate consumed the ref to v */ 1599 x = PyNumber_Or(v, w);
1600 Py_DECREF(v);
1601 Py_DECREF(w);
1602 SET_TOP(x);
1603 if (x != NULL) DISPATCH();
1604 break;
1605 }
1606
1607 TARGET(LIST_APPEND)
1608 {
1609 w = POP();
1610 v = PEEK(oparg);
1611 err = PyList_Append(v, w);
1612 Py_DECREF(w);
1613 if (err == 0) {
1614 PREDICT(JUMP_ABSOLUTE);
1615 DISPATCH();
1616 }
1617 break;
1618 }
1619
1620 TARGET(SET_ADD)
1621 {
1622 w = POP();
1623 v = stack_pointer[-oparg];
1624 err = PySet_Add(v, w);
1625 Py_DECREF(w);
1626 if (err == 0) {
1627 PREDICT(JUMP_ABSOLUTE);
1628 DISPATCH();
1629 }
1630 break;
1631 }
1632
1633 TARGET_NOARG(INPLACE_POWER)
1634 {
1635 w = POP();
1636 v = TOP();
1637 x = PyNumber_InPlacePower(v, w, Py_None);
1638 Py_DECREF(v);
1639 Py_DECREF(w);
1640 SET_TOP(x);
1641 if (x != NULL) DISPATCH();
1642 break;
1643 }
1644
1645 TARGET_NOARG(INPLACE_MULTIPLY)
1646 {
1647 w = POP();
1648 v = TOP();
1649 x = PyNumber_InPlaceMultiply(v, w);
1650 Py_DECREF(v);
1651 Py_DECREF(w);
1652 SET_TOP(x);
1653 if (x != NULL) DISPATCH();
1654 break;
1655 }
1656
1657 TARGET_NOARG(INPLACE_DIVIDE)
1658 {
1659 if (!_Py_QnewFlag) {
1660 w = POP();
1661 v = TOP();
1662 x = PyNumber_InPlaceDivide(v, w);
1663 Py_DECREF(v);
1664 Py_DECREF(w);
1665 SET_TOP(x);
1666 if (x != NULL) DISPATCH();
1667 break;
1668 }
1669 }
1670 /* -Qnew is in effect: fall through to
1671 INPLACE_TRUE_DIVIDE */
1672 TARGET_NOARG(INPLACE_TRUE_DIVIDE)
1673 {
1674 w = POP();
1675 v = TOP();
1676 x = PyNumber_InPlaceTrueDivide(v, w);
1677 Py_DECREF(v);
1678 Py_DECREF(w);
1679 SET_TOP(x);
1680 if (x != NULL) DISPATCH();
1681 break;
1682 }
1683
1684 TARGET_NOARG(INPLACE_FLOOR_DIVIDE)
1685 {
1686 w = POP();
1687 v = TOP();
1688 x = PyNumber_InPlaceFloorDivide(v, w);
1689 Py_DECREF(v);
1690 Py_DECREF(w);
1691 SET_TOP(x);
1692 if (x != NULL) DISPATCH();
1693 break;
1694 }
1695
1696 TARGET_NOARG(INPLACE_MODULO)
1697 {
1698 w = POP();
1699 v = TOP();
1700 x = PyNumber_InPlaceRemainder(v, w);
1701 Py_DECREF(v);
1702 Py_DECREF(w);
1703 SET_TOP(x);
1704 if (x != NULL) DISPATCH();
1705 break;
1706 }
1707
1708 TARGET_NOARG(INPLACE_ADD)
1709 {
1710 w = POP();
1711 v = TOP();
1712 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1713 /* INLINE: int + int */
1714 register long a, b, i;
1715 a = PyInt_AS_LONG(v);
1716 b = PyInt_AS_LONG(w);
1717 i = a + b;
1718 if ((i^a) < 0 && (i^b) < 0)
1719 goto slow_iadd;
1720 x = PyInt_FromLong(i);
1721 }
1722 else if (PyString_CheckExact(v) &&
1723 PyString_CheckExact(w)) {
1724 x = string_concatenate(v, w, f, next_instr);
1725 /* string_concatenate consumed the ref to v */
1726 goto skip_decref_v;
1764 } 1727 }
1765 else { 1728 else {
1766 sum = PyNumber_InPlaceAdd(left, right); 1729 slow_iadd:
1767 Py_DECREF(left); 1730 x = PyNumber_InPlaceAdd(v, w);
1768 } 1731 }
1769 Py_DECREF(right); 1732 Py_DECREF(v);
1770 SET_TOP(sum); 1733 skip_decref_v:
1771 if (sum == NULL) 1734 Py_DECREF(w);
1772 goto error; 1735 SET_TOP(x);
1773 DISPATCH(); 1736 if (x != NULL) DISPATCH();
1774 } 1737 break;
1775 1738 }
1776 TARGET(INPLACE_SUBTRACT) { 1739
1777 PyObject *right = POP(); 1740 TARGET_NOARG(INPLACE_SUBTRACT)
1778 PyObject *left = TOP(); 1741 {
1779 PyObject *diff = PyNumber_InPlaceSubtract(left, right); 1742 w = POP();
1780 Py_DECREF(left); 1743 v = TOP();
1781 Py_DECREF(right); 1744 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1782 SET_TOP(diff); 1745 /* INLINE: int - int */
1783 if (diff == NULL) 1746 register long a, b, i;
1784 goto error; 1747 a = PyInt_AS_LONG(v);
1785 DISPATCH(); 1748 b = PyInt_AS_LONG(w);
1786 } 1749 i = a - b;
1787 1750 if ((i^a) < 0 && (i^~b) < 0)
1788 TARGET(INPLACE_LSHIFT) { 1751 goto slow_isub;
1789 PyObject *right = POP(); 1752 x = PyInt_FromLong(i);
1790 PyObject *left = TOP(); 1753 }
1791 PyObject *res = PyNumber_InPlaceLshift(left, right); 1754 else {
1792 Py_DECREF(left); 1755 slow_isub:
1793 Py_DECREF(right); 1756 x = PyNumber_InPlaceSubtract(v, w);
1794 SET_TOP(res); 1757 }
1795 if (res == NULL) 1758 Py_DECREF(v);
1796 goto error; 1759 Py_DECREF(w);
1797 DISPATCH(); 1760 SET_TOP(x);
1798 } 1761 if (x != NULL) DISPATCH();
1799 1762 break;
1800 TARGET(INPLACE_RSHIFT) { 1763 }
1801 PyObject *right = POP(); 1764
1802 PyObject *left = TOP(); 1765 TARGET_NOARG(INPLACE_LSHIFT)
1803 PyObject *res = PyNumber_InPlaceRshift(left, right); 1766 {
1804 Py_DECREF(left); 1767 w = POP();
1805 Py_DECREF(right); 1768 v = TOP();
1806 SET_TOP(res); 1769 x = PyNumber_InPlaceLshift(v, w);
1807 if (res == NULL) 1770 Py_DECREF(v);
1808 goto error; 1771 Py_DECREF(w);
1809 DISPATCH(); 1772 SET_TOP(x);
1810 } 1773 if (x != NULL) DISPATCH();
1811 1774 break;
1812 TARGET(INPLACE_AND) { 1775 }
1813 PyObject *right = POP(); 1776
1814 PyObject *left = TOP(); 1777 TARGET_NOARG(INPLACE_RSHIFT)
1815 PyObject *res = PyNumber_InPlaceAnd(left, right); 1778 {
1816 Py_DECREF(left); 1779 w = POP();
1817 Py_DECREF(right); 1780 v = TOP();
1818 SET_TOP(res); 1781 x = PyNumber_InPlaceRshift(v, w);
1819 if (res == NULL) 1782 Py_DECREF(v);
1820 goto error; 1783 Py_DECREF(w);
1821 DISPATCH(); 1784 SET_TOP(x);
1822 } 1785 if (x != NULL) DISPATCH();
1823 1786 break;
1824 TARGET(INPLACE_XOR) { 1787 }
1825 PyObject *right = POP(); 1788
1826 PyObject *left = TOP(); 1789 TARGET_NOARG(INPLACE_AND)
1827 PyObject *res = PyNumber_InPlaceXor(left, right); 1790 {
1828 Py_DECREF(left); 1791 w = POP();
1829 Py_DECREF(right); 1792 v = TOP();
1830 SET_TOP(res); 1793 x = PyNumber_InPlaceAnd(v, w);
1831 if (res == NULL) 1794 Py_DECREF(v);
1832 goto error; 1795 Py_DECREF(w);
1833 DISPATCH(); 1796 SET_TOP(x);
1834 } 1797 if (x != NULL) DISPATCH();
1835 1798 break;
1836 TARGET(INPLACE_OR) { 1799 }
1837 PyObject *right = POP(); 1800
1838 PyObject *left = TOP(); 1801 TARGET_NOARG(INPLACE_XOR)
1839 PyObject *res = PyNumber_InPlaceOr(left, right); 1802 {
1840 Py_DECREF(left); 1803 w = POP();
1841 Py_DECREF(right); 1804 v = TOP();
1842 SET_TOP(res); 1805 x = PyNumber_InPlaceXor(v, w);
1843 if (res == NULL) 1806 Py_DECREF(v);
1844 goto error; 1807 Py_DECREF(w);
1845 DISPATCH(); 1808 SET_TOP(x);
1846 } 1809 if (x != NULL) DISPATCH();
1847 1810 break;
1848 TARGET(STORE_SUBSCR) { 1811 }
1849 PyObject *sub = TOP(); 1812
1850 PyObject *container = SECOND(); 1813 TARGET_NOARG(INPLACE_OR)
1851 PyObject *v = THIRD(); 1814 {
1852 int err; 1815 w = POP();
1816 v = TOP();
1817 x = PyNumber_InPlaceOr(v, w);
1818 Py_DECREF(v);
1819 Py_DECREF(w);
1820 SET_TOP(x);
1821 if (x != NULL) DISPATCH();
1822 break;
1823 }
1824
1825
1826
1827 TARGET_WITH_IMPL_NOARG(SLICE, _slice)
1828 TARGET_WITH_IMPL_NOARG(SLICE_1, _slice)
1829 TARGET_WITH_IMPL_NOARG(SLICE_2, _slice)
1830 TARGET_WITH_IMPL_NOARG(SLICE_3, _slice)
1831 _slice:
1832 {
1833 if ((opcode-SLICE) & 2)
1834 w = POP();
1835 else
1836 w = NULL;
1837 if ((opcode-SLICE) & 1)
1838 v = POP();
1839 else
1840 v = NULL;
1841 u = TOP();
1842 x = apply_slice(u, v, w);
1843 Py_DECREF(u);
1844 Py_XDECREF(v);
1845 Py_XDECREF(w);
1846 SET_TOP(x);
1847 if (x != NULL) DISPATCH();
1848 break;
1849 }
1850
1851
1852 TARGET_WITH_IMPL_NOARG(STORE_SLICE, _store_slice)
1853 TARGET_WITH_IMPL_NOARG(STORE_SLICE_1, _store_slice)
1854 TARGET_WITH_IMPL_NOARG(STORE_SLICE_2, _store_slice)
1855 TARGET_WITH_IMPL_NOARG(STORE_SLICE_3, _store_slice)
1856 _store_slice:
1857 {
1858 if ((opcode-STORE_SLICE) & 2)
1859 w = POP();
1860 else
1861 w = NULL;
1862 if ((opcode-STORE_SLICE) & 1)
1863 v = POP();
1864 else
1865 v = NULL;
1866 u = POP();
1867 t = POP();
1868 err = assign_slice(u, v, w, t); /* u[v:w] = t */
1869 Py_DECREF(t);
1870 Py_DECREF(u);
1871 Py_XDECREF(v);
1872 Py_XDECREF(w);
1873 if (err == 0) DISPATCH();
1874 break;
1875 }
1876
1877
1878 TARGET_WITH_IMPL_NOARG(DELETE_SLICE, _delete_slice)
1879 TARGET_WITH_IMPL_NOARG(DELETE_SLICE_1, _delete_slice)
1880 TARGET_WITH_IMPL_NOARG(DELETE_SLICE_2, _delete_slice)
1881 TARGET_WITH_IMPL_NOARG(DELETE_SLICE_3, _delete_slice)
1882 _delete_slice:
1883 {
1884 if ((opcode-DELETE_SLICE) & 2)
1885 w = POP();
1886 else
1887 w = NULL;
1888 if ((opcode-DELETE_SLICE) & 1)
1889 v = POP();
1890 else
1891 v = NULL;
1892 u = POP();
1893 err = assign_slice(u, v, w, (PyObject *)NULL);
1894 /* del u[v:w] */
1895 Py_DECREF(u);
1896 Py_XDECREF(v);
1897 Py_XDECREF(w);
1898 if (err == 0) DISPATCH();
1899 break;
1900 }
1901
1902 TARGET_NOARG(STORE_SUBSCR)
1903 {
1904 w = TOP();
1905 v = SECOND();
1906 u = THIRD();
1853 STACKADJ(-3); 1907 STACKADJ(-3);
1854 /* v[w] = u */ 1908 /* v[w] = u */
1855 err = PyObject_SetItem(container, sub, v); 1909 err = PyObject_SetItem(v, w, u);
1910 Py_DECREF(u);
1856 Py_DECREF(v); 1911 Py_DECREF(v);
1857 Py_DECREF(container); 1912 Py_DECREF(w);
1858 Py_DECREF(sub); 1913 if (err == 0) DISPATCH();
1859 if (err != 0) 1914 break;
1860 goto error; 1915 }
1861 DISPATCH(); 1916
1862 } 1917 TARGET_NOARG(DELETE_SUBSCR)
1863 1918 {
1864 TARGET(DELETE_SUBSCR) { 1919 w = TOP();
1865 PyObject *sub = TOP(); 1920 v = SECOND();
1866 PyObject *container = SECOND();
1867 int err;
1868 STACKADJ(-2); 1921 STACKADJ(-2);
1869 /* del v[w] */ 1922 /* del v[w] */
1870 err = PyObject_DelItem(container, sub); 1923 err = PyObject_DelItem(v, w);
1871 Py_DECREF(container); 1924 Py_DECREF(v);
1872 Py_DECREF(sub); 1925 Py_DECREF(w);
1873 if (err != 0) 1926 if (err == 0) DISPATCH();
1874 goto error; 1927 break;
1875 DISPATCH(); 1928 }
1876 } 1929
1877 1930 TARGET_NOARG(PRINT_EXPR)
1878 TARGET(PRINT_EXPR) { 1931 {
1879 _Py_IDENTIFIER(displayhook); 1932 v = POP();
1880 PyObject *value = POP(); 1933 w = PySys_GetObject("displayhook");
1881 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook); 1934 if (w == NULL) {
1882 PyObject *res;
1883 if (hook == NULL) {
1884 PyErr_SetString(PyExc_RuntimeError, 1935 PyErr_SetString(PyExc_RuntimeError,
1885 "lost sys.displayhook"); 1936 "lost sys.displayhook");
1886 Py_DECREF(value); 1937 err = -1;
1887 goto error; 1938 x = NULL;
1888 } 1939 }
1889 res = PyObject_CallFunctionObjArgs(hook, value, NULL); 1940 if (err == 0) {
1890 Py_DECREF(value); 1941 x = PyTuple_Pack(1, v);
1891 if (res == NULL) 1942 if (x == NULL)
1892 goto error; 1943 err = -1;
1893 Py_DECREF(res); 1944 }
1894 DISPATCH(); 1945 if (err == 0) {
1946 w = PyEval_CallObject(w, x);
1947 Py_XDECREF(w);
1948 if (w == NULL)
1949 err = -1;
1950 }
1951 Py_DECREF(v);
1952 Py_XDECREF(x);
1953 break;
1954 }
1955
1956 TARGET_NOARG(PRINT_ITEM_TO)
1957 {
1958 w = stream = POP();
1959 /* fall through to PRINT_ITEM */
1960 }
1961
1962 TARGET_NOARG(PRINT_ITEM)
1963 {
1964 v = POP();
1965 if (stream == NULL || stream == Py_None) {
1966 w = PySys_GetObject("stdout");
1967 if (w == NULL) {
1968 PyErr_SetString(PyExc_RuntimeError,
1969 "lost sys.stdout");
1970 err = -1;
1971 }
1972 }
1973 /* PyFile_SoftSpace() can exececute arbitrary code
1974 if sys.stdout is an instance with a __getattr__.
1975 If __getattr__ raises an exception, w will
1976 be freed, so we need to prevent that temporarily. */
1977 Py_XINCREF(w);
1978 if (w != NULL && PyFile_SoftSpace(w, 0))
1979 err = PyFile_WriteString(" ", w);
1980 if (err == 0)
1981 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
1982 if (err == 0) {
1983 /* XXX move into writeobject() ? */
1984 if (PyString_Check(v)) {
1985 char *s = PyString_AS_STRING(v);
1986 Py_ssize_t len = PyString_GET_SIZE(v);
1987 if (len == 0 ||
1988 !isspace(Py_CHARMASK(s[len-1])) ||
1989 s[len-1] == ' ')
1990 PyFile_SoftSpace(w, 1);
1991 }
1992 #ifdef Py_USING_UNICODE
1993 else if (PyUnicode_Check(v)) {
1994 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1995 Py_ssize_t len = PyUnicode_GET_SIZE(v);
1996 if (len == 0 ||
1997 !Py_UNICODE_ISSPACE(s[len-1]) ||
1998 s[len-1] == ' ')
1999 PyFile_SoftSpace(w, 1);
2000 }
2001 #endif
2002 else
2003 PyFile_SoftSpace(w, 1);
2004 }
2005 Py_XDECREF(w);
2006 Py_DECREF(v);
2007 Py_XDECREF(stream);
2008 stream = NULL;
2009 if (err == 0) DISPATCH();
2010 break;
2011 }
2012
2013 TARGET_NOARG(PRINT_NEWLINE_TO)
2014 {
2015 w = stream = POP();
2016 /* fall through to PRINT_NEWLINE */
2017 }
2018
2019 TARGET_NOARG(PRINT_NEWLINE)
2020 {
2021 if (stream == NULL || stream == Py_None)
2022 {
2023 w = PySys_GetObject("stdout");
2024 if (w == NULL) {
2025 PyErr_SetString(PyExc_RuntimeError,
2026 "lost sys.stdout");
2027 why = WHY_EXCEPTION;
2028 }
2029 }
2030 if (w != NULL) {
2031 /* w.write() may replace sys.stdout, so we
2032 * have to keep our reference to it */
2033 Py_INCREF(w);
2034 err = PyFile_WriteString("\n", w);
2035 if (err == 0)
2036 PyFile_SoftSpace(w, 0);
2037 Py_DECREF(w);
2038 }
2039 Py_XDECREF(stream);
2040 stream = NULL;
2041 break;
1895 } 2042 }
1896 2043
1897 #ifdef CASE_TOO_BIG 2044 #ifdef CASE_TOO_BIG
1898 default: switch (opcode) { 2045 default: switch (opcode) {
1899 #endif 2046 #endif
1900 TARGET(RAISE_VARARGS) { 2047
1901 PyObject *cause = NULL, *exc = NULL; 2048 TARGET(RAISE_VARARGS)
2049 {
2050 u = v = w = NULL;
1902 switch (oparg) { 2051 switch (oparg) {
2052 case 3:
2053 u = POP(); /* traceback */
2054 /* Fallthrough */
1903 case 2: 2055 case 2:
1904 cause = POP(); /* cause */ 2056 v = POP(); /* value */
2057 /* Fallthrough */
1905 case 1: 2058 case 1:
1906 exc = POP(); /* exc */ 2059 w = POP(); /* exc */
1907 case 0: /* Fallthrough */ 2060 case 0: /* Fallthrough */
1908 if (do_raise(exc, cause)) { 2061 why = do_raise(w, v, u);
1909 why = WHY_EXCEPTION;
1910 goto fast_block_end;
1911 }
1912 break; 2062 break;
1913 default: 2063 default:
1914 PyErr_SetString(PyExc_SystemError, 2064 PyErr_SetString(PyExc_SystemError,
1915 "bad RAISE_VARARGS oparg"); 2065 "bad RAISE_VARARGS oparg");
2066 why = WHY_EXCEPTION;
1916 break; 2067 break;
1917 } 2068 }
1918 goto error; 2069 break;
1919 } 2070 }
1920 2071
1921 TARGET(RETURN_VALUE) { 2072 TARGET_NOARG(LOAD_LOCALS)
2073 {
2074 if ((x = f->f_locals) != NULL)
2075 {
2076 Py_INCREF(x);
2077 PUSH(x);
2078 DISPATCH();
2079 }
2080 PyErr_SetString(PyExc_SystemError, "no locals");
2081 break;
2082 }
2083
2084 TARGET_NOARG(RETURN_VALUE)
2085 {
1922 retval = POP(); 2086 retval = POP();
1923 why = WHY_RETURN; 2087 why = WHY_RETURN;
1924 goto fast_block_end; 2088 goto fast_block_end;
1925 } 2089 }
1926 2090
1927 TARGET(YIELD_FROM) { 2091 TARGET_NOARG(YIELD_VALUE)
1928 PyObject *v = POP(); 2092 {
1929 PyObject *reciever = TOP();
1930 int err;
1931 if (PyGen_CheckExact(reciever)) {
1932 retval = _PyGen_Send((PyGenObject *)reciever, v);
1933 } else {
1934 _Py_IDENTIFIER(send);
1935 if (v == Py_None)
1936 retval = Py_TYPE(reciever)->tp_iternext(reciever);
1937 else
1938 retval = _PyObject_CallMethodIdObjArgs(reciever, &PyId_send, v, NULL);
1939 }
1940 Py_DECREF(v);
1941 if (retval == NULL) {
1942 PyObject *val;
1943 if (tstate->c_tracefunc != NULL
1944 && PyErr_ExceptionMatches(PyExc_StopIteration))
1945 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tsta te, f);
1946 err = _PyGen_FetchStopIterationValue(&val);
1947 if (err < 0)
1948 goto error;
1949 Py_DECREF(reciever);
1950 SET_TOP(val);
1951 DISPATCH();
1952 }
1953 /* x remains on stack, retval is value to be yielded */
1954 f->f_stacktop = stack_pointer;
1955 why = WHY_YIELD;
1956 /* and repeat... */
1957 f->f_lasti--;
1958 goto fast_yield;
1959 }
1960
1961 TARGET(YIELD_VALUE) {
1962 retval = POP(); 2093 retval = POP();
1963 f->f_stacktop = stack_pointer; 2094 f->f_stacktop = stack_pointer;
1964 why = WHY_YIELD; 2095 why = WHY_YIELD;
1965 goto fast_yield; 2096 goto fast_yield;
1966 } 2097 }
1967 2098
1968 TARGET(POP_EXCEPT) { 2099 TARGET_NOARG(EXEC_STMT)
1969 PyTryBlock *b = PyFrame_BlockPop(f); 2100 {
1970 if (b->b_type != EXCEPT_HANDLER) { 2101 w = TOP();
1971 PyErr_SetString(PyExc_SystemError, 2102 v = SECOND();
1972 "popped block is not an except handler"); 2103 u = THIRD();
1973 goto error; 2104 STACKADJ(-3);
1974 } 2105 READ_TIMESTAMP(intr0);
1975 UNWIND_EXCEPT_HANDLER(b); 2106 err = exec_statement(f, u, v, w);
2107 READ_TIMESTAMP(intr1);
2108 Py_DECREF(u);
2109 Py_DECREF(v);
2110 Py_DECREF(w);
2111 break;
2112 }
2113
2114 TARGET_NOARG(POP_BLOCK)
2115 {
2116 {
2117 PyTryBlock *b = PyFrame_BlockPop(f);
2118 while (STACK_LEVEL() > b->b_level) {
2119 v = POP();
2120 Py_DECREF(v);
2121 }
2122 }
1976 DISPATCH(); 2123 DISPATCH();
1977 } 2124 }
1978 2125
1979 TARGET(POP_BLOCK) {
1980 PyTryBlock *b = PyFrame_BlockPop(f);
1981 UNWIND_BLOCK(b);
1982 DISPATCH();
1983 }
1984
1985 PREDICTED(END_FINALLY); 2126 PREDICTED(END_FINALLY);
1986 TARGET(END_FINALLY) { 2127 TARGET_NOARG(END_FINALLY)
1987 PyObject *status = POP(); 2128 {
1988 if (PyLong_Check(status)) { 2129 v = POP();
1989 why = (enum why_code) PyLong_AS_LONG(status); 2130 if (PyInt_Check(v)) {
1990 assert(why != WHY_YIELD && why != WHY_EXCEPTION); 2131 why = (enum why_code) PyInt_AS_LONG(v);
2132 assert(why != WHY_YIELD);
1991 if (why == WHY_RETURN || 2133 if (why == WHY_RETURN ||
1992 why == WHY_CONTINUE) 2134 why == WHY_CONTINUE)
1993 retval = POP(); 2135 retval = POP();
1994 if (why == WHY_SILENCED) { 2136 }
1995 /* An exception was silenced by 'with', we must 2137 else if (PyExceptionClass_Check(v) ||
1996 manually unwind the EXCEPT_HANDLER block which was 2138 PyString_Check(v)) {
1997 created when the exception was caught, otherwise 2139 w = POP();
1998 the stack will be in an inconsistent state. */ 2140 u = POP();
1999 PyTryBlock *b = PyFrame_BlockPop(f); 2141 PyErr_Restore(v, w, u);
2000 assert(b->b_type == EXCEPT_HANDLER); 2142 why = WHY_RERAISE;
2001 UNWIND_EXCEPT_HANDLER(b); 2143 break;
2002 why = WHY_NOT; 2144 }
2003 Py_DECREF(status); 2145 else if (v != Py_None) {
2004 DISPATCH();
2005 }
2006 Py_DECREF(status);
2007 goto fast_block_end;
2008 }
2009 else if (PyExceptionClass_Check(status)) {
2010 PyObject *exc = POP();
2011 PyObject *tb = POP();
2012 PyErr_Restore(status, exc, tb);
2013 why = WHY_EXCEPTION;
2014 goto fast_block_end;
2015 }
2016 else if (status != Py_None) {
2017 PyErr_SetString(PyExc_SystemError, 2146 PyErr_SetString(PyExc_SystemError,
2018 "'finally' pops bad exception"); 2147 "'finally' pops bad exception");
2019 Py_DECREF(status); 2148 why = WHY_EXCEPTION;
2020 goto error; 2149 }
2021 } 2150 Py_DECREF(v);
2022 Py_DECREF(status); 2151 break;
2023 DISPATCH(); 2152 }
2024 } 2153
2025 2154 TARGET_NOARG(BUILD_CLASS)
2026 TARGET(LOAD_BUILD_CLASS) { 2155 {
2027 _Py_IDENTIFIER(__build_class__); 2156 u = TOP();
2028 2157 v = SECOND();
2029 PyObject *bc; 2158 w = THIRD();
2030 if (PyDict_CheckExact(f->f_builtins)) { 2159 STACKADJ(-2);
2031 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__); 2160 x = build_class(u, v, w);
2032 if (bc == NULL) { 2161 SET_TOP(x);
2033 PyErr_SetString(PyExc_NameError, 2162 Py_DECREF(u);
2034 "__build_class__ not found"); 2163 Py_DECREF(v);
2035 goto error; 2164 Py_DECREF(w);
2165 break;
2166 }
2167
2168 TARGET(STORE_NAME)
2169 {
2170 w = GETITEM(names, oparg);
2171 v = POP();
2172 if ((x = f->f_locals) != NULL) {
2173 if (PyDict_CheckExact(x))
2174 err = PyDict_SetItem(x, w, v);
2175 else
2176 err = PyObject_SetItem(x, w, v);
2177 Py_DECREF(v);
2178 if (err == 0) DISPATCH();
2179 break;
2180 }
2181 t = PyObject_Repr(w);
2182 if (t == NULL)
2183 break;
2184 PyErr_Format(PyExc_SystemError,
2185 "no locals found when storing %s",
2186 PyString_AS_STRING(t));
2187 Py_DECREF(t);
2188 break;
2189 }
2190
2191 TARGET(DELETE_NAME)
2192 {
2193 w = GETITEM(names, oparg);
2194 if ((x = f->f_locals) != NULL) {
2195 if ((err = PyObject_DelItem(x, w)) != 0)
2196 format_exc_check_arg(PyExc_NameError,
2197 NAME_ERROR_MSG,
2198 w);
2199 break;
2200 }
2201 t = PyObject_Repr(w);
2202 if (t == NULL)
2203 break;
2204 PyErr_Format(PyExc_SystemError,
2205 "no locals when deleting %s",
2206 PyString_AS_STRING(w));
2207 Py_DECREF(t);
2208 break;
2209 }
2210
2211 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
2212 TARGET(UNPACK_SEQUENCE)
2213 {
2214 v = POP();
2215 if (PyTuple_CheckExact(v) &&
2216 PyTuple_GET_SIZE(v) == oparg) {
2217 PyObject **items = \
2218 ((PyTupleObject *)v)->ob_item;
2219 while (oparg--) {
2220 w = items[oparg];
2221 Py_INCREF(w);
2222 PUSH(w);
2036 } 2223 }
2037 Py_INCREF(bc); 2224 Py_DECREF(v);
2038 } 2225 DISPATCH();
2039 else { 2226 } else if (PyList_CheckExact(v) &&
2040 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_clas s__); 2227 PyList_GET_SIZE(v) == oparg) {
2041 if (build_class_str == NULL) 2228 PyObject **items = \
2042 break; 2229 ((PyListObject *)v)->ob_item;
2043 bc = PyObject_GetItem(f->f_builtins, build_class_str); 2230 while (oparg--) {
2044 if (bc == NULL) { 2231 w = items[oparg];
2045 if (PyErr_ExceptionMatches(PyExc_KeyError)) 2232 Py_INCREF(w);
2046 PyErr_SetString(PyExc_NameError, 2233 PUSH(w);
2047 "__build_class__ not found");
2048 goto error;
2049 } 2234 }
2050 } 2235 } else if (unpack_iterable(v, oparg,
2051 PUSH(bc);
2052 DISPATCH();
2053 }
2054
2055 TARGET(STORE_NAME) {
2056 PyObject *name = GETITEM(names, oparg);
2057 PyObject *v = POP();
2058 PyObject *ns = f->f_locals;
2059 int err;
2060 if (ns == NULL) {
2061 PyErr_Format(PyExc_SystemError,
2062 "no locals found when storing %R", name);
2063 Py_DECREF(v);
2064 goto error;
2065 }
2066 if (PyDict_CheckExact(ns))
2067 err = PyDict_SetItem(ns, name, v);
2068 else
2069 err = PyObject_SetItem(ns, name, v);
2070 Py_DECREF(v);
2071 if (err != 0)
2072 goto error;
2073 DISPATCH();
2074 }
2075
2076 TARGET(DELETE_NAME) {
2077 PyObject *name = GETITEM(names, oparg);
2078 PyObject *ns = f->f_locals;
2079 int err;
2080 if (ns == NULL) {
2081 PyErr_Format(PyExc_SystemError,
2082 "no locals when deleting %R", name);
2083 goto error;
2084 }
2085 err = PyObject_DelItem(ns, name);
2086 if (err != 0) {
2087 format_exc_check_arg(PyExc_NameError,
2088 NAME_ERROR_MSG,
2089 name);
2090 goto error;
2091 }
2092 DISPATCH();
2093 }
2094
2095 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
2096 TARGET(UNPACK_SEQUENCE) {
2097 PyObject *seq = POP(), *item, **items;
2098 if (PyTuple_CheckExact(seq) &&
2099 PyTuple_GET_SIZE(seq) == oparg) {
2100 items = ((PyTupleObject *)seq)->ob_item;
2101 while (oparg--) {
2102 item = items[oparg];
2103 Py_INCREF(item);
2104 PUSH(item);
2105 }
2106 } else if (PyList_CheckExact(seq) &&
2107 PyList_GET_SIZE(seq) == oparg) {
2108 items = ((PyListObject *)seq)->ob_item;
2109 while (oparg--) {
2110 item = items[oparg];
2111 Py_INCREF(item);
2112 PUSH(item);
2113 }
2114 } else if (unpack_iterable(seq, oparg, -1,
2115 stack_pointer + oparg)) { 2236 stack_pointer + oparg)) {
2116 STACKADJ(oparg); 2237 STACKADJ(oparg);
2117 } else { 2238 } else {
2118 /* unpack_iterable() raised an exception */ 2239 /* unpack_iterable() raised an exception */
2119 Py_DECREF(seq); 2240 why = WHY_EXCEPTION;
2120 goto error; 2241 }
2121 } 2242 Py_DECREF(v);
2122 Py_DECREF(seq); 2243 break;
2123 DISPATCH(); 2244 }
2124 } 2245
2125 2246
2126 TARGET(UNPACK_EX) { 2247 TARGET(STORE_ATTR)
2127 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8); 2248 {
2128 PyObject *seq = POP(); 2249 w = GETITEM(names, oparg);
2129 2250 v = TOP();
2130 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8, 2251 u = SECOND();
2131 stack_pointer + totalargs)) {
2132 stack_pointer += totalargs;
2133 } else {
2134 Py_DECREF(seq);
2135 goto error;
2136 }
2137 Py_DECREF(seq);
2138 DISPATCH();
2139 }
2140
2141 TARGET(STORE_ATTR) {
2142 PyObject *name = GETITEM(names, oparg);
2143 PyObject *owner = TOP();
2144 PyObject *v = SECOND();
2145 int err;
2146 STACKADJ(-2); 2252 STACKADJ(-2);
2147 err = PyObject_SetAttr(owner, name, v); 2253 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2148 Py_DECREF(v); 2254 Py_DECREF(v);
2149 Py_DECREF(owner); 2255 Py_DECREF(u);
2150 if (err != 0) 2256 if (err == 0) DISPATCH();
2151 goto error; 2257 break;
2152 DISPATCH(); 2258 }
2153 } 2259
2154 2260 TARGET(DELETE_ATTR)
2155 TARGET(DELETE_ATTR) { 2261 {
2156 PyObject *name = GETITEM(names, oparg); 2262 w = GETITEM(names, oparg);
2157 PyObject *owner = POP(); 2263 v = POP();
2158 int err; 2264 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2159 err = PyObject_SetAttr(owner, name, (PyObject *)NULL); 2265 /* del v.w */
2160 Py_DECREF(owner);
2161 if (err != 0)
2162 goto error;
2163 DISPATCH();
2164 }
2165
2166 TARGET(STORE_GLOBAL) {
2167 PyObject *name = GETITEM(names, oparg);
2168 PyObject *v = POP();
2169 int err;
2170 err = PyDict_SetItem(f->f_globals, name, v);
2171 Py_DECREF(v); 2266 Py_DECREF(v);
2172 if (err != 0) 2267 break;
2173 goto error; 2268 }
2174 DISPATCH(); 2269
2175 } 2270
2176 2271 TARGET(STORE_GLOBAL)
2177 TARGET(DELETE_GLOBAL) { 2272 {
2178 PyObject *name = GETITEM(names, oparg); 2273 w = GETITEM(names, oparg);
2179 int err; 2274 v = POP();
2180 err = PyDict_DelItem(f->f_globals, name); 2275 err = PyDict_SetItem(f->f_globals, w, v);
2181 if (err != 0) { 2276 Py_DECREF(v);
2277 if (err == 0) DISPATCH();
2278 break;
2279 }
2280
2281 TARGET(DELETE_GLOBAL)
2282 {
2283 w = GETITEM(names, oparg);
2284 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2182 format_exc_check_arg( 2285 format_exc_check_arg(
2183 PyExc_NameError, NAME_ERROR_MSG, name); 2286 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2184 goto error; 2287 break;
2185 } 2288 }
2186 DISPATCH(); 2289
2187 } 2290 TARGET(LOAD_NAME)
2188 2291 {
2189 TARGET(LOAD_NAME) { 2292 w = GETITEM(names, oparg);
2190 PyObject *name = GETITEM(names, oparg); 2293 if ((v = f->f_locals) == NULL) {
2191 PyObject *locals = f->f_locals; 2294 why = WHY_EXCEPTION;
2192 PyObject *v; 2295 t = PyObject_Repr(w);
2193 if (locals == NULL) { 2296 if (t == NULL)
2297 break;
2194 PyErr_Format(PyExc_SystemError, 2298 PyErr_Format(PyExc_SystemError,
2195 "no locals when loading %R", name); 2299 "no locals when loading %s",
2196 goto error; 2300 PyString_AS_STRING(w));
2197 } 2301 Py_DECREF(t);
2198 if (PyDict_CheckExact(locals)) { 2302 break;
2199 v = PyDict_GetItem(locals, name); 2303 }
2200 Py_XINCREF(v); 2304 if (PyDict_CheckExact(v)) {
2305 x = PyDict_GetItem(v, w);
2306 Py_XINCREF(x);
2201 } 2307 }
2202 else { 2308 else {
2203 v = PyObject_GetItem(locals, name); 2309 x = PyObject_GetItem(v, w);
2204 if (v == NULL && _PyErr_OCCURRED()) { 2310 if (x == NULL && PyErr_Occurred()) {
2205 if (!PyErr_ExceptionMatches(PyExc_KeyError)) 2311 if (!PyErr_ExceptionMatches(
2206 goto error; 2312 PyExc_KeyError))
2313 break;
2207 PyErr_Clear(); 2314 PyErr_Clear();
2208 } 2315 }
2209 } 2316 }
2210 if (v == NULL) { 2317 if (x == NULL) {
2211 v = PyDict_GetItem(f->f_globals, name); 2318 x = PyDict_GetItem(f->f_globals, w);
2212 Py_XINCREF(v); 2319 if (x == NULL) {
2213 if (v == NULL) { 2320 x = PyDict_GetItem(f->f_builtins, w);
2214 if (PyDict_CheckExact(f->f_builtins)) { 2321 if (x == NULL) {
2215 v = PyDict_GetItem(f->f_builtins, name); 2322 format_exc_check_arg(
2216 if (v == NULL) { 2323 PyExc_NameError,
2217 format_exc_check_arg( 2324 NAME_ERROR_MSG, w);
2218 PyExc_NameError, 2325 break;
2219 NAME_ERROR_MSG, name);
2220 goto error;
2221 }
2222 Py_INCREF(v);
2223 }
2224 else {
2225 v = PyObject_GetItem(f->f_builtins, name);
2226 if (v == NULL) {
2227 if (PyErr_ExceptionMatches(PyExc_KeyError))
2228 format_exc_check_arg(
2229 PyExc_NameError,
2230 NAME_ERROR_MSG, name);
2231 goto error;
2232 }
2233 } 2326 }
2234 } 2327 }
2235 } 2328 Py_INCREF(x);
2236 PUSH(v); 2329 }
2330 PUSH(x);
2237 DISPATCH(); 2331 DISPATCH();
2238 } 2332 }
2239 2333
2240 TARGET(LOAD_GLOBAL) { 2334 TARGET(LOAD_GLOBAL)
2241 PyObject *name = GETITEM(names, oparg); 2335 {
2242 PyObject *v; 2336 w = GETITEM(names, oparg);
2243 if (PyDict_CheckExact(f->f_globals) 2337 if (PyString_CheckExact(w)) {
2244 && PyDict_CheckExact(f->f_builtins)) { 2338 /* Inline the PyDict_GetItem() calls.
2245 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals, 2339 WARNING: this is an extreme speed hack.
2246 (PyDictObject *)f->f_builtins, 2340 Do not try this at home. */
2247 name); 2341 long hash = ((PyStringObject *)w)->ob_shash;
2248 if (v == NULL) { 2342 if (hash != -1) {
2249 if (!_PyErr_OCCURRED()) 2343 PyDictObject *d;
2250 format_exc_check_arg(PyExc_NameError, 2344 PyDictEntry *e;
2251 NAME_ERROR_MSG, name); 2345 d = (PyDictObject *)(f->f_globals);
2252 goto error; 2346 e = d->ma_lookup(d, w, hash);
2347 if (e == NULL) {
2348 x = NULL;
2349 break;
2350 }
2351 x = e->me_value;
2352 if (x != NULL) {
2353 Py_INCREF(x);
2354 PUSH(x);
2355 DISPATCH();
2356 }
2357 d = (PyDictObject *)(f->f_builtins);
2358 e = d->ma_lookup(d, w, hash);
2359 if (e == NULL) {
2360 x = NULL;
2361 break;
2362 }
2363 x = e->me_value;
2364 if (x != NULL) {
2365 Py_INCREF(x);
2366 PUSH(x);
2367 DISPATCH();
2368 }
2369 goto load_global_error;
2253 } 2370 }
2254 Py_INCREF(v); 2371 }
2255 } 2372 /* This is the un-inlined version of the code above */
2256 else { 2373 x = PyDict_GetItem(f->f_globals, w);
2257 /* Slow-path if globals or builtins is not a dict */ 2374 if (x == NULL) {
2258 v = PyObject_GetItem(f->f_globals, name); 2375 x = PyDict_GetItem(f->f_builtins, w);
2259 if (v == NULL) { 2376 if (x == NULL) {
2260 v = PyObject_GetItem(f->f_builtins, name); 2377 load_global_error:
2261 if (v == NULL) { 2378 format_exc_check_arg(
2262 if (PyErr_ExceptionMatches(PyExc_KeyError)) 2379 PyExc_NameError,
2263 format_exc_check_arg( 2380 GLOBAL_NAME_ERROR_MSG, w);
2264 PyExc_NameError, 2381 break;
2265 NAME_ERROR_MSG, name);
2266 goto error;
2267 }
2268 } 2382 }
2269 } 2383 }
2270 PUSH(v); 2384 Py_INCREF(x);
2385 PUSH(x);
2271 DISPATCH(); 2386 DISPATCH();
2272 } 2387 }
2273 2388
2274 TARGET(DELETE_FAST) { 2389 TARGET(DELETE_FAST)
2275 PyObject *v = GETLOCAL(oparg); 2390 {
2276 if (v != NULL) { 2391 x = GETLOCAL(oparg);
2392 if (x != NULL) {
2277 SETLOCAL(oparg, NULL); 2393 SETLOCAL(oparg, NULL);
2278 DISPATCH(); 2394 DISPATCH();
2279 } 2395 }
2280 format_exc_check_arg( 2396 format_exc_check_arg(
2281 PyExc_UnboundLocalError, 2397 PyExc_UnboundLocalError,
2282 UNBOUNDLOCAL_ERROR_MSG, 2398 UNBOUNDLOCAL_ERROR_MSG,
2283 PyTuple_GetItem(co->co_varnames, oparg) 2399 PyTuple_GetItem(co->co_varnames, oparg)
2284 ); 2400 );
2285 goto error; 2401 break;
2286 } 2402 }
2287 2403
2288 TARGET(DELETE_DEREF) { 2404 TARGET(LOAD_CLOSURE)
2289 PyObject *cell = freevars[oparg]; 2405 {
2290 if (PyCell_GET(cell) != NULL) { 2406 x = freevars[oparg];
2291 PyCell_Set(cell, NULL); 2407 Py_INCREF(x);
2408 PUSH(x);
2409 if (x != NULL) DISPATCH();
2410 break;
2411 }
2412
2413 TARGET(LOAD_DEREF)
2414 {
2415 x = freevars[oparg];
2416 w = PyCell_Get(x);
2417 if (w != NULL) {
2418 PUSH(w);
2292 DISPATCH(); 2419 DISPATCH();
2293 } 2420 }
2294 format_exc_unbound(co, oparg); 2421 err = -1;
2295 goto error; 2422 /* Don't stomp existing exception */
2296 } 2423 if (PyErr_Occurred())
2297 2424 break;
2298 TARGET(LOAD_CLOSURE) { 2425 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2299 PyObject *cell = freevars[oparg]; 2426 v = PyTuple_GET_ITEM(co->co_cellvars,
2300 Py_INCREF(cell); 2427 oparg);
2301 PUSH(cell); 2428 format_exc_check_arg(
2429 PyExc_UnboundLocalError,
2430 UNBOUNDLOCAL_ERROR_MSG,
2431 v);
2432 } else {
2433 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2434 PyTuple_GET_SIZE(co->co_cellvars));
2435 format_exc_check_arg(PyExc_NameError,
2436 UNBOUNDFREE_ERROR_MSG, v);
2437 }
2438 break;
2439 }
2440
2441 TARGET(STORE_DEREF)
2442 {
2443 w = POP();
2444 x = freevars[oparg];
2445 PyCell_Set(x, w);
2446 Py_DECREF(w);
2302 DISPATCH(); 2447 DISPATCH();
2303 } 2448 }
2304 2449
2305 TARGET(LOAD_CLASSDEREF) { 2450 TARGET(BUILD_TUPLE)
2306 PyObject *name, *value, *locals = f->f_locals; 2451 {
2307 Py_ssize_t idx; 2452 x = PyTuple_New(oparg);
2308 assert(locals); 2453 if (x != NULL) {
2309 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars)); 2454 for (; --oparg >= 0;) {
2310 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars); 2455 w = POP();
2311 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars)); 2456 PyTuple_SET_ITEM(x, oparg, w);
2312 name = PyTuple_GET_ITEM(co->co_freevars, idx); 2457 }
2313 if (PyDict_CheckExact(locals)) { 2458 PUSH(x);
2314 value = PyDict_GetItem(locals, name); 2459 DISPATCH();
2315 Py_XINCREF(value); 2460 }
2461 break;
2462 }
2463
2464 TARGET(BUILD_LIST)
2465 {
2466 x = PyList_New(oparg);
2467 if (x != NULL) {
2468 for (; --oparg >= 0;) {
2469 w = POP();
2470 PyList_SET_ITEM(x, oparg, w);
2471 }
2472 PUSH(x);
2473 DISPATCH();
2474 }
2475 break;
2476 }
2477
2478 TARGET(BUILD_SET)
2479 {
2480 x = PySet_New(NULL);
2481 if (x != NULL) {
2482 for (; --oparg >= 0;) {
2483 w = POP();
2484 if (err == 0)
2485 err = PySet_Add(x, w);
2486 Py_DECREF(w);
2487 }
2488 if (err != 0) {
2489 Py_DECREF(x);
2490 break;
2491 }
2492 PUSH(x);
2493 DISPATCH();
2494 }
2495 break;
2496 }
2497
2498 TARGET(BUILD_MAP)
2499 {
2500 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2501 PUSH(x);
2502 if (x != NULL) DISPATCH();
2503 break;
2504 }
2505
2506 TARGET_NOARG(STORE_MAP)
2507 {
2508 w = TOP(); /* key */
2509 u = SECOND(); /* value */
2510 v = THIRD(); /* dict */
2511 STACKADJ(-2);
2512 assert (PyDict_CheckExact(v));
2513 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2514 Py_DECREF(u);
2515 Py_DECREF(w);
2516 if (err == 0) DISPATCH();
2517 break;
2518 }
2519
2520 TARGET(MAP_ADD)
2521 {
2522 w = TOP(); /* key */
2523 u = SECOND(); /* value */
2524 STACKADJ(-2);
2525 v = stack_pointer[-oparg]; /* dict */
2526 assert (PyDict_CheckExact(v));
2527 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2528 Py_DECREF(u);
2529 Py_DECREF(w);
2530 if (err == 0) {
2531 PREDICT(JUMP_ABSOLUTE);
2532 DISPATCH();
2533 }
2534 break;
2535 }
2536
2537 TARGET(LOAD_ATTR)
2538 {
2539 w = GETITEM(names, oparg);
2540 v = TOP();
2541 x = PyObject_GetAttr(v, w);
2542 Py_DECREF(v);
2543 SET_TOP(x);
2544 if (x != NULL) DISPATCH();
2545 break;
2546 }
2547
2548 TARGET(COMPARE_OP)
2549 {
2550 w = POP();
2551 v = TOP();
2552 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
2553 /* INLINE: cmp(int, int) */
2554 register long a, b;
2555 register int res;
2556 a = PyInt_AS_LONG(v);
2557 b = PyInt_AS_LONG(w);
2558 switch (oparg) {
2559 case PyCmp_LT: res = a < b; break;
2560 case PyCmp_LE: res = a <= b; break;
2561 case PyCmp_EQ: res = a == b; break;
2562 case PyCmp_NE: res = a != b; break;
2563 case PyCmp_GT: res = a > b; break;
2564 case PyCmp_GE: res = a >= b; break;
2565 case PyCmp_IS: res = v == w; break;
2566 case PyCmp_IS_NOT: res = v != w; break;
2567 default: goto slow_compare;
2568 }
2569 x = res ? Py_True : Py_False;
2570 Py_INCREF(x);
2316 } 2571 }
2317 else { 2572 else {
2318 value = PyObject_GetItem(locals, name); 2573 slow_compare:
2319 if (value == NULL && PyErr_Occurred()) { 2574 x = cmp_outcome(oparg, v, w);
2320 if (!PyErr_ExceptionMatches(PyExc_KeyError)) 2575 }
2321 goto error;
2322 PyErr_Clear();
2323 }
2324 }
2325 if (!value) {
2326 PyObject *cell = freevars[oparg];
2327 value = PyCell_GET(cell);
2328 if (value == NULL) {
2329 format_exc_unbound(co, oparg);
2330 goto error;
2331 }
2332 Py_INCREF(value);
2333 }
2334 PUSH(value);
2335 DISPATCH();
2336 }
2337
2338 TARGET(LOAD_DEREF) {
2339 PyObject *cell = freevars[oparg];
2340 PyObject *value = PyCell_GET(cell);
2341 if (value == NULL) {
2342 format_exc_unbound(co, oparg);
2343 goto error;
2344 }
2345 Py_INCREF(value);
2346 PUSH(value);
2347 DISPATCH();
2348 }
2349
2350 TARGET(STORE_DEREF) {
2351 PyObject *v = POP();
2352 PyObject *cell = freevars[oparg];
2353 PyCell_Set(cell, v);
2354 Py_DECREF(v); 2576 Py_DECREF(v);
2355 DISPATCH(); 2577 Py_DECREF(w);
2356 } 2578 SET_TOP(x);
2357 2579 if (x == NULL) break;
2358 TARGET(BUILD_TUPLE) {
2359 PyObject *tup = PyTuple_New(oparg);
2360 if (tup == NULL)
2361 goto error;
2362 while (--oparg >= 0) {
2363 PyObject *item = POP();
2364 PyTuple_SET_ITEM(tup, oparg, item);
2365 }
2366 PUSH(tup);
2367 DISPATCH();
2368 }
2369
2370 TARGET(BUILD_LIST) {
2371 PyObject *list = PyList_New(oparg);
2372 if (list == NULL)
2373 goto error;
2374 while (--oparg >= 0) {
2375 PyObject *item = POP();
2376 PyList_SET_ITEM(list, oparg, item);
2377 }
2378 PUSH(list);
2379 DISPATCH();
2380 }
2381
2382 TARGET(BUILD_SET) {
2383 PyObject *set = PySet_New(NULL);
2384 int err = 0;
2385 if (set == NULL)
2386 goto error;
2387 while (--oparg >= 0) {
2388 PyObject *item = POP();
2389 if (err == 0)
2390 err = PySet_Add(set, item);
2391 Py_DECREF(item);
2392 }
2393 if (err != 0) {
2394 Py_DECREF(set);
2395 goto error;
2396 }
2397 PUSH(set);
2398 DISPATCH();
2399 }
2400
2401 TARGET(BUILD_MAP) {
2402 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2403 if (map == NULL)
2404 goto error;
2405 PUSH(map);
2406 DISPATCH();
2407 }
2408
2409 TARGET(STORE_MAP) {
2410 PyObject *key = TOP();
2411 PyObject *value = SECOND();
2412 PyObject *map = THIRD();
2413 int err;
2414 STACKADJ(-2);
2415 assert(PyDict_CheckExact(map));
2416 err = PyDict_SetItem(map, key, value);
2417 Py_DECREF(value);
2418 Py_DECREF(key);
2419 if (err != 0)
2420 goto error;
2421 DISPATCH();
2422 }
2423
2424 TARGET(MAP_ADD) {
2425 PyObject *key = TOP();
2426 PyObject *value = SECOND();
2427 PyObject *map;
2428 int err;
2429 STACKADJ(-2);
2430 map = stack_pointer[-oparg]; /* dict */
2431 assert(PyDict_CheckExact(map));
2432 err = PyDict_SetItem(map, key, value); /* v[w] = u */
2433 Py_DECREF(value);
2434 Py_DECREF(key);
2435 if (err != 0)
2436 goto error;
2437 PREDICT(JUMP_ABSOLUTE);
2438 DISPATCH();
2439 }
2440
2441 TARGET(LOAD_ATTR) {
2442 PyObject *name = GETITEM(names, oparg);
2443 PyObject *owner = TOP();
2444 PyObject *res = PyObject_GetAttr(owner, name);
2445 Py_DECREF(owner);
2446 SET_TOP(res);
2447 if (res == NULL)
2448 goto error;
2449 DISPATCH();
2450 }
2451
2452 TARGET(COMPARE_OP) {
2453 PyObject *right = POP();
2454 PyObject *left = TOP();
2455 PyObject *res = cmp_outcome(oparg, left, right);
2456 Py_DECREF(left);
2457 Py_DECREF(right);
2458 SET_TOP(res);
2459 if (res == NULL)
2460 goto error;
2461 PREDICT(POP_JUMP_IF_FALSE); 2580 PREDICT(POP_JUMP_IF_FALSE);
2462 PREDICT(POP_JUMP_IF_TRUE); 2581 PREDICT(POP_JUMP_IF_TRUE);
2463 DISPATCH(); 2582 DISPATCH();
2464 } 2583 }
2465 2584
2466 TARGET(IMPORT_NAME) { 2585 TARGET(IMPORT_NAME)
2467 _Py_IDENTIFIER(__import__); 2586 {
2468 PyObject *name = GETITEM(names, oparg); 2587 w = GETITEM(names, oparg);
2469 PyObject *func = _PyDict_GetItemId(f->f_builtins, &PyId___import__); 2588 x = PyDict_GetItemString(f->f_builtins, "__import__");
2470 PyObject *from, *level, *args, *res; 2589 if (x == NULL) {
2471 if (func == NULL) {
2472 PyErr_SetString(PyExc_ImportError, 2590 PyErr_SetString(PyExc_ImportError,
2473 "__import__ not found"); 2591 "__import__ not found");
2474 goto error; 2592 break;
2475 } 2593 }
2476 Py_INCREF(func); 2594 Py_INCREF(x);
2477 from = POP(); 2595 v = POP();
2478 level = TOP(); 2596 u = TOP();
2479 if (PyLong_AsLong(level) != -1 || PyErr_Occurred()) 2597 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2480 args = PyTuple_Pack(5, 2598 w = PyTuple_Pack(5,
2481 name, 2599 w,
2482 f->f_globals, 2600 f->f_globals,
2483 f->f_locals == NULL ? 2601 f->f_locals == NULL ?
2484 Py_None : f->f_locals, 2602 Py_None : f->f_locals,
2485 from, 2603 v,
2486 level); 2604 u);
2487 else 2605 else
2488 args = PyTuple_Pack(4, 2606 w = PyTuple_Pack(4,
2489 name, 2607 w,
2490 f->f_globals, 2608 f->f_globals,
2491 f->f_locals == NULL ? 2609 f->f_locals == NULL ?
2492 Py_None : f->f_locals, 2610 Py_None : f->f_locals,
2493 from); 2611 v);
2494 Py_DECREF(level); 2612 Py_DECREF(v);
2495 Py_DECREF(from); 2613 Py_DECREF(u);
2496 if (args == NULL) { 2614 if (w == NULL) {
2497 Py_DECREF(func); 2615 u = POP();
2498 STACKADJ(-1); 2616 Py_DECREF(x);
2499 goto error; 2617 x = NULL;
2618 break;
2500 } 2619 }
2501 READ_TIMESTAMP(intr0); 2620 READ_TIMESTAMP(intr0);
2502 res = PyEval_CallObject(func, args); 2621 v = x;
2622 x = PyEval_CallObject(v, w);
2623 Py_DECREF(v);
2503 READ_TIMESTAMP(intr1); 2624 READ_TIMESTAMP(intr1);
2504 Py_DECREF(args); 2625 Py_DECREF(w);
2505 Py_DECREF(func); 2626 SET_TOP(x);
2506 SET_TOP(res); 2627 if (x != NULL) DISPATCH();
2507 if (res == NULL) 2628 break;
2508 goto error; 2629 }
2509 DISPATCH(); 2630
2510 } 2631 TARGET_NOARG(IMPORT_STAR)
2511 2632 {
2512 TARGET(IMPORT_STAR) { 2633 v = POP();
2513 PyObject *from = POP(), *locals; 2634 PyFrame_FastToLocals(f);
2514 int err; 2635 if ((x = f->f_locals) == NULL) {
2515 if (PyFrame_FastToLocalsWithError(f) < 0)
2516 goto error;
2517
2518 locals = f->f_locals;
2519 if (locals == NULL) {
2520 PyErr_SetString(PyExc_SystemError, 2636 PyErr_SetString(PyExc_SystemError,
2521 "no locals found during 'import *'"); 2637 "no locals found during 'import *'");
2522 goto error; 2638 break;
2523 } 2639 }
2524 READ_TIMESTAMP(intr0); 2640 READ_TIMESTAMP(intr0);
2525 err = import_all_from(locals, from); 2641 err = import_all_from(x, v);
2526 READ_TIMESTAMP(intr1); 2642 READ_TIMESTAMP(intr1);
2527 PyFrame_LocalsToFast(f, 0); 2643 PyFrame_LocalsToFast(f, 0);
2528 Py_DECREF(from); 2644 Py_DECREF(v);
2529 if (err != 0) 2645 if (err == 0) DISPATCH();
2530 goto error; 2646 break;
2531 DISPATCH(); 2647 }
2532 } 2648
2533 2649 TARGET(IMPORT_FROM)
2534 TARGET(IMPORT_FROM) { 2650 {
2535 PyObject *name = GETITEM(names, oparg); 2651 w = GETITEM(names, oparg);
2536 PyObject *from = TOP(); 2652 v = TOP();
2537 PyObject *res;
2538 READ_TIMESTAMP(intr0); 2653 READ_TIMESTAMP(intr0);
2539 res = import_from(from, name); 2654 x = import_from(v, w);
2540 READ_TIMESTAMP(intr1); 2655 READ_TIMESTAMP(intr1);
2541 PUSH(res); 2656 PUSH(x);
2542 if (res == NULL) 2657 if (x != NULL) DISPATCH();
2543 goto error; 2658 break;
2544 DISPATCH(); 2659 }
2545 } 2660
2546 2661 TARGET(JUMP_FORWARD)
2547 TARGET(JUMP_FORWARD) { 2662 {
2548 JUMPBY(oparg); 2663 JUMPBY(oparg);
2549 FAST_DISPATCH(); 2664 FAST_DISPATCH();
2550 } 2665 }
2551 2666
2552 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE); 2667 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2553 TARGET(POP_JUMP_IF_FALSE) { 2668 TARGET(POP_JUMP_IF_FALSE)
2554 PyObject *cond = POP(); 2669 {
2555 int err; 2670 w = POP();
2556 if (cond == Py_True) { 2671 if (w == Py_True) {
2557 Py_DECREF(cond); 2672 Py_DECREF(w);
2558 FAST_DISPATCH(); 2673 FAST_DISPATCH();
2559 } 2674 }
2560 if (cond == Py_False) { 2675 if (w == Py_False) {
2561 Py_DECREF(cond); 2676 Py_DECREF(w);
2562 JUMPTO(oparg); 2677 JUMPTO(oparg);
2563 FAST_DISPATCH(); 2678 FAST_DISPATCH();
2564 } 2679 }
2565 err = PyObject_IsTrue(cond); 2680 err = PyObject_IsTrue(w);
2566 Py_DECREF(cond); 2681 Py_DECREF(w);
2567 if (err > 0) 2682 if (err > 0)
2568 err = 0; 2683 err = 0;
2569 else if (err == 0) 2684 else if (err == 0)
2570 JUMPTO(oparg); 2685 JUMPTO(oparg);
2571 else 2686 else
2572 goto error; 2687 break;
2573 DISPATCH(); 2688 DISPATCH();
2574 } 2689 }
2575 2690
2576 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE); 2691 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2577 TARGET(POP_JUMP_IF_TRUE) { 2692 TARGET(POP_JUMP_IF_TRUE)
2578 PyObject *cond = POP(); 2693 {
2579 int err; 2694 w = POP();
2580 if (cond == Py_False) { 2695 if (w == Py_False) {
2581 Py_DECREF(cond); 2696 Py_DECREF(w);
2582 FAST_DISPATCH(); 2697 FAST_DISPATCH();
2583 } 2698 }
2584 if (cond == Py_True) { 2699 if (w == Py_True) {
2585 Py_DECREF(cond); 2700 Py_DECREF(w);
2586 JUMPTO(oparg); 2701 JUMPTO(oparg);
2587 FAST_DISPATCH(); 2702 FAST_DISPATCH();
2588 } 2703 }
2589 err = PyObject_IsTrue(cond); 2704 err = PyObject_IsTrue(w);
2590 Py_DECREF(cond); 2705 Py_DECREF(w);
2591 if (err > 0) { 2706 if (err > 0) {
2592 err = 0; 2707 err = 0;
2593 JUMPTO(oparg); 2708 JUMPTO(oparg);
2594 } 2709 }
2595 else if (err == 0) 2710 else if (err == 0)
2596 ; 2711 ;
2597 else 2712 else
2598 goto error; 2713 break;
2599 DISPATCH(); 2714 DISPATCH();
2600 } 2715 }
2601 2716
2602 TARGET(JUMP_IF_FALSE_OR_POP) { 2717 TARGET(JUMP_IF_FALSE_OR_POP)
2603 PyObject *cond = TOP(); 2718 {
2604 int err; 2719 w = TOP();
2605 if (cond == Py_True) { 2720 if (w == Py_True) {
2606 STACKADJ(-1); 2721 STACKADJ(-1);
2607 Py_DECREF(cond); 2722 Py_DECREF(w);
2608 FAST_DISPATCH(); 2723 FAST_DISPATCH();
2609 } 2724 }
2610 if (cond == Py_False) { 2725 if (w == Py_False) {
2611 JUMPTO(oparg); 2726 JUMPTO(oparg);
2612 FAST_DISPATCH(); 2727 FAST_DISPATCH();
2613 } 2728 }
2614 err = PyObject_IsTrue(cond); 2729 err = PyObject_IsTrue(w);
2615 if (err > 0) { 2730 if (err > 0) {
2616 STACKADJ(-1); 2731 STACKADJ(-1);
2617 Py_DECREF(cond); 2732 Py_DECREF(w);
2618 err = 0; 2733 err = 0;
2619 } 2734 }
2620 else if (err == 0) 2735 else if (err == 0)
2621 JUMPTO(oparg); 2736 JUMPTO(oparg);
2622 else 2737 else
2623 goto error; 2738 break;
2624 DISPATCH(); 2739 DISPATCH();
2625 } 2740 }
2626 2741
2627 TARGET(JUMP_IF_TRUE_OR_POP) { 2742 TARGET(JUMP_IF_TRUE_OR_POP)
2628 PyObject *cond = TOP(); 2743 {
2629 int err; 2744 w = TOP();
2630 if (cond == Py_False) { 2745 if (w == Py_False) {
2631 STACKADJ(-1); 2746 STACKADJ(-1);
2632 Py_DECREF(cond); 2747 Py_DECREF(w);
2633 FAST_DISPATCH(); 2748 FAST_DISPATCH();
2634 } 2749 }
2635 if (cond == Py_True) { 2750 if (w == Py_True) {
2636 JUMPTO(oparg); 2751 JUMPTO(oparg);
2637 FAST_DISPATCH(); 2752 FAST_DISPATCH();
2638 } 2753 }
2639 err = PyObject_IsTrue(cond); 2754 err = PyObject_IsTrue(w);
2640 if (err > 0) { 2755 if (err > 0) {
2641 err = 0; 2756 err = 0;
2642 JUMPTO(oparg); 2757 JUMPTO(oparg);
2643 } 2758 }
2644 else if (err == 0) { 2759 else if (err == 0) {
2645 STACKADJ(-1); 2760 STACKADJ(-1);
2646 Py_DECREF(cond); 2761 Py_DECREF(w);
2647 } 2762 }
2648 else 2763 else
2649 goto error; 2764 break;
2650 DISPATCH(); 2765 DISPATCH();
2651 } 2766 }
2652 2767
2653 PREDICTED_WITH_ARG(JUMP_ABSOLUTE); 2768 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2654 TARGET(JUMP_ABSOLUTE) { 2769 TARGET(JUMP_ABSOLUTE)
2770 {
2655 JUMPTO(oparg); 2771 JUMPTO(oparg);
2656 #if FAST_LOOPS 2772 #if FAST_LOOPS
2657 /* Enabling this path speeds-up all while and for-loops by bypassing 2773 /* Enabling this path speeds-up all while and for-loops by bypassing
2658 the per-loop checks for signals. By default, this should be turn ed-off 2774 the per-loop checks for signals. By default, this should be turn ed-off
2659 because it prevents detection of a control-break in tight loops l ike 2775 because it prevents detection of a control-break in tight loops l ike
2660 "while 1: pass". Compile with this option turned-on when you nee d 2776 "while 1: pass". Compile with this option turned-on when you nee d
2661 the speed-up and do not need break checking inside tight loops (o nes 2777 the speed-up and do not need break checking inside tight loops (o nes
2662 that contain only instructions ending with FAST_DISPATCH). 2778 that contain only instructions ending with goto fast_next_opcode) .
2663 */ 2779 */
2664 FAST_DISPATCH(); 2780 goto fast_next_opcode;
2665 #else 2781 #else
2666 DISPATCH(); 2782 DISPATCH();
2667 #endif 2783 #endif
2668 } 2784 }
2669 2785
2670 TARGET(GET_ITER) { 2786 TARGET_NOARG(GET_ITER)
2787 {
2671 /* before: [obj]; after [getiter(obj)] */ 2788 /* before: [obj]; after [getiter(obj)] */
2672 PyObject *iterable = TOP(); 2789 v = TOP();
2673 PyObject *iter = PyObject_GetIter(iterable); 2790 x = PyObject_GetIter(v);
2674 Py_DECREF(iterable); 2791 Py_DECREF(v);
2675 SET_TOP(iter); 2792 if (x != NULL) {
2676 if (iter == NULL) 2793 SET_TOP(x);
2677 goto error; 2794 PREDICT(FOR_ITER);
2678 PREDICT(FOR_ITER); 2795 DISPATCH();
2679 DISPATCH(); 2796 }
2797 STACKADJ(-1);
2798 break;
2680 } 2799 }
2681 2800
2682 PREDICTED_WITH_ARG(FOR_ITER); 2801 PREDICTED_WITH_ARG(FOR_ITER);
2683 TARGET(FOR_ITER) { 2802 TARGET(FOR_ITER)
2803 {
2684 /* before: [iter]; after: [iter, iter()] *or* [] */ 2804 /* before: [iter]; after: [iter, iter()] *or* [] */
2685 PyObject *iter = TOP(); 2805 v = TOP();
2686 PyObject *next = (*iter->ob_type->tp_iternext)(iter); 2806 x = (*v->ob_type->tp_iternext)(v);
2687 if (next != NULL) { 2807 if (x != NULL) {
2688 PUSH(next); 2808 PUSH(x);
2689 PREDICT(STORE_FAST); 2809 PREDICT(STORE_FAST);
2690 PREDICT(UNPACK_SEQUENCE); 2810 PREDICT(UNPACK_SEQUENCE);
2691 DISPATCH(); 2811 DISPATCH();
2692 } 2812 }
2693 if (PyErr_Occurred()) { 2813 if (PyErr_Occurred()) {
2694 if (!PyErr_ExceptionMatches(PyExc_StopIteration)) 2814 if (!PyErr_ExceptionMatches(
2695 goto error; 2815 PyExc_StopIteration))
2696 else if (tstate->c_tracefunc != NULL) 2816 break;
2697 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tsta te, f);
2698 PyErr_Clear(); 2817 PyErr_Clear();
2699 } 2818 }
2700 /* iterator ended normally */ 2819 /* iterator ended normally */
2701 STACKADJ(-1); 2820 x = v = POP();
2702 Py_DECREF(iter); 2821 Py_DECREF(v);
2703 JUMPBY(oparg); 2822 JUMPBY(oparg);
2704 DISPATCH(); 2823 DISPATCH();
2705 } 2824 }
2706 2825
2707 TARGET(BREAK_LOOP) { 2826 TARGET_NOARG(BREAK_LOOP)
2827 {
2708 why = WHY_BREAK; 2828 why = WHY_BREAK;
2709 goto fast_block_end; 2829 goto fast_block_end;
2710 } 2830 }
2711 2831
2712 TARGET(CONTINUE_LOOP) { 2832 TARGET(CONTINUE_LOOP)
2713 retval = PyLong_FromLong(oparg); 2833 {
2714 if (retval == NULL) 2834 retval = PyInt_FromLong(oparg);
2715 goto error; 2835 if (!retval) {
2836 x = NULL;
2837 break;
2838 }
2716 why = WHY_CONTINUE; 2839 why = WHY_CONTINUE;
2717 goto fast_block_end; 2840 goto fast_block_end;
2718 } 2841 }
2719 2842
2720 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally) 2843 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2721 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally) 2844 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2722 TARGET(SETUP_FINALLY) 2845 TARGET(SETUP_FINALLY)
2723 _setup_finally: { 2846 _setup_finally:
2847 {
2724 /* NOTE: If you add any new block-setup opcodes that 2848 /* NOTE: If you add any new block-setup opcodes that
2725 are not try/except/finally handlers, you may need 2849 are not try/except/finally handlers, you may need
2726 to update the PyGen_NeedsFinalizing() function. 2850 to update the PyGen_NeedsFinalizing() function.
2727 */ 2851 */
2728 2852
2729 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, 2853 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2730 STACK_LEVEL()); 2854 STACK_LEVEL());
2731 DISPATCH(); 2855 DISPATCH();
2732 } 2856 }
2733 2857
2734 TARGET(SETUP_WITH) { 2858
2735 _Py_IDENTIFIER(__exit__); 2859
2736 _Py_IDENTIFIER(__enter__); 2860 TARGET(SETUP_WITH)
2737 PyObject *mgr = TOP(); 2861 {
2738 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter; 2862 {
2739 PyObject *res; 2863 static PyObject *exit, *enter;
2740 if (exit == NULL) 2864 w = TOP();
2741 goto error; 2865 x = special_lookup(w, "__exit__", &exit);
2742 SET_TOP(exit); 2866 if (!x)
2743 enter = special_lookup(mgr, &PyId___enter__); 2867 break;
2744 Py_DECREF(mgr); 2868 SET_TOP(x);
2745 if (enter == NULL) 2869 u = special_lookup(w, "__enter__", &enter);
2746 goto error; 2870 Py_DECREF(w);
2747 res = PyObject_CallFunctionObjArgs(enter, NULL); 2871 if (!u) {
2748 Py_DECREF(enter); 2872 x = NULL;
2749 if (res == NULL) 2873 break;
2750 goto error; 2874 }
2751 /* Setup the finally block before pushing the result 2875 x = PyObject_CallFunctionObjArgs(u, NULL);
2752 of __enter__ on the stack. */ 2876 Py_DECREF(u);
2753 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, 2877 if (!x)
2878 break;
2879 /* Setup a finally block (SETUP_WITH as a block is
2880 equivalent to SETUP_FINALLY except it normalizes
2881 the exception) before pushing the result of
2882 __enter__ on the stack. */
2883 PyFrame_BlockSetup(f, SETUP_WITH, INSTR_OFFSET() + oparg,
2754 STACK_LEVEL()); 2884 STACK_LEVEL());
2755 2885
2756 PUSH(res); 2886 PUSH(x);
2757 DISPATCH(); 2887 DISPATCH();
2758 } 2888 }
2759 2889 }
2760 TARGET(WITH_CLEANUP) { 2890
2761 /* At the top of the stack are 1-6 values indicating 2891 TARGET_NOARG(WITH_CLEANUP)
2892 {
2893 /* At the top of the stack are 1-3 values indicating
2762 how/why we entered the finally clause: 2894 how/why we entered the finally clause:
2763 - TOP = None 2895 - TOP = None
2764 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval 2896 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2765 - TOP = WHY_*; no retval below it 2897 - TOP = WHY_*; no retval below it
2766 - (TOP, SECOND, THIRD) = exc_info() 2898 - (TOP, SECOND, THIRD) = exc_info()
2767 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2768 Below them is EXIT, the context.__exit__ bound method. 2899 Below them is EXIT, the context.__exit__ bound method.
2769 In the last case, we must call 2900 In the last case, we must call
2770 EXIT(TOP, SECOND, THIRD) 2901 EXIT(TOP, SECOND, THIRD)
2771 otherwise we must call 2902 otherwise we must call
2772 EXIT(None, None, None) 2903 EXIT(None, None, None)
2773 2904
2774 In the first three cases, we remove EXIT from the 2905 In all cases, we remove EXIT from the stack, leaving
2775 stack, leaving the rest in the same order. In the 2906 the rest in the same order.
2776 fourth case, we shift the bottom 3 values of the
2777 stack down, and replace the empty spot with NULL.
2778 2907
2779 In addition, if the stack represents an exception, 2908 In addition, if the stack represents an exception,
2780 *and* the function call returns a 'true' value, we 2909 *and* the function call returns a 'true' value, we
2781 push WHY_SILENCED onto the stack. END_FINALLY will 2910 "zap" this information, to prevent END_FINALLY from
2782 then not re-raise the exception. (But non-local 2911 re-raising the exception. (But non-local gotos
2783 gotos should still be resumed.) 2912 should still be resumed.)
2784 */ 2913 */
2785 2914
2786 PyObject *exit_func; 2915 PyObject *exit_func;
2787 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res; 2916
2788 int err; 2917 u = POP();
2789 if (exc == Py_None) { 2918 if (u == Py_None) {
2790 (void)POP();
2791 exit_func = TOP(); 2919 exit_func = TOP();
2792 SET_TOP(exc); 2920 SET_TOP(u);
2793 } 2921 v = w = Py_None;
2794 else if (PyLong_Check(exc)) { 2922 }
2795 STACKADJ(-1); 2923 else if (PyInt_Check(u)) {
2796 switch (PyLong_AsLong(exc)) { 2924 switch(PyInt_AS_LONG(u)) {
2797 case WHY_RETURN: 2925 case WHY_RETURN:
2798 case WHY_CONTINUE: 2926 case WHY_CONTINUE:
2799 /* Retval in TOP. */ 2927 /* Retval in TOP. */
2800 exit_func = SECOND(); 2928 exit_func = SECOND();
2801 SET_SECOND(TOP()); 2929 SET_SECOND(TOP());
2802 SET_TOP(exc); 2930 SET_TOP(u);
2803 break; 2931 break;
2804 default: 2932 default:
2805 exit_func = TOP(); 2933 exit_func = TOP();
2806 SET_TOP(exc); 2934 SET_TOP(u);
2807 break; 2935 break;
2808 } 2936 }
2809 exc = Py_None; 2937 u = v = w = Py_None;
2810 } 2938 }
2811 else { 2939 else {
2812 PyObject *tp2, *exc2, *tb2; 2940 v = TOP();
2813 PyTryBlock *block; 2941 w = SECOND();
2814 val = SECOND(); 2942 exit_func = THIRD();
2815 tb = THIRD(); 2943 SET_TOP(u);
2816 tp2 = FOURTH(); 2944 SET_SECOND(v);
2817 exc2 = PEEK(5); 2945 SET_THIRD(w);
2818 tb2 = PEEK(6);
2819 exit_func = PEEK(7);
2820 SET_VALUE(7, tb2);
2821 SET_VALUE(6, exc2);
2822 SET_VALUE(5, tp2);
2823 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2824 SET_FOURTH(NULL);