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

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 8 years, 12 months ago
Right Patch Set: Created 4 years 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 suppport. 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 {
(...skipping 18 matching lines...) Expand all
58 #define READ_TIMESTAMP(val) \ 59 #define READ_TIMESTAMP(val) \
59 __asm__ __volatile__("rdtsc" : "=A" (val)) 60 __asm__ __volatile__("rdtsc" : "=A" (val))
60 61
61 #elif defined(__x86_64__) 62 #elif defined(__x86_64__)
62 63
63 /* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx; 64 /* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
64 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax 65 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
65 even in 64-bit mode, we need to use "a" and "d" for the lower and upper 66 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
66 32-bit pieces of the result. */ 67 32-bit pieces of the result. */
67 68
68 #define READ_TIMESTAMP(val) \ 69 #define READ_TIMESTAMP(val) do { \
69 __asm__ __volatile__("rdtsc" : \ 70 unsigned int h, l; \
70 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1])); 71 __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (h)); \
72 (val) = ((uint64)l) | (((uint64)h) << 32); \
73 } while(0)
71 74
72 75
73 #else 76 #else
74 77
75 #error "Don't know how to implement timestamp counter for this architecture" 78 #error "Don't know how to implement timestamp counter for this architecture"
76 79
77 #endif 80 #endif
78 81
79 void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1, 82 void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
80 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1) 83 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 static int prtrace(PyObject *, char *); 127 static int prtrace(PyObject *, char *);
125 #endif 128 #endif
126 static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *, 129 static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
127 int, PyObject *); 130 int, PyObject *);
128 static int call_trace_protected(Py_tracefunc, PyObject *, 131 static int call_trace_protected(Py_tracefunc, PyObject *,
129 PyFrameObject *, int, PyObject *); 132 PyFrameObject *, int, PyObject *);
130 static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *); 133 static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
131 static int maybe_call_line_trace(Py_tracefunc, PyObject *, 134 static int maybe_call_line_trace(Py_tracefunc, PyObject *,
132 PyFrameObject *, int *, int *, int *); 135 PyFrameObject *, int *, int *, int *);
133 136
137 static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
138 static int assign_slice(PyObject *, PyObject *,
139 PyObject *, PyObject *);
134 static PyObject * cmp_outcome(int, PyObject *, PyObject *); 140 static PyObject * cmp_outcome(int, PyObject *, PyObject *);
135 static PyObject * import_from(PyObject *, PyObject *); 141 static PyObject * import_from(PyObject *, PyObject *);
136 static int import_all_from(PyObject *, PyObject *); 142 static int import_all_from(PyObject *, PyObject *);
137 static void format_exc_check_arg(PyObject *, const char *, PyObject *); 143 static PyObject * build_class(PyObject *, PyObject *, PyObject *);
138 static void format_exc_unbound(PyCodeObject *co, int oparg); 144 static int exec_statement(PyFrameObject *,
139 static PyObject * unicode_concatenate(PyObject *, PyObject *, 145 PyObject *, PyObject *, PyObject *);
140 PyFrameObject *, unsigned char *); 146 static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
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 *);
141 static PyObject * special_lookup(PyObject *, char *, PyObject **); 152 static PyObject * special_lookup(PyObject *, char *, PyObject **);
142 153
143 #define NAME_ERROR_MSG \ 154 #define NAME_ERROR_MSG \
144 "name '%.200s' is not defined" 155 "name '%.200s' is not defined"
145 #define GLOBAL_NAME_ERROR_MSG \ 156 #define GLOBAL_NAME_ERROR_MSG \
146 "global name '%.200s' is not defined" 157 "global name '%.200s' is not defined"
147 #define UNBOUNDLOCAL_ERROR_MSG \ 158 #define UNBOUNDLOCAL_ERROR_MSG \
148 "local variable '%.200s' referenced before assignment" 159 "local variable '%.200s' referenced before assignment"
149 #define UNBOUNDFREE_ERROR_MSG \ 160 #define UNBOUNDFREE_ERROR_MSG \
150 "free variable '%.200s' referenced before assignment" \ 161 "free variable '%.200s' referenced before assignment" \
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 PyObject * 222 PyObject *
212 PyEval_GetCallStats(PyObject *self) 223 PyEval_GetCallStats(PyObject *self)
213 { 224 {
214 Py_INCREF(Py_None); 225 Py_INCREF(Py_None);
215 return Py_None; 226 return Py_None;
216 } 227 }
217 #endif 228 #endif
218 229
219 230
220 #ifdef WITH_THREAD 231 #ifdef WITH_THREAD
221 #define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
222 #else
223 #define GIL_REQUEST 0
224 #endif
225
226 /* This can set eval_breaker to 0 even though gil_drop_request became
227 1. We believe this is all right because the eval loop will release
228 the GIL eventually anyway. */
229 #define COMPUTE_EVAL_BREAKER() \
230 _Py_atomic_store_relaxed( \
231 &eval_breaker, \
232 GIL_REQUEST | \
233 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
234 pending_async_exc)
235
236 #ifdef WITH_THREAD
237
238 #define SET_GIL_DROP_REQUEST() \
239 do { \
240 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
241 _Py_atomic_store_relaxed(&eval_breaker, 1); \
242 } while (0)
243
244 #define RESET_GIL_DROP_REQUEST() \
245 do { \
246 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
247 COMPUTE_EVAL_BREAKER(); \
248 } while (0)
249
250 #endif
251
252 /* Pending calls are only modified under pending_lock */
253 #define SIGNAL_PENDING_CALLS() \
254 do { \
255 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
256 _Py_atomic_store_relaxed(&eval_breaker, 1); \
257 } while (0)
258
259 #define UNSIGNAL_PENDING_CALLS() \
260 do { \
261 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
262 COMPUTE_EVAL_BREAKER(); \
263 } while (0)
264
265 #define SIGNAL_ASYNC_EXC() \
266 do { \
267 pending_async_exc = 1; \
268 _Py_atomic_store_relaxed(&eval_breaker, 1); \
269 } while (0)
270
271 #define UNSIGNAL_ASYNC_EXC() \
272 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
273
274
275 #ifdef WITH_THREAD
276 232
277 #ifdef HAVE_ERRNO_H 233 #ifdef HAVE_ERRNO_H
278 #include <errno.h> 234 #include <errno.h>
279 #endif 235 #endif
280 #include "pythread.h" 236 #include "pythread.h"
281 237
238 static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
282 static PyThread_type_lock pending_lock = 0; /* for pending calls */ 239 static PyThread_type_lock pending_lock = 0; /* for pending calls */
283 static long main_thread = 0; 240 static long main_thread = 0;
284 /* This single variable consolidates all requests to break out of the fast path
285 in the eval loop. */
286 static _Py_atomic_int eval_breaker = {0};
287 /* Request for dropping the GIL */
288 static _Py_atomic_int gil_drop_request = {0};
289 /* Request for running pending calls. */
290 static _Py_atomic_int pendingcalls_to_do = {0};
291 /* Request for looking at the `async_exc` field of the current thread state.
292 Guarded by the GIL. */
293 static int pending_async_exc = 0;
294
295 #include "ceval_gil.h"
296 241
297 int 242 int
298 PyEval_ThreadsInitialized(void) 243 PyEval_ThreadsInitialized(void)
299 { 244 {
300 return gil_created(); 245 return interpreter_lock != 0;
301 } 246 }
302 247
303 void 248 void
304 PyEval_InitThreads(void) 249 PyEval_InitThreads(void)
305 { 250 {
306 if (gil_created()) 251 if (interpreter_lock)
307 return; 252 return;
308 create_gil(); 253 interpreter_lock = PyThread_allocate_lock();
309 take_gil(PyThreadState_GET()); 254 PyThread_acquire_lock(interpreter_lock, 1);
310 main_thread = PyThread_get_thread_ident(); 255 main_thread = PyThread_get_thread_ident();
311 if (!pending_lock)
312 pending_lock = PyThread_allocate_lock();
313 }
314
315 void
316 _PyEval_FiniThreads(void)
317 {
318 if (!gil_created())
319 return;
320 destroy_gil();
321 assert(!gil_created());
322 } 256 }
323 257
324 void 258 void
325 PyEval_AcquireLock(void) 259 PyEval_AcquireLock(void)
326 { 260 {
327 PyThreadState *tstate = PyThreadState_GET(); 261 PyThread_acquire_lock(interpreter_lock, 1);
328 if (tstate == NULL)
329 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
330 take_gil(tstate);
331 } 262 }
332 263
333 void 264 void
334 PyEval_ReleaseLock(void) 265 PyEval_ReleaseLock(void)
335 { 266 {
336 /* This function must succeed when the current thread state is NULL. 267 PyThread_release_lock(interpreter_lock);
337 We therefore avoid PyThreadState_GET() which dumps a fatal error
338 in debug mode.
339 */
340 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
341 &_PyThreadState_Current));
342 } 268 }
343 269
344 void 270 void
345 PyEval_AcquireThread(PyThreadState *tstate) 271 PyEval_AcquireThread(PyThreadState *tstate)
346 { 272 {
347 if (tstate == NULL) 273 if (tstate == NULL)
348 Py_FatalError("PyEval_AcquireThread: NULL new thread state"); 274 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
349 /* Check someone has called PyEval_InitThreads() to create the lock */ 275 /* Check someone has called PyEval_InitThreads() to create the lock */
350 assert(gil_created()); 276 assert(interpreter_lock);
351 take_gil(tstate); 277 PyThread_acquire_lock(interpreter_lock, 1);
352 if (PyThreadState_Swap(tstate) != NULL) 278 if (PyThreadState_Swap(tstate) != NULL)
353 Py_FatalError( 279 Py_FatalError(
354 "PyEval_AcquireThread: non-NULL old thread state"); 280 "PyEval_AcquireThread: non-NULL old thread state");
355 } 281 }
356 282
357 void 283 void
358 PyEval_ReleaseThread(PyThreadState *tstate) 284 PyEval_ReleaseThread(PyThreadState *tstate)
359 { 285 {
360 if (tstate == NULL) 286 if (tstate == NULL)
361 Py_FatalError("PyEval_ReleaseThread: NULL thread state"); 287 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
362 if (PyThreadState_Swap(NULL) != tstate) 288 if (PyThreadState_Swap(NULL) != tstate)
363 Py_FatalError("PyEval_ReleaseThread: wrong thread state"); 289 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
364 drop_gil(tstate); 290 PyThread_release_lock(interpreter_lock);
365 } 291 }
366 292
367 /* This function is called from PyOS_AfterFork to ensure that newly 293 /* This function is called from PyOS_AfterFork to ensure that newly
368 created child processes don't hold locks referring to threads which 294 created child processes don't hold locks referring to threads which
369 are not running in the child process. (This could also be done using 295 are not running in the child process. (This could also be done using
370 pthread_atfork mechanism, at least for the pthreads implementation.) */ 296 pthread_atfork mechanism, at least for the pthreads implementation.) */
371 297
372 void 298 void
373 PyEval_ReInitThreads(void) 299 PyEval_ReInitThreads(void)
374 { 300 {
375 PyObject *threading, *result; 301 PyObject *threading, *result;
376 PyThreadState *tstate = PyThreadState_GET(); 302 PyThreadState *tstate;
377 303
378 if (!gil_created()) 304 if (!interpreter_lock)
379 return; 305 return;
380 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();
381 pending_lock = PyThread_allocate_lock(); 311 pending_lock = PyThread_allocate_lock();
382 take_gil(tstate); 312 PyThread_acquire_lock(interpreter_lock, 1);
383 main_thread = PyThread_get_thread_ident(); 313 main_thread = PyThread_get_thread_ident();
384 314
385 /* Update the threading module with the new state. 315 /* Update the threading module with the new state.
386 */ 316 */
387 tstate = PyThreadState_GET(); 317 tstate = PyThreadState_GET();
388 threading = PyMapping_GetItemString(tstate->interp->modules, 318 threading = PyMapping_GetItemString(tstate->interp->modules,
389 "threading"); 319 "threading");
390 if (threading == NULL) { 320 if (threading == NULL) {
391 /* threading not imported */ 321 /* threading not imported */
392 PyErr_Clear(); 322 PyErr_Clear();
393 return; 323 return;
394 } 324 }
395 result = PyObject_CallMethod(threading, "_after_fork", NULL); 325 result = PyObject_CallMethod(threading, "_after_fork", NULL);
396 if (result == NULL) 326 if (result == NULL)
397 PyErr_WriteUnraisable(threading); 327 PyErr_WriteUnraisable(threading);
398 else 328 else
399 Py_DECREF(result); 329 Py_DECREF(result);
400 Py_DECREF(threading); 330 Py_DECREF(threading);
401 } 331 }
402 332 #endif
403 #else
404 static _Py_atomic_int eval_breaker = {0};
405 static int pending_async_exc = 0;
406 #endif /* WITH_THREAD */
407
408 /* This function is used to signal that async exceptions are waiting to be
409 raised, therefore it is also useful in non-threaded builds. */
410
411 void
412 _PyEval_SignalAsyncExc(void)
413 {
414 SIGNAL_ASYNC_EXC();
415 }
416 333
417 /* Functions save_thread and restore_thread are always defined so 334 /* Functions save_thread and restore_thread are always defined so
418 dynamically loaded modules needn't be compiled separately for use 335 dynamically loaded modules needn't be compiled separately for use
419 with and without threads: */ 336 with and without threads: */
420 337
421 PyThreadState * 338 PyThreadState *
422 PyEval_SaveThread(void) 339 PyEval_SaveThread(void)
423 { 340 {
424 PyThreadState *tstate = PyThreadState_Swap(NULL); 341 PyThreadState *tstate = PyThreadState_Swap(NULL);
425 if (tstate == NULL) 342 if (tstate == NULL)
426 Py_FatalError("PyEval_SaveThread: NULL tstate"); 343 Py_FatalError("PyEval_SaveThread: NULL tstate");
427 #ifdef WITH_THREAD 344 #ifdef WITH_THREAD
428 if (gil_created()) 345 if (interpreter_lock)
429 drop_gil(tstate); 346 PyThread_release_lock(interpreter_lock);
430 #endif 347 #endif
431 return tstate; 348 return tstate;
432 } 349 }
433 350
434 void 351 void
435 PyEval_RestoreThread(PyThreadState *tstate) 352 PyEval_RestoreThread(PyThreadState *tstate)
436 { 353 {
437 if (tstate == NULL) 354 if (tstate == NULL)
438 Py_FatalError("PyEval_RestoreThread: NULL tstate"); 355 Py_FatalError("PyEval_RestoreThread: NULL tstate");
439 #ifdef WITH_THREAD 356 #ifdef WITH_THREAD
440 if (gil_created()) { 357 if (interpreter_lock) {
441 int err = errno; 358 int err = errno;
442 take_gil(tstate); 359 PyThread_acquire_lock(interpreter_lock, 1);
443 errno = err; 360 errno = err;
444 } 361 }
445 #endif 362 #endif
446 PyThreadState_Swap(tstate); 363 PyThreadState_Swap(tstate);
447 } 364 }
448 365
449 366
450 /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX 367 /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
451 signal handlers or Mac I/O completion routines) can schedule calls 368 signal handlers or Mac I/O completion routines) can schedule calls
452 to a function to be called synchronously. 369 to a function to be called synchronously.
(...skipping 25 matching lines...) Expand all
478 callback. 395 callback.
479 */ 396 */
480 397
481 #define NPENDINGCALLS 32 398 #define NPENDINGCALLS 32
482 static struct { 399 static struct {
483 int (*func)(void *); 400 int (*func)(void *);
484 void *arg; 401 void *arg;
485 } pendingcalls[NPENDINGCALLS]; 402 } pendingcalls[NPENDINGCALLS];
486 static int pendingfirst = 0; 403 static int pendingfirst = 0;
487 static int pendinglast = 0; 404 static int pendinglast = 0;
405 static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
488 static char pendingbusy = 0; 406 static char pendingbusy = 0;
489 407
490 int 408 int
491 Py_AddPendingCall(int (*func)(void *), void *arg) 409 Py_AddPendingCall(int (*func)(void *), void *arg)
492 { 410 {
493 int i, j, result=0; 411 int i, j, result=0;
494 PyThread_type_lock lock = pending_lock; 412 PyThread_type_lock lock = pending_lock;
495 413
496 /* 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
497 * for signal handling (on the main thread), there is a (slim) 415 * for signal handling (on the main thread), there is a (slim)
(...skipping 18 matching lines...) Expand all
516 i = pendinglast; 434 i = pendinglast;
517 j = (i + 1) % NPENDINGCALLS; 435 j = (i + 1) % NPENDINGCALLS;
518 if (j == pendingfirst) { 436 if (j == pendingfirst) {
519 result = -1; /* Queue full */ 437 result = -1; /* Queue full */
520 } else { 438 } else {
521 pendingcalls[i].func = func; 439 pendingcalls[i].func = func;
522 pendingcalls[i].arg = arg; 440 pendingcalls[i].arg = arg;
523 pendinglast = j; 441 pendinglast = j;
524 } 442 }
525 /* signal main loop */ 443 /* signal main loop */
526 SIGNAL_PENDING_CALLS(); 444 _Py_Ticker = 0;
445 pendingcalls_to_do = 1;
527 if (lock != NULL) 446 if (lock != NULL)
528 PyThread_release_lock(lock); 447 PyThread_release_lock(lock);
529 return result; 448 return result;
530 } 449 }
531 450
532 int 451 int
533 Py_MakePendingCalls(void) 452 Py_MakePendingCalls(void)
534 { 453 {
535 int i; 454 int i;
536 int r = 0; 455 int r = 0;
(...skipping 21 matching lines...) Expand all
558 /* pop one item off the queue while holding the lock */ 477 /* pop one item off the queue while holding the lock */
559 PyThread_acquire_lock(pending_lock, WAIT_LOCK); 478 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
560 j = pendingfirst; 479 j = pendingfirst;
561 if (j == pendinglast) { 480 if (j == pendinglast) {
562 func = NULL; /* Queue empty */ 481 func = NULL; /* Queue empty */
563 } else { 482 } else {
564 func = pendingcalls[j].func; 483 func = pendingcalls[j].func;
565 arg = pendingcalls[j].arg; 484 arg = pendingcalls[j].arg;
566 pendingfirst = (j + 1) % NPENDINGCALLS; 485 pendingfirst = (j + 1) % NPENDINGCALLS;
567 } 486 }
568 if (pendingfirst != pendinglast) 487 pendingcalls_to_do = pendingfirst != pendinglast;
569 SIGNAL_PENDING_CALLS();
570 else
571 UNSIGNAL_PENDING_CALLS();
572 PyThread_release_lock(pending_lock); 488 PyThread_release_lock(pending_lock);
573 /* having released the lock, perform the callback */ 489 /* having released the lock, perform the callback */
574 if (func == NULL) 490 if (func == NULL)
575 break; 491 break;
576 r = func(arg); 492 r = func(arg);
577 if (r) 493 if (r)
578 break; 494 break;
579 } 495 }
580 pendingbusy = 0; 496 pendingbusy = 0;
581 return r; 497 return r;
(...skipping 24 matching lines...) Expand all
606 The two threads could theoretically wiggle around the "busy" variable. 522 The two threads could theoretically wiggle around the "busy" variable.
607 */ 523 */
608 524
609 #define NPENDINGCALLS 32 525 #define NPENDINGCALLS 32
610 static struct { 526 static struct {
611 int (*func)(void *); 527 int (*func)(void *);
612 void *arg; 528 void *arg;
613 } pendingcalls[NPENDINGCALLS]; 529 } pendingcalls[NPENDINGCALLS];
614 static volatile int pendingfirst = 0; 530 static volatile int pendingfirst = 0;
615 static volatile int pendinglast = 0; 531 static volatile int pendinglast = 0;
616 static _Py_atomic_int pendingcalls_to_do = {0}; 532 static volatile int pendingcalls_to_do = 0;
617 533
618 int 534 int
619 Py_AddPendingCall(int (*func)(void *), void *arg) 535 Py_AddPendingCall(int (*func)(void *), void *arg)
620 { 536 {
621 static volatile int busy = 0; 537 static volatile int busy = 0;
622 int i, j; 538 int i, j;
623 /* XXX Begin critical section */ 539 /* XXX Begin critical section */
624 if (busy) 540 if (busy)
625 return -1; 541 return -1;
626 busy = 1; 542 busy = 1;
627 i = pendinglast; 543 i = pendinglast;
628 j = (i + 1) % NPENDINGCALLS; 544 j = (i + 1) % NPENDINGCALLS;
629 if (j == pendingfirst) { 545 if (j == pendingfirst) {
630 busy = 0; 546 busy = 0;
631 return -1; /* Queue full */ 547 return -1; /* Queue full */
632 } 548 }
633 pendingcalls[i].func = func; 549 pendingcalls[i].func = func;
634 pendingcalls[i].arg = arg; 550 pendingcalls[i].arg = arg;
635 pendinglast = j; 551 pendinglast = j;
636 552
637 SIGNAL_PENDING_CALLS(); 553 _Py_Ticker = 0;
554 pendingcalls_to_do = 1; /* Signal main loop */
638 busy = 0; 555 busy = 0;
639 /* XXX End critical section */ 556 /* XXX End critical section */
640 return 0; 557 return 0;
641 } 558 }
642 559
643 int 560 int
644 Py_MakePendingCalls(void) 561 Py_MakePendingCalls(void)
645 { 562 {
646 static int busy = 0; 563 static int busy = 0;
647 if (busy) 564 if (busy)
648 return 0; 565 return 0;
649 busy = 1; 566 busy = 1;
650 UNSIGNAL_PENDING_CALLS(); 567 pendingcalls_to_do = 0;
651 for (;;) { 568 for (;;) {
652 int i; 569 int i;
653 int (*func)(void *); 570 int (*func)(void *);
654 void *arg; 571 void *arg;
655 i = pendingfirst; 572 i = pendingfirst;
656 if (i == pendinglast) 573 if (i == pendinglast)
657 break; /* Queue empty */ 574 break; /* Queue empty */
658 func = pendingcalls[i].func; 575 func = pendingcalls[i].func;
659 arg = pendingcalls[i].arg; 576 arg = pendingcalls[i].arg;
660 pendingfirst = (i + 1) % NPENDINGCALLS; 577 pendingfirst = (i + 1) % NPENDINGCALLS;
661 if (func(arg) < 0) { 578 if (func(arg) < 0) {
662 busy = 0; 579 busy = 0;
663 SIGNAL_PENDING_CALLS(); /* We're not done yet */ 580 pendingcalls_to_do = 1; /* We're not done yet */
664 return -1; 581 return -1;
665 } 582 }
666 } 583 }
667 busy = 0; 584 busy = 0;
668 return 0; 585 return 0;
669 } 586 }
670 587
671 #endif /* WITH_THREAD */ 588 #endif /* WITH_THREAD */
672 589
673 590
(...skipping 17 matching lines...) Expand all
691 recursion_limit = new_limit; 608 recursion_limit = new_limit;
692 _Py_CheckRecursionLimit = recursion_limit; 609 _Py_CheckRecursionLimit = recursion_limit;
693 } 610 }
694 611
695 /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() 612 /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
696 if the recursion_depth reaches _Py_CheckRecursionLimit. 613 if the recursion_depth reaches _Py_CheckRecursionLimit.
697 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit 614 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
698 to guarantee that _Py_CheckRecursiveCall() is regularly called. 615 to guarantee that _Py_CheckRecursiveCall() is regularly called.
699 Without USE_STACKCHECK, there is no need for this. */ 616 Without USE_STACKCHECK, there is no need for this. */
700 int 617 int
701 _Py_CheckRecursiveCall(char *where) 618 _Py_CheckRecursiveCall(const char *where)
702 { 619 {
703 PyThreadState *tstate = PyThreadState_GET(); 620 PyThreadState *tstate = PyThreadState_GET();
704 621
705 #ifdef USE_STACKCHECK 622 #ifdef USE_STACKCHECK
706 if (PyOS_CheckStack()) { 623 if (PyOS_CheckStack()) {
707 --tstate->recursion_depth; 624 --tstate->recursion_depth;
708 PyErr_SetString(PyExc_MemoryError, "Stack overflow"); 625 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
709 return -1; 626 return -1;
710 } 627 }
711 #endif 628 #endif
712 _Py_CheckRecursionLimit = recursion_limit;
713 if (tstate->recursion_critical)
714 /* Somebody asked that we don't check for recursion. */
715 return 0;
716 if (tstate->overflowed) {
717 if (tstate->recursion_depth > recursion_limit + 50) {
718 /* Overflowing while handling an overflow. Give up. */
719 Py_FatalError("Cannot recover from stack overflow.");
720 }
721 return 0;
722 }
723 if (tstate->recursion_depth > recursion_limit) { 629 if (tstate->recursion_depth > recursion_limit) {
724 --tstate->recursion_depth; 630 --tstate->recursion_depth;
725 tstate->overflowed = 1;
726 PyErr_Format(PyExc_RuntimeError, 631 PyErr_Format(PyExc_RuntimeError,
727 "maximum recursion depth exceeded%s", 632 "maximum recursion depth exceeded%s",
728 where); 633 where);
729 return -1; 634 return -1;
730 } 635 }
636 _Py_CheckRecursionLimit = recursion_limit;
731 return 0; 637 return 0;
732 } 638 }
733 639
734 /* Status code for main loop (reason for stack unwind) */ 640 /* Status code for main loop (reason for stack unwind) */
735 enum why_code { 641 enum why_code {
736 WHY_NOT = 0x0001, /* No error */ 642 WHY_NOT = 0x0001, /* No error */
737 WHY_EXCEPTION = 0x0002, /* Exception occurred */ 643 WHY_EXCEPTION = 0x0002, /* Exception occurred */
738 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */ 644 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
739 WHY_RETURN = 0x0008, /* 'return' statement */ 645 WHY_RETURN = 0x0008, /* 'return' statement */
740 WHY_BREAK = 0x0010, /* 'break' statement */ 646 WHY_BREAK = 0x0010, /* 'break' statement */
741 WHY_CONTINUE = 0x0020, /* 'continue' statement */ 647 WHY_CONTINUE = 0x0020, /* 'continue' statement */
742 WHY_YIELD = 0x0040, /* 'yield' operator */ 648 WHY_YIELD = 0x0040 /* 'yield' operator */
743 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
744 }; 649 };
745 650
746 static enum why_code do_raise(PyObject *, PyObject *); 651 static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
747 static int unpack_iterable(PyObject *, int, int, PyObject **); 652 static int unpack_iterable(PyObject *, int, PyObject **);
748 653
749 /* 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
750 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
751 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.
752 This speeds up the if statement in PyEval_EvalFrameEx() after 657 This speeds up the if statement in PyEval_EvalFrameEx() after
753 fast_next_opcode*/ 658 fast_next_opcode*/
754 static int _Py_TracingPossible = 0; 659 static int _Py_TracingPossible = 0;
755 660
756 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 */
757 665
758 PyObject * 666 PyObject *
759 PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) 667 PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
760 { 668 {
761 return PyEval_EvalCodeEx(co, 669 return PyEval_EvalCodeEx(co,
762 globals, locals, 670 globals, locals,
763 (PyObject **)NULL, 0, 671 (PyObject **)NULL, 0,
764 (PyObject **)NULL, 0, 672 (PyObject **)NULL, 0,
765 (PyObject **)NULL, 0, 673 (PyObject **)NULL, 0,
766 NULL, NULL); 674 NULL);
767 } 675 }
768 676
769 677
770 /* Interpreter main loop */ 678 /* Interpreter main loop */
771 679
772 PyObject * 680 PyObject *
773 PyEval_EvalFrame(PyFrameObject *f) { 681 PyEval_EvalFrame(PyFrameObject *f) {
774 /* This is for backward compatibility with extension modules that 682 /* This is for backward compatibility with extension modules that
775 used this API; core interpreter code should call 683 used this API; core interpreter code should call
776 PyEval_EvalFrameEx() */ 684 PyEval_EvalFrameEx() */
777 return PyEval_EvalFrameEx(f, 0); 685 return PyEval_EvalFrameEx(f, 0);
778 } 686 }
779 687
780 PyObject * 688 PyObject *
781 PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) 689 PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
782 { 690 {
691 #ifdef DYNAMIC_EXECUTION_PROFILE
692 #undef USE_COMPUTED_GOTOS
693 #endif
694 #ifdef HAVE_COMPUTED_GOTOS
695 #ifndef USE_COMPUTED_GOTOS
696 #define USE_COMPUTED_GOTOS 1
697 #endif
698 #else
699 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
700 #error "Computed gotos are not supported on this compiler."
701 #endif
702 #undef USE_COMPUTED_GOTOS
703 #define USE_COMPUTED_GOTOS 0
704 #endif
705 #if USE_COMPUTED_GOTOS
706 /* Import the static jump table */
707 #include "opcode_targets.h"
708
709 /* This macro is used when several opcodes defer to the same implementation
710 (e.g. SETUP_LOOP, SETUP_FINALLY) */
711 #define TARGET_WITH_IMPL(op, impl) \
712 TARGET_##op: \
713 opcode = op; \
714 oparg = NEXTARG(); \
715 case op: \
716 goto impl; \
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
729 #define TARGET(op) \
730 TARGET_##op: \
731 opcode = op; \
732 oparg = NEXTARG(); \
733 case op:\
734
735
736 #define DISPATCH() \
737 { \
738 int _tick = _Py_Ticker - 1; \
739 _Py_Ticker = _tick; \
740 if (_tick >= 0) { \
741 FAST_DISPATCH(); \
742 } \
743 continue; \
744 }
745
746 #ifdef LLTRACE
747 #define FAST_DISPATCH() \
748 { \
749 if (!lltrace && !_Py_TracingPossible) { \
750 f->f_lasti = INSTR_OFFSET(); \
751 goto *opcode_targets[*next_instr++]; \
752 } \
753 goto fast_next_opcode; \
754 }
755 #else
756 #define FAST_DISPATCH() { \
757 if (!_Py_TracingPossible) { \
758 f->f_lasti = INSTR_OFFSET(); \
759 goto *opcode_targets[*next_instr++]; \
760 } \
761 goto fast_next_opcode;\
762 }
763 #endif
764
765 #else
766 #define TARGET(op) \
767 case op:
768 #define TARGET_WITH_IMPL(op, impl) \
769 /* silence compiler warnings about `impl` unused */ \
770 if (0) goto impl; \
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
780 #define DISPATCH() continue
781 #define FAST_DISPATCH() goto fast_next_opcode
782 #endif
783
784
783 #ifdef DXPAIRS 785 #ifdef DXPAIRS
784 int lastopcode = 0; 786 int lastopcode = 0;
785 #endif 787 #endif
786 register PyObject **stack_pointer; /* Next free slot in value stack */ 788 register PyObject **stack_pointer; /* Next free slot in value stack */
787 register unsigned char *next_instr; 789 register unsigned char *next_instr;
788 register int opcode; /* Current opcode */ 790 register int opcode; /* Current opcode */
789 register int oparg; /* Current opcode argument, if any */ 791 register int oparg; /* Current opcode argument, if any */
790 register enum why_code why; /* Reason for block stack unwind */ 792 register enum why_code why; /* Reason for block stack unwind */
791 register int err; /* Error status -- nonzero if error */ 793 register int err; /* Error status -- nonzero if error */
792 register PyObject *x; /* Result object -- NULL if error */ 794 register PyObject *x; /* Result object -- NULL if error */
793 register PyObject *v; /* Temporary objects popped off stack */ 795 register PyObject *v; /* Temporary objects popped off stack */
794 register PyObject *w; 796 register PyObject *w;
795 register PyObject *u; 797 register PyObject *u;
796 register PyObject *t; 798 register PyObject *t;
799 register PyObject *stream = NULL; /* for PRINT opcodes */
797 register PyObject **fastlocals, **freevars; 800 register PyObject **fastlocals, **freevars;
798 PyObject *retval = NULL; /* Return value */ 801 PyObject *retval = NULL; /* Return value */
799 PyThreadState *tstate = PyThreadState_GET(); 802 PyThreadState *tstate = PyThreadState_GET();
800 PyCodeObject *co; 803 PyCodeObject *co;
801 804
802 /* when tracing we set things up so that 805 /* when tracing we set things up so that
803 806
804 not (instr_lb <= current_bytecode_offset < instr_ub) 807 not (instr_lb <= current_bytecode_offset < instr_ub)
805 808
806 is true when the line being executed has changed. The 809 is true when the line being executed has changed. The
807 initial values are such as to make this false the first 810 initial values are such as to make this false the first
808 time it is tested. */ 811 time it is tested. */
809 int instr_ub = -1, instr_lb = 0, instr_prev = -1; 812 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
810 813
811 unsigned char *first_instr; 814 unsigned char *first_instr;
812 PyObject *names; 815 PyObject *names;
813 PyObject *consts; 816 PyObject *consts;
814 817 #if defined(Py_DEBUG) || defined(LLTRACE)
815 /* Computed GOTOs, or 818 /* Make it easier to find out where we are with a debugger */
816 the-optimization-commonly-but-improperly-known-as-"threaded code" 819 char *filename;
817 using gcc's labels-as-values extension 820 #endif
818 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
819
820 The traditional bytecode evaluation loop uses a "switch" statement, which
821 decent compilers will optimize as a single indirect branch instruction
822 combined with a lookup table of jump addresses. However, since the
823 indirect jump instruction is shared by all opcodes, the CPU will have a
824 hard time making the right prediction for where to jump next (actually,
825 it will be always wrong except in the uncommon case of a sequence of
826 several identical opcodes).
827
828 "Threaded code" in contrast, uses an explicit jump table and an explicit
829 indirect jump instruction at the end of each opcode. Since the jump
830 instruction is at a different address for each opcode, the CPU will make a
831 separate prediction for each of these instructions, which is equivalent to
832 predicting the second opcode of each opcode pair. These predictions have
833 a much better chance to turn out valid, especially in small bytecode loops.
834
835 A mispredicted branch on a modern CPU flushes the whole pipeline and
836 can cost several CPU cycles (depending on the pipeline depth),
837 and potentially many more instructions (depending on the pipeline width).
838 A correctly predicted branch, however, is nearly free.
839
840 At the time of this writing, the "threaded code" version is up to 15-20%
841 faster than the normal "switch" version, depending on the compiler and the
842 CPU architecture.
843
844 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
845 because it would render the measurements invalid.
846
847
848 NOTE: care must be taken that the compiler doesn't try to "optimize" the
849 indirect jumps by sharing them between all opcodes. Such optimizations
850 can be disabled on gcc by using the -fno-gcse flag (or possibly
851 -fno-crossjumping).
852 */
853
854 #ifdef DYNAMIC_EXECUTION_PROFILE
855 #undef USE_COMPUTED_GOTOS
856 #define USE_COMPUTED_GOTOS 0
857 #endif
858
859 #ifdef HAVE_COMPUTED_GOTOS
860 #ifndef USE_COMPUTED_GOTOS
861 #define USE_COMPUTED_GOTOS 1
862 #endif
863 #else
864 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
865 #error "Computed gotos are not supported on this compiler."
866 #endif
867 #undef USE_COMPUTED_GOTOS
868 #define USE_COMPUTED_GOTOS 0
869 #endif
870
871 #if USE_COMPUTED_GOTOS
872 /* Import the static jump table */
873 #include "opcode_targets.h"
874
875 /* This macro is used when several opcodes defer to the same implementation
876 (e.g. SETUP_LOOP, SETUP_FINALLY) */
877 #define TARGET_WITH_IMPL(op, impl) \
878 TARGET_##op: \
879 opcode = op; \
880 if (HAS_ARG(op)) \
881 oparg = NEXTARG(); \
882 case op: \
883 goto impl; \
884
885 #define TARGET(op) \
886 TARGET_##op: \
887 opcode = op; \
888 if (HAS_ARG(op)) \
889 oparg = NEXTARG(); \
890 case op:
891
892
893 #define DISPATCH() \
894 { \
895 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
896 FAST_DISPATCH(); \
897 } \
898 continue; \
899 }
900
901 #ifdef LLTRACE
902 #define FAST_DISPATCH() \
903 { \
904 if (!lltrace && !_Py_TracingPossible) { \
905 f->f_lasti = INSTR_OFFSET(); \
906 goto *opcode_targets[*next_instr++]; \
907 } \
908 goto fast_next_opcode; \
909 }
910 #else
911 #define FAST_DISPATCH() \
912 { \
913 if (!_Py_TracingPossible) { \
914 f->f_lasti = INSTR_OFFSET(); \
915 goto *opcode_targets[*next_instr++]; \
916 } \
917 goto fast_next_opcode; \
918 }
919 #endif
920
921 #else
922 #define TARGET(op) \
923 case op:
924 #define TARGET_WITH_IMPL(op, impl) \
925 /* silence compiler warnings about `impl` unused */ \
926 if (0) goto impl; \
927 case op:
928 #define DISPATCH() continue
929 #define FAST_DISPATCH() goto fast_next_opcode
930 #endif
931
932 821
933 /* Tuple access macros */ 822 /* Tuple access macros */
934 823
935 #ifndef Py_DEBUG 824 #ifndef Py_DEBUG
936 #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) 825 #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
937 #else 826 #else
938 #define GETITEM(v, i) PyTuple_GetItem((v), (i)) 827 #define GETITEM(v, i) PyTuple_GetItem((v), (i))
939 #endif 828 #endif
940 829
941 #ifdef WITH_TSC 830 #ifdef WITH_TSC
(...skipping 12 matching lines...) Expand all
954 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
955 bytecode executed by the called function to the caller. 844 bytecode executed by the called function to the caller.
956 845
957 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
958 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
959 execute new Python code or not. 848 execute new Python code or not.
960 849
961 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
962 cases: 851 cases:
963 852
853 EXEC_STMT
964 IMPORT_STAR 854 IMPORT_STAR
965 IMPORT_FROM 855 IMPORT_FROM
966 CALL_FUNCTION (and friends) 856 CALL_FUNCTION (and friends)
967 857
968 */ 858 */
969 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0; 859 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
970 int ticked = 0; 860 int ticked = 0;
971 861
972 READ_TIMESTAMP(inst0); 862 READ_TIMESTAMP(inst0);
973 READ_TIMESTAMP(inst1); 863 READ_TIMESTAMP(inst1);
974 READ_TIMESTAMP(loop0); 864 READ_TIMESTAMP(loop0);
975 READ_TIMESTAMP(loop1); 865 READ_TIMESTAMP(loop1);
976 866
977 /* shut up the compiler */ 867 /* shut up the compiler */
978 opcode = 0; 868 opcode = 0;
979 #endif 869 #endif
980 870
981 /* Code access macros */ 871 /* Code access macros */
982 872
983 #define INSTR_OFFSET() ((int)(next_instr - first_instr)) 873 #define INSTR_OFFSET() ((int)(next_instr - first_instr))
984 #define NEXTOP() (*next_instr++) 874 #define NEXTOP() (*next_instr++)
985 #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])
986 #define PEEKARG() ((next_instr[2]<<8) + next_instr[1]) 876 #define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
987 #define JUMPTO(x) (next_instr = first_instr + (x)) 877 #define JUMPTO(x) (next_instr = first_instr + (x))
988 #define JUMPBY(x) (next_instr += (x)) 878 #define JUMPBY(x) (next_instr += (x))
989 879
990 /* OpCode prediction macros 880 /* OpCode prediction macros
991 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
992 predict the second code when the first is run. For example, 882 predict the second code when the first is run. For example,
993 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
994 those opcodes are often followed by a POP_TOP. 884 followed by STORE_FAST or UNPACK_SEQUENCE.
995 885
996 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
997 variable against a constant. If the pairing was good, then the 887 variable against a constant. If the pairing was good, then the
998 processor's own internal branch predication has a high likelihood of 888 processor's own internal branch predication has a high likelihood of
999 success, resulting in a nearly zero-overhead transition to the 889 success, resulting in a nearly zero-overhead transition to the
1000 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
1001 including its two unpredictable branches, the HAS_ARG test and the 891 including its two unpredictable branches, the HAS_ARG test and the
1002 switch-case. Combined with the processor's internal branch prediction, 892 switch-case. Combined with the processor's internal branch prediction,
1003 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
1004 they were a single new opcode with the bodies combined. 894 they were a single new opcode with the bodies combined.
1005 895
1006 If collecting opcode statistics, your choices are to either keep the 896 If collecting opcode statistics, your choices are to either keep the
1007 predictions turned-on and interpret the results as if some opcodes 897 predictions turned-on and interpret the results as if some opcodes
1008 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
1009 counter updates for both opcodes. 899 counter updates for both opcodes.
1010
1011 Opcode prediction is disabled with threaded code, since the latter allows
1012 the CPU to record separate branch prediction information for each
1013 opcode.
1014
1015 */ 900 */
901
1016 902
1017 #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS 903 #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
1018 #define PREDICT(op) if (0) goto PRED_##op 904 #define PREDICT(op) if (0) goto PRED_##op
1019 #define PREDICTED(op) PRED_##op: 905 #define PREDICTED(op) PRED_##op:
1020 #define PREDICTED_WITH_ARG(op) PRED_##op: 906 #define PREDICTED_WITH_ARG(op) PRED_##op:
1021 #else 907 #else
1022 #define PREDICT(op) if (*next_instr == op) goto PRED_##op 908 #define PREDICT(op) if (*next_instr == op) goto PRED_##op
1023 #define PREDICTED(op) PRED_##op: next_instr++ 909 #define PREDICTED(op) PRED_##op: next_instr++
1024 #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
1025 #endif 911 #endif
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1070 956
1071 /* 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
1072 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
1073 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.
1074 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
1075 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
1076 variable would be pointing to already-freed memory. */ 962 variable would be pointing to already-freed memory. */
1077 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ 963 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
1078 GETLOCAL(i) = value; \ 964 GETLOCAL(i) = value; \
1079 Py_XDECREF(tmp); } while (0) 965 Py_XDECREF(tmp); } while (0)
1080
1081
1082 #define UNWIND_BLOCK(b) \
1083 while (STACK_LEVEL() > (b)->b_level) { \
1084 PyObject *v = POP(); \
1085 Py_XDECREF(v); \
1086 }
1087
1088 #define UNWIND_EXCEPT_HANDLER(b) \
1089 { \
1090 PyObject *type, *value, *traceback; \
1091 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1092 while (STACK_LEVEL() > (b)->b_level + 3) { \
1093 value = POP(); \
1094 Py_XDECREF(value); \
1095 } \
1096 type = tstate->exc_type; \
1097 value = tstate->exc_value; \
1098 traceback = tstate->exc_traceback; \
1099 tstate->exc_type = POP(); \
1100 tstate->exc_value = POP(); \
1101 tstate->exc_traceback = POP(); \
1102 Py_XDECREF(type); \
1103 Py_XDECREF(value); \
1104 Py_XDECREF(traceback); \
1105 }
1106
1107 #define SAVE_EXC_STATE() \
1108 { \
1109 PyObject *type, *value, *traceback; \
1110 Py_XINCREF(tstate->exc_type); \
1111 Py_XINCREF(tstate->exc_value); \
1112 Py_XINCREF(tstate->exc_traceback); \
1113 type = f->f_exc_type; \
1114 value = f->f_exc_value; \
1115 traceback = f->f_exc_traceback; \
1116 f->f_exc_type = tstate->exc_type; \
1117 f->f_exc_value = tstate->exc_value; \
1118 f->f_exc_traceback = tstate->exc_traceback; \
1119 Py_XDECREF(type); \
1120 Py_XDECREF(value); \
1121 Py_XDECREF(traceback); \
1122 }
1123
1124 #define SWAP_EXC_STATE() \
1125 { \
1126 PyObject *tmp; \
1127 tmp = tstate->exc_type; \
1128 tstate->exc_type = f->f_exc_type; \
1129 f->f_exc_type = tmp; \
1130 tmp = tstate->exc_value; \
1131 tstate->exc_value = f->f_exc_value; \
1132 f->f_exc_value = tmp; \
1133 tmp = tstate->exc_traceback; \
1134 tstate->exc_traceback = f->f_exc_traceback; \
1135 f->f_exc_traceback = tmp; \
1136 }
1137 966
1138 /* Start of code */ 967 /* Start of code */
1139 968
1140 if (f == NULL) 969 if (f == NULL)
1141 return NULL; 970 return NULL;
1142 971
1143 /* push frame */ 972 /* push frame */
1144 if (Py_EnterRecursiveCall("")) 973 if (Py_EnterRecursiveCall(""))
1145 return NULL; 974 return NULL;
1146 975
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1178 goto exit_eval_frame; 1007 goto exit_eval_frame;
1179 } 1008 }
1180 } 1009 }
1181 } 1010 }
1182 1011
1183 co = f->f_code; 1012 co = f->f_code;
1184 names = co->co_names; 1013 names = co->co_names;
1185 consts = co->co_consts; 1014 consts = co->co_consts;
1186 fastlocals = f->f_localsplus; 1015 fastlocals = f->f_localsplus;
1187 freevars = f->f_localsplus + co->co_nlocals; 1016 freevars = f->f_localsplus + co->co_nlocals;
1188 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code); 1017 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
1189 /* An explanation is in order for the next line. 1018 /* An explanation is in order for the next line.
1190 1019
1191 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
1192 executed. You might think this was obvious from the name, but 1021 executed. You might think this was obvious from the name, but
1193 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
1194 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)
1195 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
1196 does work. Promise. 1025 does work. Promise.
1197 1026
1198 When the PREDICT() macros are enabled, some opcode pairs follow in 1027 When the PREDICT() macros are enabled, some opcode pairs follow in
1199 direct succession without updating f->f_lasti. A successful 1028 direct succession without updating f->f_lasti. A successful
1200 prediction effectively links the two codes together as if they 1029 prediction effectively links the two codes together as if they
1201 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
1202 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
1203 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
1204 at to the beginning of the combined pair.) 1033 at to the beginning of the combined pair.)
1205 */ 1034 */
1206 next_instr = first_instr + f->f_lasti + 1; 1035 next_instr = first_instr + f->f_lasti + 1;
1207 stack_pointer = f->f_stacktop; 1036 stack_pointer = f->f_stacktop;
1208 assert(stack_pointer != NULL); 1037 assert(stack_pointer != NULL);
1209 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ 1038 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
1210 1039
1211 if (co->co_flags & CO_GENERATOR && !throwflag) {
1212 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1213 /* We were in an except handler when we left,
1214 restore the exception state which was put aside
1215 (see YIELD_VALUE). */
1216 SWAP_EXC_STATE();
1217 }
1218 else {
1219 SAVE_EXC_STATE();
1220 }
1221 }
1222
1223 #ifdef LLTRACE 1040 #ifdef LLTRACE
1224 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != 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);
1225 #endif 1045 #endif
1226 1046
1227 why = WHY_NOT; 1047 why = WHY_NOT;
1228 err = 0; 1048 err = 0;
1229 x = Py_None; /* Not a reference, just anything non-NULL */ 1049 x = Py_None; /* Not a reference, just anything non-NULL */
1230 w = NULL; 1050 w = NULL;
1231 1051
1232 if (throwflag) { /* support for generator.throw() */ 1052 if (throwflag) { /* support for generator.throw() */
1233 why = WHY_EXCEPTION; 1053 why = WHY_EXCEPTION;
1234 goto on_error; 1054 goto on_error;
(...skipping 21 matching lines...) Expand all
1256 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ 1076 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
1257 1077
1258 /* Do periodic things. Doing this every time through 1078 /* Do periodic things. Doing this every time through
1259 the loop would add too much overhead, so we do it 1079 the loop would add too much overhead, so we do it
1260 only every Nth instruction. We also do it if 1080 only every Nth instruction. We also do it if
1261 ``pendingcalls_to_do'' is set, i.e. when an asynchronous 1081 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1262 event needs attention (e.g. a signal handler or 1082 event needs attention (e.g. a signal handler or
1263 async I/O handler); see Py_AddPendingCall() and 1083 async I/O handler); see Py_AddPendingCall() and
1264 Py_MakePendingCalls() above. */ 1084 Py_MakePendingCalls() above. */
1265 1085
1266 if (_Py_atomic_load_relaxed(&eval_breaker)) { 1086 if (--_Py_Ticker < 0) {
1267 if (*next_instr == SETUP_FINALLY) { 1087 if (*next_instr == SETUP_FINALLY) {
1268 /* Make the last opcode before 1088 /* Make the last opcode before
1269 a try: finally: block uninterruptable. */ 1089 a try: finally: block uninterruptible. */
1270 goto fast_next_opcode; 1090 goto fast_next_opcode;
1271 } 1091 }
1092 _Py_Ticker = _Py_CheckInterval;
1272 tstate->tick_counter++; 1093 tstate->tick_counter++;
1273 #ifdef WITH_TSC 1094 #ifdef WITH_TSC
1274 ticked = 1; 1095 ticked = 1;
1275 #endif 1096 #endif
1276 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) { 1097 if (pendingcalls_to_do) {
1277 if (Py_MakePendingCalls() < 0) { 1098 if (Py_MakePendingCalls() < 0) {
1278 why = WHY_EXCEPTION; 1099 why = WHY_EXCEPTION;
1279 goto on_error; 1100 goto on_error;
1280 } 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;
1281 } 1108 }
1282 #ifdef WITH_THREAD 1109 #ifdef WITH_THREAD
1283 if (_Py_atomic_load_relaxed(&gil_drop_request)) { 1110 if (interpreter_lock) {
1284 /* Give another thread a chance */ 1111 /* Give another thread a chance */
1112
1285 if (PyThreadState_Swap(NULL) != tstate) 1113 if (PyThreadState_Swap(NULL) != tstate)
1286 Py_FatalError("ceval: tstate mix-up"); 1114 Py_FatalError("ceval: tstate mix-up");
1287 drop_gil(tstate); 1115 PyThread_release_lock(interpreter_lock);
1288 1116
1289 /* Other threads may run now */ 1117 /* Other threads may run now */
1290 1118
1291 take_gil(tstate); 1119 PyThread_acquire_lock(interpreter_lock, 1);
1120
1292 if (PyThreadState_Swap(tstate) != NULL) 1121 if (PyThreadState_Swap(tstate) != NULL)
1293 Py_FatalError("ceval: orphan tstate"); 1122 Py_FatalError("ceval: orphan tstate");
1294 } 1123
1295 #endif 1124 /* Check for thread interrupts */
1296 /* Check for asynchronous exceptions. */ 1125
1297 if (tstate->async_exc != NULL) { 1126 if (tstate->async_exc != NULL) {
1298 x = tstate->async_exc; 1127 x = tstate->async_exc;
1299 tstate->async_exc = NULL; 1128 tstate->async_exc = NULL;
1300 UNSIGNAL_ASYNC_EXC(); 1129 PyErr_SetNone(x);
1301 PyErr_SetNone(x); 1130 Py_DECREF(x);
1302 Py_DECREF(x); 1131 why = WHY_EXCEPTION;
1303 why = WHY_EXCEPTION; 1132 goto on_error;
1304 goto on_error; 1133 }
1305 } 1134 }
1135 #endif
1306 } 1136 }
1307 1137
1308 fast_next_opcode: 1138 fast_next_opcode:
1309 f->f_lasti = INSTR_OFFSET(); 1139 f->f_lasti = INSTR_OFFSET();
1310 1140
1311 /* line-by-line tracing support */ 1141 /* line-by-line tracing support */
1312 1142
1313 if (_Py_TracingPossible && 1143 if (_Py_TracingPossible &&
1314 tstate->c_tracefunc != NULL && !tstate->tracing) { 1144 tstate->c_tracefunc != NULL && !tstate->tracing) {
1315 /* see maybe_call_line_trace 1145 /* see maybe_call_line_trace
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1368 1198
1369 switch (opcode) { 1199 switch (opcode) {
1370 1200
1371 /* BEWARE! 1201 /* BEWARE!
1372 It is essential that any operation that fails sets either 1202 It is essential that any operation that fails sets either
1373 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,
1374 and that no operation that succeeds does this! */ 1204 and that no operation that succeeds does this! */
1375 1205
1376 /* case STOP_CODE: this is an error! */ 1206 /* case STOP_CODE: this is an error! */
1377 1207
1378 TARGET(NOP) 1208 TARGET_NOARG(NOP)
1209 {
1379 FAST_DISPATCH(); 1210 FAST_DISPATCH();
1211 }
1380 1212
1381 TARGET(LOAD_FAST) 1213 TARGET(LOAD_FAST)
1214 {
1382 x = GETLOCAL(oparg); 1215 x = GETLOCAL(oparg);
1383 if (x != NULL) { 1216 if (x != NULL) {
1384 Py_INCREF(x); 1217 Py_INCREF(x);
1385 PUSH(x); 1218 PUSH(x);
1386 FAST_DISPATCH(); 1219 FAST_DISPATCH();
1387 } 1220 }
1388 format_exc_check_arg(PyExc_UnboundLocalError, 1221 format_exc_check_arg(PyExc_UnboundLocalError,
1389 UNBOUNDLOCAL_ERROR_MSG, 1222 UNBOUNDLOCAL_ERROR_MSG,
1390 PyTuple_GetItem(co->co_varnames, oparg)); 1223 PyTuple_GetItem(co->co_varnames, oparg));
1391 break; 1224 break;
1225 }
1392 1226
1393 TARGET(LOAD_CONST) 1227 TARGET(LOAD_CONST)
1228 {
1394 x = GETITEM(consts, oparg); 1229 x = GETITEM(consts, oparg);
1395 Py_INCREF(x); 1230 Py_INCREF(x);
1396 PUSH(x); 1231 PUSH(x);
1397 FAST_DISPATCH(); 1232 FAST_DISPATCH();
1233 }
1398 1234
1399 PREDICTED_WITH_ARG(STORE_FAST); 1235 PREDICTED_WITH_ARG(STORE_FAST);
1400 TARGET(STORE_FAST) 1236 TARGET(STORE_FAST)
1237 {
1401 v = POP(); 1238 v = POP();
1402 SETLOCAL(oparg, v); 1239 SETLOCAL(oparg, v);
1403 FAST_DISPATCH(); 1240 FAST_DISPATCH();
1404 1241 }
1405 TARGET(POP_TOP) 1242
1243 TARGET_NOARG(POP_TOP)
1244 {
1406 v = POP(); 1245 v = POP();
1407 Py_DECREF(v); 1246 Py_DECREF(v);
1408 FAST_DISPATCH(); 1247 FAST_DISPATCH();
1409 1248 }
1410 TARGET(ROT_TWO) 1249
1250 TARGET_NOARG(ROT_TWO)
1251 {
1411 v = TOP(); 1252 v = TOP();
1412 w = SECOND(); 1253 w = SECOND();
1413 SET_TOP(w); 1254 SET_TOP(w);
1414 SET_SECOND(v); 1255 SET_SECOND(v);
1415 FAST_DISPATCH(); 1256 FAST_DISPATCH();
1416 1257 }
1417 TARGET(ROT_THREE) 1258
1259 TARGET_NOARG(ROT_THREE)
1260 {
1418 v = TOP(); 1261 v = TOP();
1419 w = SECOND(); 1262 w = SECOND();
1420 x = THIRD(); 1263 x = THIRD();
1421 SET_TOP(w); 1264 SET_TOP(w);
1422 SET_SECOND(x); 1265 SET_SECOND(x);
1423 SET_THIRD(v); 1266 SET_THIRD(v);
1424 FAST_DISPATCH(); 1267 FAST_DISPATCH();
1425 1268 }
1426 TARGET(DUP_TOP) 1269
1270 TARGET_NOARG(ROT_FOUR)
1271 {
1272 u = TOP();
1273 v = SECOND();
1274 w = THIRD();
1275 x = FOURTH();
1276 SET_TOP(v);
1277 SET_SECOND(w);
1278 SET_THIRD(x);
1279 SET_FOURTH(u);
1280 FAST_DISPATCH();
1281 }
1282
1283
1284 TARGET_NOARG(DUP_TOP)
1285 {
1427 v = TOP(); 1286 v = TOP();
1428 Py_INCREF(v); 1287 Py_INCREF(v);
1429 PUSH(v); 1288 PUSH(v);
1430 FAST_DISPATCH(); 1289 FAST_DISPATCH();
1431 1290 }
1432 TARGET(DUP_TOP_TWO) 1291
1433 x = TOP(); 1292
1434 Py_INCREF(x); 1293 TARGET(DUP_TOPX)
1435 w = SECOND(); 1294 {
1436 Py_INCREF(w); 1295 if (oparg == 2) {
1437 STACKADJ(2); 1296 x = TOP();
1438 SET_TOP(x); 1297 Py_INCREF(x);
1439 SET_SECOND(w); 1298 w = SECOND();
1440 FAST_DISPATCH(); 1299 Py_INCREF(w);
1441 1300 STACKADJ(2);
1442 TARGET(UNARY_POSITIVE) 1301 SET_TOP(x);
1302 SET_SECOND(w);
1303 FAST_DISPATCH();
1304 } else if (oparg == 3) {
1305 x = TOP();
1306 Py_INCREF(x);
1307 w = SECOND();
1308 Py_INCREF(w);
1309 v = THIRD();
1310 Py_INCREF(v);
1311 STACKADJ(3);
1312 SET_TOP(x);
1313 SET_SECOND(w);
1314 SET_THIRD(v);
1315 FAST_DISPATCH();
1316 }
1317 Py_FatalError("invalid argument to DUP_TOPX"
1318 " (bytecode corruption?)");
1319 /* Never returns, so don't bother to set why. */
1320 break;
1321 }
1322
1323 TARGET_NOARG(UNARY_POSITIVE)
1324 {
1443 v = TOP(); 1325 v = TOP();
1444 x = PyNumber_Positive(v); 1326 x = PyNumber_Positive(v);
1445 Py_DECREF(v); 1327 Py_DECREF(v);
1446 SET_TOP(x); 1328 SET_TOP(x);
1447 if (x != NULL) DISPATCH(); 1329 if (x != NULL) DISPATCH();
1448 break; 1330 break;
1449 1331 }
1450 TARGET(UNARY_NEGATIVE) 1332
1333 TARGET_NOARG( UNARY_NEGATIVE)
1334 {
1451 v = TOP(); 1335 v = TOP();
1452 x = PyNumber_Negative(v); 1336 x = PyNumber_Negative(v);
1453 Py_DECREF(v); 1337 Py_DECREF(v);
1454 SET_TOP(x); 1338 SET_TOP(x);
1455 if (x != NULL) DISPATCH(); 1339 if (x != NULL) DISPATCH();
1456 break; 1340 break;
1457 1341 }
1458 TARGET(UNARY_NOT) 1342
1343 TARGET_NOARG(UNARY_NOT)
1344 {
1459 v = TOP(); 1345 v = TOP();
1460 err = PyObject_IsTrue(v); 1346 err = PyObject_IsTrue(v);
1461 Py_DECREF(v); 1347 Py_DECREF(v);
1462 if (err == 0) { 1348 if (err == 0) {
1463 Py_INCREF(Py_True); 1349 Py_INCREF(Py_True);
1464 SET_TOP(Py_True); 1350 SET_TOP(Py_True);
1465 DISPATCH(); 1351 DISPATCH();
1466 } 1352 }
1467 else if (err > 0) { 1353 else if (err > 0) {
1468 Py_INCREF(Py_False); 1354 Py_INCREF(Py_False);
1469 SET_TOP(Py_False); 1355 SET_TOP(Py_False);
1470 err = 0; 1356 err = 0;
1471 DISPATCH(); 1357 DISPATCH();
1472 } 1358 }
1473 STACKADJ(-1); 1359 STACKADJ(-1);
1474 break; 1360 break;
1475 1361 }
1476 TARGET(UNARY_INVERT) 1362
1363 TARGET_NOARG(UNARY_CONVERT)
1364 {
1365 v = TOP();
1366 x = PyObject_Repr(v);
1367 Py_DECREF(v);
1368 SET_TOP(x);
1369 if (x != NULL) DISPATCH();
1370 break;
1371 }
1372
1373 TARGET_NOARG(UNARY_INVERT)
1374 {
1477 v = TOP(); 1375 v = TOP();
1478 x = PyNumber_Invert(v); 1376 x = PyNumber_Invert(v);
1479 Py_DECREF(v); 1377 Py_DECREF(v);
1480 SET_TOP(x); 1378 SET_TOP(x);
1481 if (x != NULL) DISPATCH(); 1379 if (x != NULL) DISPATCH();
1482 break; 1380 break;
1483 1381 }
1484 TARGET(BINARY_POWER) 1382
1383 TARGET_NOARG(BINARY_POWER)
1384 {
1485 w = POP(); 1385 w = POP();
1486 v = TOP(); 1386 v = TOP();
1487 x = PyNumber_Power(v, w, Py_None); 1387 x = PyNumber_Power(v, w, Py_None);
1488 Py_DECREF(v); 1388 Py_DECREF(v);
1489 Py_DECREF(w); 1389 Py_DECREF(w);
1490 SET_TOP(x); 1390 SET_TOP(x);
1491 if (x != NULL) DISPATCH(); 1391 if (x != NULL) DISPATCH();
1492 break; 1392 break;
1493 1393 }
1494 TARGET(BINARY_MULTIPLY) 1394
1395 TARGET_NOARG(BINARY_MULTIPLY)
1396 {
1495 w = POP(); 1397 w = POP();
1496 v = TOP(); 1398 v = TOP();
1497 x = PyNumber_Multiply(v, w); 1399 x = PyNumber_Multiply(v, w);
1498 Py_DECREF(v); 1400 Py_DECREF(v);
1499 Py_DECREF(w); 1401 Py_DECREF(w);
1500 SET_TOP(x); 1402 SET_TOP(x);
1501 if (x != NULL) DISPATCH(); 1403 if(x!=NULL) DISPATCH();
1502 break; 1404 break;
1503 1405 }
1504 TARGET(BINARY_TRUE_DIVIDE) 1406
1407 TARGET_NOARG(BINARY_DIVIDE)
1408 {
1409 if (!_Py_QnewFlag) {
1410 w = POP();
1411 v = TOP();
1412 x = PyNumber_Divide(v, w);
1413 Py_DECREF(v);
1414 Py_DECREF(w);
1415 SET_TOP(x);
1416 if (x != NULL) DISPATCH();
1417 break;
1418 }
1419 }
1420 /* -Qnew is in effect: fall through to BINARY_TRUE_DIVIDE */
1421 TARGET_NOARG(BINARY_TRUE_DIVIDE)
1422 {
1505 w = POP(); 1423 w = POP();
1506 v = TOP(); 1424 v = TOP();
1507 x = PyNumber_TrueDivide(v, w); 1425 x = PyNumber_TrueDivide(v, w);
1508 Py_DECREF(v); 1426 Py_DECREF(v);
1509 Py_DECREF(w); 1427 Py_DECREF(w);
1510 SET_TOP(x); 1428 SET_TOP(x);
1511 if (x != NULL) DISPATCH(); 1429 if (x != NULL) DISPATCH();
1512 break; 1430 break;
1513 1431 }
1514 TARGET(BINARY_FLOOR_DIVIDE) 1432
1433 TARGET_NOARG(BINARY_FLOOR_DIVIDE)
1434 {
1515 w = POP(); 1435 w = POP();
1516 v = TOP(); 1436 v = TOP();
1517 x = PyNumber_FloorDivide(v, w); 1437 x = PyNumber_FloorDivide(v, w);
1518 Py_DECREF(v); 1438 Py_DECREF(v);
1519 Py_DECREF(w); 1439 Py_DECREF(w);
1520 SET_TOP(x); 1440 SET_TOP(x);
1521 if (x != NULL) DISPATCH(); 1441 if (x != NULL) DISPATCH();
1522 break; 1442 break;
1523 1443 }
1524 TARGET(BINARY_MODULO) 1444
1445 TARGET_NOARG(BINARY_MODULO)
1446 {
1525 w = POP(); 1447 w = POP();
1526 v = TOP(); 1448 v = TOP();
1527 if (PyUnicode_CheckExact(v)) 1449 if (PyString_CheckExact(v))
1528 x = PyUnicode_Format(v, w); 1450 x = PyString_Format(v, w);
1529 else 1451 else
1530 x = PyNumber_Remainder(v, w); 1452 x = PyNumber_Remainder(v, w);
1531 Py_DECREF(v); 1453 Py_DECREF(v);
1532 Py_DECREF(w); 1454 Py_DECREF(w);
1533 SET_TOP(x); 1455 SET_TOP(x);
1534 if (x != NULL) DISPATCH(); 1456 if (x != NULL) DISPATCH();
1535 break; 1457 break;
1536 1458 }
1537 TARGET(BINARY_ADD) 1459
1460 TARGET_NOARG(BINARY_ADD)
1461 {
1538 w = POP(); 1462 w = POP();
1539 v = TOP(); 1463 v = TOP();
1540 if (PyUnicode_CheckExact(v) && 1464 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1541 PyUnicode_CheckExact(w)) { 1465 /* INLINE: int + int */
1542 x = unicode_concatenate(v, w, f, next_instr); 1466 register long a, b, i;
1543 /* unicode_concatenate consumed the ref to v */ 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 */
1544 goto skip_decref_vx; 1480 goto skip_decref_vx;
1545 } 1481 }
1546 else { 1482 else {
1483 slow_add:
1547 x = PyNumber_Add(v, w); 1484 x = PyNumber_Add(v, w);
1548 } 1485 }
1549 Py_DECREF(v); 1486 Py_DECREF(v);
1550 skip_decref_vx: 1487 skip_decref_vx:
1551 Py_DECREF(w); 1488 Py_DECREF(w);
1552 SET_TOP(x); 1489 SET_TOP(x);
1553 if (x != NULL) DISPATCH(); 1490 if (x != NULL) DISPATCH();
1554 break; 1491 break;
1555 1492 }
1556 TARGET(BINARY_SUBTRACT) 1493
1494 TARGET_NOARG(BINARY_SUBTRACT)
1495 {
1557 w = POP(); 1496 w = POP();
1558 v = TOP(); 1497 v = TOP();
1559 x = PyNumber_Subtract(v, w); 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 }
1560 Py_DECREF(v); 1514 Py_DECREF(v);
1561 Py_DECREF(w); 1515 Py_DECREF(w);
1562 SET_TOP(x); 1516 SET_TOP(x);
1563 if (x != NULL) DISPATCH(); 1517 if (x != NULL) DISPATCH();
1564 break; 1518 break;
1565 1519 }
1566 TARGET(BINARY_SUBSCR) 1520
1521 TARGET_NOARG(BINARY_SUBSCR)
1522 {
1567 w = POP(); 1523 w = POP();
1568 v = TOP(); 1524 v = TOP();
1569 x = PyObject_GetItem(v, w); 1525 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
1526 /* INLINE: list[int] */
1527 Py_ssize_t i = PyInt_AsSsize_t(w);
1528 if (i < 0)
1529 i += PyList_GET_SIZE(v);
1530 if (i >= 0 && i < PyList_GET_SIZE(v)) {
1531 x = PyList_GET_ITEM(v, i);
1532 Py_INCREF(x);
1533 }
1534 else
1535 goto slow_get;
1536 }
1537 else
1538 slow_get:
1539 x = PyObject_GetItem(v, w);
1570 Py_DECREF(v); 1540 Py_DECREF(v);
1571 Py_DECREF(w); 1541 Py_DECREF(w);
1572 SET_TOP(x); 1542 SET_TOP(x);
1573 if (x != NULL) DISPATCH(); 1543 if (x != NULL) DISPATCH();
1574 break; 1544 break;
1575 1545 }
1576 TARGET(BINARY_LSHIFT) 1546
1547 TARGET_NOARG(BINARY_LSHIFT)
1548 {
1577 w = POP(); 1549 w = POP();
1578 v = TOP(); 1550 v = TOP();
1579 x = PyNumber_Lshift(v, w); 1551 x = PyNumber_Lshift(v, w);
1580 Py_DECREF(v); 1552 Py_DECREF(v);
1581 Py_DECREF(w); 1553 Py_DECREF(w);
1582 SET_TOP(x); 1554 SET_TOP(x);
1583 if (x != NULL) DISPATCH(); 1555 if (x != NULL) DISPATCH();
1584 break; 1556 break;
1585 1557 }
1586 TARGET(BINARY_RSHIFT) 1558
1559 TARGET_NOARG(BINARY_RSHIFT)
1560 {
1587 w = POP(); 1561 w = POP();
1588 v = TOP(); 1562 v = TOP();
1589 x = PyNumber_Rshift(v, w); 1563 x = PyNumber_Rshift(v, w);
1590 Py_DECREF(v); 1564 Py_DECREF(v);
1591 Py_DECREF(w); 1565 Py_DECREF(w);
1592 SET_TOP(x); 1566 SET_TOP(x);
1593 if (x != NULL) DISPATCH(); 1567 if (x != NULL) DISPATCH();
1594 break; 1568 break;
1595 1569 }
1596 TARGET(BINARY_AND) 1570
1571 TARGET_NOARG(BINARY_AND)
1572 {
1597 w = POP(); 1573 w = POP();
1598 v = TOP(); 1574 v = TOP();
1599 x = PyNumber_And(v, w); 1575 x = PyNumber_And(v, w);
1600 Py_DECREF(v); 1576 Py_DECREF(v);
1601 Py_DECREF(w); 1577 Py_DECREF(w);
1602 SET_TOP(x); 1578 SET_TOP(x);
1603 if (x != NULL) DISPATCH(); 1579 if (x != NULL) DISPATCH();
1604 break; 1580 break;
1605 1581 }
1606 TARGET(BINARY_XOR) 1582
1583 TARGET_NOARG(BINARY_XOR)
1584 {
1607 w = POP(); 1585 w = POP();
1608 v = TOP(); 1586 v = TOP();
1609 x = PyNumber_Xor(v, w); 1587 x = PyNumber_Xor(v, w);
1610 Py_DECREF(v); 1588 Py_DECREF(v);
1611 Py_DECREF(w); 1589 Py_DECREF(w);
1612 SET_TOP(x); 1590 SET_TOP(x);
1613 if (x != NULL) DISPATCH(); 1591 if (x != NULL) DISPATCH();
1614 break; 1592 break;
1615 1593 }
1616 TARGET(BINARY_OR) 1594
1595 TARGET_NOARG(BINARY_OR)
1596 {
1617 w = POP(); 1597 w = POP();
1618 v = TOP(); 1598 v = TOP();
1619 x = PyNumber_Or(v, w); 1599 x = PyNumber_Or(v, w);
1620 Py_DECREF(v); 1600 Py_DECREF(v);
1621 Py_DECREF(w); 1601 Py_DECREF(w);
1622 SET_TOP(x); 1602 SET_TOP(x);
1623 if (x != NULL) DISPATCH(); 1603 if (x != NULL) DISPATCH();
1624 break; 1604 break;
1605 }
1625 1606
1626 TARGET(LIST_APPEND) 1607 TARGET(LIST_APPEND)
1608 {
1627 w = POP(); 1609 w = POP();
1628 v = PEEK(oparg); 1610 v = PEEK(oparg);
1629 err = PyList_Append(v, w); 1611 err = PyList_Append(v, w);
1630 Py_DECREF(w); 1612 Py_DECREF(w);
1631 if (err == 0) { 1613 if (err == 0) {
1632 PREDICT(JUMP_ABSOLUTE); 1614 PREDICT(JUMP_ABSOLUTE);
1633 DISPATCH(); 1615 DISPATCH();
1634 } 1616 }
1635 break; 1617 break;
1618 }
1636 1619
1637 TARGET(SET_ADD) 1620 TARGET(SET_ADD)
1621 {
1638 w = POP(); 1622 w = POP();
1639 v = stack_pointer[-oparg]; 1623 v = stack_pointer[-oparg];
1640 err = PySet_Add(v, w); 1624 err = PySet_Add(v, w);
1641 Py_DECREF(w); 1625 Py_DECREF(w);
1642 if (err == 0) { 1626 if (err == 0) {
1643 PREDICT(JUMP_ABSOLUTE); 1627 PREDICT(JUMP_ABSOLUTE);
1644 DISPATCH(); 1628 DISPATCH();
1645 } 1629 }
1646 break; 1630 break;
1647 1631 }
1648 TARGET(INPLACE_POWER) 1632
1633 TARGET_NOARG(INPLACE_POWER)
1634 {
1649 w = POP(); 1635 w = POP();
1650 v = TOP(); 1636 v = TOP();
1651 x = PyNumber_InPlacePower(v, w, Py_None); 1637 x = PyNumber_InPlacePower(v, w, Py_None);
1652 Py_DECREF(v); 1638 Py_DECREF(v);
1653 Py_DECREF(w); 1639 Py_DECREF(w);
1654 SET_TOP(x); 1640 SET_TOP(x);
1655 if (x != NULL) DISPATCH(); 1641 if (x != NULL) DISPATCH();
1656 break; 1642 break;
1657 1643 }
1658 TARGET(INPLACE_MULTIPLY) 1644
1645 TARGET_NOARG(INPLACE_MULTIPLY)
1646 {
1659 w = POP(); 1647 w = POP();
1660 v = TOP(); 1648 v = TOP();
1661 x = PyNumber_InPlaceMultiply(v, w); 1649 x = PyNumber_InPlaceMultiply(v, w);
1662 Py_DECREF(v); 1650 Py_DECREF(v);
1663 Py_DECREF(w); 1651 Py_DECREF(w);
1664 SET_TOP(x); 1652 SET_TOP(x);
1665 if (x != NULL) DISPATCH(); 1653 if (x != NULL) DISPATCH();
1666 break; 1654 break;
1667 1655 }
1668 TARGET(INPLACE_TRUE_DIVIDE) 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 {
1669 w = POP(); 1674 w = POP();
1670 v = TOP(); 1675 v = TOP();
1671 x = PyNumber_InPlaceTrueDivide(v, w); 1676 x = PyNumber_InPlaceTrueDivide(v, w);
1672 Py_DECREF(v); 1677 Py_DECREF(v);
1673 Py_DECREF(w); 1678 Py_DECREF(w);
1674 SET_TOP(x); 1679 SET_TOP(x);
1675 if (x != NULL) DISPATCH(); 1680 if (x != NULL) DISPATCH();
1676 break; 1681 break;
1677 1682 }
1678 TARGET(INPLACE_FLOOR_DIVIDE) 1683
1684 TARGET_NOARG(INPLACE_FLOOR_DIVIDE)
1685 {
1679 w = POP(); 1686 w = POP();
1680 v = TOP(); 1687 v = TOP();
1681 x = PyNumber_InPlaceFloorDivide(v, w); 1688 x = PyNumber_InPlaceFloorDivide(v, w);
1682 Py_DECREF(v); 1689 Py_DECREF(v);
1683 Py_DECREF(w); 1690 Py_DECREF(w);
1684 SET_TOP(x); 1691 SET_TOP(x);
1685 if (x != NULL) DISPATCH(); 1692 if (x != NULL) DISPATCH();
1686 break; 1693 break;
1687 1694 }
1688 TARGET(INPLACE_MODULO) 1695
1696 TARGET_NOARG(INPLACE_MODULO)
1697 {
1689 w = POP(); 1698 w = POP();
1690 v = TOP(); 1699 v = TOP();
1691 x = PyNumber_InPlaceRemainder(v, w); 1700 x = PyNumber_InPlaceRemainder(v, w);
1692 Py_DECREF(v); 1701 Py_DECREF(v);
1693 Py_DECREF(w); 1702 Py_DECREF(w);
1694 SET_TOP(x); 1703 SET_TOP(x);
1695 if (x != NULL) DISPATCH(); 1704 if (x != NULL) DISPATCH();
1696 break; 1705 break;
1697 1706 }
1698 TARGET(INPLACE_ADD) 1707
1708 TARGET_NOARG(INPLACE_ADD)
1709 {
1699 w = POP(); 1710 w = POP();
1700 v = TOP(); 1711 v = TOP();
1701 if (PyUnicode_CheckExact(v) && 1712 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1702 PyUnicode_CheckExact(w)) { 1713 /* INLINE: int + int */
1703 x = unicode_concatenate(v, w, f, next_instr); 1714 register long a, b, i;
1704 /* unicode_concatenate consumed the ref to v */ 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 */
1705 goto skip_decref_v; 1726 goto skip_decref_v;
1706 } 1727 }
1707 else { 1728 else {
1729 slow_iadd:
1708 x = PyNumber_InPlaceAdd(v, w); 1730 x = PyNumber_InPlaceAdd(v, w);
1709 } 1731 }
1710 Py_DECREF(v); 1732 Py_DECREF(v);
1711 skip_decref_v: 1733 skip_decref_v:
1712 Py_DECREF(w); 1734 Py_DECREF(w);
1713 SET_TOP(x); 1735 SET_TOP(x);
1714 if (x != NULL) DISPATCH(); 1736 if (x != NULL) DISPATCH();
1715 break; 1737 break;
1716 1738 }
1717 TARGET(INPLACE_SUBTRACT) 1739
1740 TARGET_NOARG(INPLACE_SUBTRACT)
1741 {
1718 w = POP(); 1742 w = POP();
1719 v = TOP(); 1743 v = TOP();
1720 x = PyNumber_InPlaceSubtract(v, w); 1744 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1745 /* INLINE: int - int */
1746 register long a, b, i;
1747 a = PyInt_AS_LONG(v);
1748 b = PyInt_AS_LONG(w);
1749 i = a - b;
1750 if ((i^a) < 0 && (i^~b) < 0)
1751 goto slow_isub;
1752 x = PyInt_FromLong(i);
1753 }
1754 else {
1755 slow_isub:
1756 x = PyNumber_InPlaceSubtract(v, w);
1757 }
1721 Py_DECREF(v); 1758 Py_DECREF(v);
1722 Py_DECREF(w); 1759 Py_DECREF(w);
1723 SET_TOP(x); 1760 SET_TOP(x);
1724 if (x != NULL) DISPATCH(); 1761 if (x != NULL) DISPATCH();
1725 break; 1762 break;
1726 1763 }
1727 TARGET(INPLACE_LSHIFT) 1764
1765 TARGET_NOARG(INPLACE_LSHIFT)
1766 {
1728 w = POP(); 1767 w = POP();
1729 v = TOP(); 1768 v = TOP();
1730 x = PyNumber_InPlaceLshift(v, w); 1769 x = PyNumber_InPlaceLshift(v, w);
1731 Py_DECREF(v); 1770 Py_DECREF(v);
1732 Py_DECREF(w); 1771 Py_DECREF(w);
1733 SET_TOP(x); 1772 SET_TOP(x);
1734 if (x != NULL) DISPATCH(); 1773 if (x != NULL) DISPATCH();
1735 break; 1774 break;
1736 1775 }
1737 TARGET(INPLACE_RSHIFT) 1776
1777 TARGET_NOARG(INPLACE_RSHIFT)
1778 {
1738 w = POP(); 1779 w = POP();
1739 v = TOP(); 1780 v = TOP();
1740 x = PyNumber_InPlaceRshift(v, w); 1781 x = PyNumber_InPlaceRshift(v, w);
1741 Py_DECREF(v); 1782 Py_DECREF(v);
1742 Py_DECREF(w); 1783 Py_DECREF(w);
1743 SET_TOP(x); 1784 SET_TOP(x);
1744 if (x != NULL) DISPATCH(); 1785 if (x != NULL) DISPATCH();
1745 break; 1786 break;
1746 1787 }
1747 TARGET(INPLACE_AND) 1788
1789 TARGET_NOARG(INPLACE_AND)
1790 {
1748 w = POP(); 1791 w = POP();
1749 v = TOP(); 1792 v = TOP();
1750 x = PyNumber_InPlaceAnd(v, w); 1793 x = PyNumber_InPlaceAnd(v, w);
1751 Py_DECREF(v); 1794 Py_DECREF(v);
1752 Py_DECREF(w); 1795 Py_DECREF(w);
1753 SET_TOP(x); 1796 SET_TOP(x);
1754 if (x != NULL) DISPATCH(); 1797 if (x != NULL) DISPATCH();
1755 break; 1798 break;
1756 1799 }
1757 TARGET(INPLACE_XOR) 1800
1801 TARGET_NOARG(INPLACE_XOR)
1802 {
1758 w = POP(); 1803 w = POP();
1759 v = TOP(); 1804 v = TOP();
1760 x = PyNumber_InPlaceXor(v, w); 1805 x = PyNumber_InPlaceXor(v, w);
1761 Py_DECREF(v); 1806 Py_DECREF(v);
1762 Py_DECREF(w); 1807 Py_DECREF(w);
1763 SET_TOP(x); 1808 SET_TOP(x);
1764 if (x != NULL) DISPATCH(); 1809 if (x != NULL) DISPATCH();
1765 break; 1810 break;
1766 1811 }
1767 TARGET(INPLACE_OR) 1812
1813 TARGET_NOARG(INPLACE_OR)
1814 {
1768 w = POP(); 1815 w = POP();
1769 v = TOP(); 1816 v = TOP();
1770 x = PyNumber_InPlaceOr(v, w); 1817 x = PyNumber_InPlaceOr(v, w);
1771 Py_DECREF(v); 1818 Py_DECREF(v);
1772 Py_DECREF(w); 1819 Py_DECREF(w);
1773 SET_TOP(x); 1820 SET_TOP(x);
1774 if (x != NULL) DISPATCH(); 1821 if (x != NULL) DISPATCH();
1775 break; 1822 break;
1776 1823 }
1777 TARGET(STORE_SUBSCR) 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 {
1778 w = TOP(); 1904 w = TOP();
1779 v = SECOND(); 1905 v = SECOND();
1780 u = THIRD(); 1906 u = THIRD();
1781 STACKADJ(-3); 1907 STACKADJ(-3);
1782 /* v[w] = u */ 1908 /* v[w] = u */
1783 err = PyObject_SetItem(v, w, u); 1909 err = PyObject_SetItem(v, w, u);
1784 Py_DECREF(u); 1910 Py_DECREF(u);
1785 Py_DECREF(v); 1911 Py_DECREF(v);
1786 Py_DECREF(w); 1912 Py_DECREF(w);
1787 if (err == 0) DISPATCH(); 1913 if (err == 0) DISPATCH();
1788 break; 1914 break;
1789 1915 }
1790 TARGET(DELETE_SUBSCR) 1916
1917 TARGET_NOARG(DELETE_SUBSCR)
1918 {
1791 w = TOP(); 1919 w = TOP();
1792 v = SECOND(); 1920 v = SECOND();
1793 STACKADJ(-2); 1921 STACKADJ(-2);
1794 /* del v[w] */ 1922 /* del v[w] */
1795 err = PyObject_DelItem(v, w); 1923 err = PyObject_DelItem(v, w);
1796 Py_DECREF(v); 1924 Py_DECREF(v);
1797 Py_DECREF(w); 1925 Py_DECREF(w);
1798 if (err == 0) DISPATCH(); 1926 if (err == 0) DISPATCH();
1799 break; 1927 break;
1800 1928 }
1801 TARGET(PRINT_EXPR) 1929
1930 TARGET_NOARG(PRINT_EXPR)
1931 {
1802 v = POP(); 1932 v = POP();
1803 w = PySys_GetObject("displayhook"); 1933 w = PySys_GetObject("displayhook");
1804 if (w == NULL) { 1934 if (w == NULL) {
1805 PyErr_SetString(PyExc_RuntimeError, 1935 PyErr_SetString(PyExc_RuntimeError,
1806 "lost sys.displayhook"); 1936 "lost sys.displayhook");
1807 err = -1; 1937 err = -1;
1808 x = NULL; 1938 x = NULL;
1809 } 1939 }
1810 if (err == 0) { 1940 if (err == 0) {
1811 x = PyTuple_Pack(1, v); 1941 x = PyTuple_Pack(1, v);
1812 if (x == NULL) 1942 if (x == NULL)
1813 err = -1; 1943 err = -1;
1814 } 1944 }
1815 if (err == 0) { 1945 if (err == 0) {
1816 w = PyEval_CallObject(w, x); 1946 w = PyEval_CallObject(w, x);
1817 Py_XDECREF(w); 1947 Py_XDECREF(w);
1818 if (w == NULL) 1948 if (w == NULL)
1819 err = -1; 1949 err = -1;
1820 } 1950 }
1821 Py_DECREF(v); 1951 Py_DECREF(v);
1822 Py_XDECREF(x); 1952 Py_XDECREF(x);
1823 break; 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;
2042 }
1824 2043
1825 #ifdef CASE_TOO_BIG 2044 #ifdef CASE_TOO_BIG
1826 default: switch (opcode) { 2045 default: switch (opcode) {
1827 #endif 2046 #endif
2047
1828 TARGET(RAISE_VARARGS) 2048 TARGET(RAISE_VARARGS)
1829 v = w = NULL; 2049 {
2050 u = v = w = NULL;
1830 switch (oparg) { 2051 switch (oparg) {
2052 case 3:
2053 u = POP(); /* traceback */
2054 /* Fallthrough */
1831 case 2: 2055 case 2:
1832 v = POP(); /* cause */ 2056 v = POP(); /* value */
2057 /* Fallthrough */
1833 case 1: 2058 case 1:
1834 w = POP(); /* exc */ 2059 w = POP(); /* exc */
1835 case 0: /* Fallthrough */ 2060 case 0: /* Fallthrough */
1836 why = do_raise(w, v); 2061 why = do_raise(w, v, u);
1837 break; 2062 break;
1838 default: 2063 default:
1839 PyErr_SetString(PyExc_SystemError, 2064 PyErr_SetString(PyExc_SystemError,
1840 "bad RAISE_VARARGS oparg"); 2065 "bad RAISE_VARARGS oparg");
1841 why = WHY_EXCEPTION; 2066 why = WHY_EXCEPTION;
1842 break; 2067 break;
1843 } 2068 }
1844 break; 2069 break;
1845 2070 }
1846 TARGET(STORE_LOCALS) 2071
1847 x = POP(); 2072 TARGET_NOARG(LOAD_LOCALS)
1848 v = f->f_locals; 2073 {
1849 Py_XDECREF(v); 2074 if ((x = f->f_locals) != NULL)
1850 f->f_locals = x; 2075 {
1851 DISPATCH(); 2076 Py_INCREF(x);
1852 2077 PUSH(x);
1853 TARGET(RETURN_VALUE) 2078 DISPATCH();
2079 }
2080 PyErr_SetString(PyExc_SystemError, "no locals");
2081 break;
2082 }
2083
2084 TARGET_NOARG(RETURN_VALUE)
2085 {
1854 retval = POP(); 2086 retval = POP();
1855 why = WHY_RETURN; 2087 why = WHY_RETURN;
1856 goto fast_block_end; 2088 goto fast_block_end;
1857 2089 }
1858 TARGET(YIELD_VALUE) 2090
2091 TARGET_NOARG(YIELD_VALUE)
2092 {
1859 retval = POP(); 2093 retval = POP();
1860 f->f_stacktop = stack_pointer; 2094 f->f_stacktop = stack_pointer;
1861 why = WHY_YIELD; 2095 why = WHY_YIELD;
1862 /* Put aside the current exception state and restore
1863 that of the calling frame. This only serves when
1864 "yield" is used inside an except handler. */
1865 SWAP_EXC_STATE();
1866 goto fast_yield; 2096 goto fast_yield;
1867 2097 }
1868 TARGET(POP_EXCEPT) 2098
2099 TARGET_NOARG(EXEC_STMT)
2100 {
2101 w = TOP();
2102 v = SECOND();
2103 u = THIRD();
2104 STACKADJ(-3);
2105 READ_TIMESTAMP(intr0);
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 {
1869 { 2116 {
1870 PyTryBlock *b = PyFrame_BlockPop(f); 2117 PyTryBlock *b = PyFrame_BlockPop(f);
1871 if (b->b_type != EXCEPT_HANDLER) { 2118 while (STACK_LEVEL() > b->b_level) {
1872 PyErr_SetString(PyExc_SystemError, 2119 v = POP();
1873 "popped block is not an except handler"); 2120 Py_DECREF(v);
1874 why = WHY_EXCEPTION;
1875 break;
1876 } 2121 }
1877 UNWIND_EXCEPT_HANDLER(b);
1878 } 2122 }
1879 DISPATCH(); 2123 DISPATCH();
1880 2124 }
1881 TARGET(POP_BLOCK)
1882 {
1883 PyTryBlock *b = PyFrame_BlockPop(f);
1884 UNWIND_BLOCK(b);
1885 }
1886 DISPATCH();
1887 2125
1888 PREDICTED(END_FINALLY); 2126 PREDICTED(END_FINALLY);
1889 TARGET(END_FINALLY) 2127 TARGET_NOARG(END_FINALLY)
2128 {
1890 v = POP(); 2129 v = POP();
1891 if (PyLong_Check(v)) { 2130 if (PyInt_Check(v)) {
1892 why = (enum why_code) PyLong_AS_LONG(v); 2131 why = (enum why_code) PyInt_AS_LONG(v);
1893 assert(why != WHY_YIELD); 2132 assert(why != WHY_YIELD);
1894 if (why == WHY_RETURN || 2133 if (why == WHY_RETURN ||
1895 why == WHY_CONTINUE) 2134 why == WHY_CONTINUE)
1896 retval = POP(); 2135 retval = POP();
1897 if (why == WHY_SILENCED) { 2136 }
1898 /* An exception was silenced by 'with', we must 2137 else if (PyExceptionClass_Check(v) ||
1899 manually unwind the EXCEPT_HANDLER block which was 2138 PyString_Check(v)) {
1900 created when the exception was caught, otherwise
1901 the stack will be in an inconsistent state. */
1902 PyTryBlock *b = PyFrame_BlockPop(f);
1903 assert(b->b_type == EXCEPT_HANDLER);
1904 UNWIND_EXCEPT_HANDLER(b);
1905 why = WHY_NOT;
1906 }
1907 }
1908 else if (PyExceptionClass_Check(v)) {
1909 w = POP(); 2139 w = POP();
1910 u = POP(); 2140 u = POP();
1911 PyErr_Restore(v, w, u); 2141 PyErr_Restore(v, w, u);
1912 why = WHY_RERAISE; 2142 why = WHY_RERAISE;
1913 break; 2143 break;
1914 } 2144 }
1915 else if (v != Py_None) { 2145 else if (v != Py_None) {
1916 PyErr_SetString(PyExc_SystemError, 2146 PyErr_SetString(PyExc_SystemError,
1917 "'finally' pops bad exception"); 2147 "'finally' pops bad exception");
1918 why = WHY_EXCEPTION; 2148 why = WHY_EXCEPTION;
1919 } 2149 }
1920 Py_DECREF(v); 2150 Py_DECREF(v);
1921 break; 2151 break;
1922 2152 }
1923 TARGET(LOAD_BUILD_CLASS) 2153
1924 x = PyDict_GetItemString(f->f_builtins, 2154 TARGET_NOARG(BUILD_CLASS)
1925 "__build_class__"); 2155 {
1926 if (x == NULL) { 2156 u = TOP();
1927 PyErr_SetString(PyExc_ImportError, 2157 v = SECOND();
1928 "__build_class__ not found"); 2158 w = THIRD();
1929 break; 2159 STACKADJ(-2);
1930 } 2160 x = build_class(u, v, w);
1931 Py_INCREF(x); 2161 SET_TOP(x);
1932 PUSH(x); 2162 Py_DECREF(u);
1933 break; 2163 Py_DECREF(v);
2164 Py_DECREF(w);
2165 break;
2166 }
1934 2167
1935 TARGET(STORE_NAME) 2168 TARGET(STORE_NAME)
2169 {
1936 w = GETITEM(names, oparg); 2170 w = GETITEM(names, oparg);
1937 v = POP(); 2171 v = POP();
1938 if ((x = f->f_locals) != NULL) { 2172 if ((x = f->f_locals) != NULL) {
1939 if (PyDict_CheckExact(x)) 2173 if (PyDict_CheckExact(x))
1940 err = PyDict_SetItem(x, w, v); 2174 err = PyDict_SetItem(x, w, v);
1941 else 2175 else
1942 err = PyObject_SetItem(x, w, v); 2176 err = PyObject_SetItem(x, w, v);
1943 Py_DECREF(v); 2177 Py_DECREF(v);
1944 if (err == 0) DISPATCH(); 2178 if (err == 0) DISPATCH();
1945 break; 2179 break;
1946 } 2180 }
2181 t = PyObject_Repr(w);
2182 if (t == NULL)
2183 break;
1947 PyErr_Format(PyExc_SystemError, 2184 PyErr_Format(PyExc_SystemError,
1948 "no locals found when storing %R", w); 2185 "no locals found when storing %s",
1949 break; 2186 PyString_AS_STRING(t));
2187 Py_DECREF(t);
2188 break;
2189 }
1950 2190
1951 TARGET(DELETE_NAME) 2191 TARGET(DELETE_NAME)
2192 {
1952 w = GETITEM(names, oparg); 2193 w = GETITEM(names, oparg);
1953 if ((x = f->f_locals) != NULL) { 2194 if ((x = f->f_locals) != NULL) {
1954 if ((err = PyObject_DelItem(x, w)) != 0) 2195 if ((err = PyObject_DelItem(x, w)) != 0)
1955 format_exc_check_arg(PyExc_NameError, 2196 format_exc_check_arg(PyExc_NameError,
1956 NAME_ERROR_MSG, 2197 NAME_ERROR_MSG,
1957 w); 2198 w);
1958 break; 2199 break;
1959 } 2200 }
2201 t = PyObject_Repr(w);
2202 if (t == NULL)
2203 break;
1960 PyErr_Format(PyExc_SystemError, 2204 PyErr_Format(PyExc_SystemError,
1961 "no locals when deleting %R", w); 2205 "no locals when deleting %s",
1962 break; 2206 PyString_AS_STRING(w));
2207 Py_DECREF(t);
2208 break;
2209 }
1963 2210
1964 PREDICTED_WITH_ARG(UNPACK_SEQUENCE); 2211 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1965 TARGET(UNPACK_SEQUENCE) 2212 TARGET(UNPACK_SEQUENCE)
2213 {
1966 v = POP(); 2214 v = POP();
1967 if (PyTuple_CheckExact(v) && 2215 if (PyTuple_CheckExact(v) &&
1968 PyTuple_GET_SIZE(v) == oparg) { 2216 PyTuple_GET_SIZE(v) == oparg) {
1969 PyObject **items = \ 2217 PyObject **items = \
1970 ((PyTupleObject *)v)->ob_item; 2218 ((PyTupleObject *)v)->ob_item;
1971 while (oparg--) { 2219 while (oparg--) {
1972 w = items[oparg]; 2220 w = items[oparg];
1973 Py_INCREF(w); 2221 Py_INCREF(w);
1974 PUSH(w); 2222 PUSH(w);
1975 } 2223 }
1976 Py_DECREF(v); 2224 Py_DECREF(v);
1977 DISPATCH(); 2225 DISPATCH();
1978 } else if (PyList_CheckExact(v) && 2226 } else if (PyList_CheckExact(v) &&
1979 PyList_GET_SIZE(v) == oparg) { 2227 PyList_GET_SIZE(v) == oparg) {
1980 PyObject **items = \ 2228 PyObject **items = \
1981 ((PyListObject *)v)->ob_item; 2229 ((PyListObject *)v)->ob_item;
1982 while (oparg--) { 2230 while (oparg--) {
1983 w = items[oparg]; 2231 w = items[oparg];
1984 Py_INCREF(w); 2232 Py_INCREF(w);
1985 PUSH(w); 2233 PUSH(w);
1986 } 2234 }
1987 } else if (unpack_iterable(v, oparg, -1, 2235 } else if (unpack_iterable(v, oparg,
1988 stack_pointer + oparg)) { 2236 stack_pointer + oparg)) {
1989 STACKADJ(oparg); 2237 STACKADJ(oparg);
1990 } else { 2238 } else {
1991 /* unpack_iterable() raised an exception */ 2239 /* unpack_iterable() raised an exception */
1992 why = WHY_EXCEPTION; 2240 why = WHY_EXCEPTION;
1993 } 2241 }
1994 Py_DECREF(v); 2242 Py_DECREF(v);
1995 break; 2243 break;
1996 2244 }
1997 TARGET(UNPACK_EX) 2245
1998 {
1999 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2000 v = POP();
2001
2002 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
2003 stack_pointer + totalargs)) {
2004 stack_pointer += totalargs;
2005 } else {
2006 why = WHY_EXCEPTION;
2007 }
2008 Py_DECREF(v);
2009 break;
2010 }
2011 2246
2012 TARGET(STORE_ATTR) 2247 TARGET(STORE_ATTR)
2248 {
2013 w = GETITEM(names, oparg); 2249 w = GETITEM(names, oparg);
2014 v = TOP(); 2250 v = TOP();
2015 u = SECOND(); 2251 u = SECOND();
2016 STACKADJ(-2); 2252 STACKADJ(-2);
2017 err = PyObject_SetAttr(v, w, u); /* v.w = u */ 2253 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2018 Py_DECREF(v); 2254 Py_DECREF(v);
2019 Py_DECREF(u); 2255 Py_DECREF(u);
2020 if (err == 0) DISPATCH(); 2256 if (err == 0) DISPATCH();
2021 break; 2257 break;
2258 }
2022 2259
2023 TARGET(DELETE_ATTR) 2260 TARGET(DELETE_ATTR)
2261 {
2024 w = GETITEM(names, oparg); 2262 w = GETITEM(names, oparg);
2025 v = POP(); 2263 v = POP();
2026 err = PyObject_SetAttr(v, w, (PyObject *)NULL); 2264 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2027 /* del v.w */ 2265 /* del v.w */
2028 Py_DECREF(v); 2266 Py_DECREF(v);
2029 break; 2267 break;
2268 }
2269
2030 2270
2031 TARGET(STORE_GLOBAL) 2271 TARGET(STORE_GLOBAL)
2272 {
2032 w = GETITEM(names, oparg); 2273 w = GETITEM(names, oparg);
2033 v = POP(); 2274 v = POP();
2034 err = PyDict_SetItem(f->f_globals, w, v); 2275 err = PyDict_SetItem(f->f_globals, w, v);
2035 Py_DECREF(v); 2276 Py_DECREF(v);
2036 if (err == 0) DISPATCH(); 2277 if (err == 0) DISPATCH();
2037 break; 2278 break;
2279 }
2038 2280
2039 TARGET(DELETE_GLOBAL) 2281 TARGET(DELETE_GLOBAL)
2282 {
2040 w = GETITEM(names, oparg); 2283 w = GETITEM(names, oparg);
2041 if ((err = PyDict_DelItem(f->f_globals, w)) != 0) 2284 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2042 format_exc_check_arg( 2285 format_exc_check_arg(
2043 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w); 2286 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2044 break; 2287 break;
2288 }
2045 2289
2046 TARGET(LOAD_NAME) 2290 TARGET(LOAD_NAME)
2291 {
2047 w = GETITEM(names, oparg); 2292 w = GETITEM(names, oparg);
2048 if ((v = f->f_locals) == NULL) { 2293 if ((v = f->f_locals) == NULL) {
2294 why = WHY_EXCEPTION;
2295 t = PyObject_Repr(w);
2296 if (t == NULL)
2297 break;
2049 PyErr_Format(PyExc_SystemError, 2298 PyErr_Format(PyExc_SystemError,
2050 "no locals when loading %R", w); 2299 "no locals when loading %s",
2051 why = WHY_EXCEPTION; 2300 PyString_AS_STRING(w));
2301 Py_DECREF(t);
2052 break; 2302 break;
2053 } 2303 }
2054 if (PyDict_CheckExact(v)) { 2304 if (PyDict_CheckExact(v)) {
2055 x = PyDict_GetItem(v, w); 2305 x = PyDict_GetItem(v, w);
2056 Py_XINCREF(x); 2306 Py_XINCREF(x);
2057 } 2307 }
2058 else { 2308 else {
2059 x = PyObject_GetItem(v, w); 2309 x = PyObject_GetItem(v, w);
2060 if (x == NULL && PyErr_Occurred()) { 2310 if (x == NULL && PyErr_Occurred()) {
2061 if (!PyErr_ExceptionMatches( 2311 if (!PyErr_ExceptionMatches(
(...skipping 10 matching lines...) Expand all
2072 format_exc_check_arg( 2322 format_exc_check_arg(
2073 PyExc_NameError, 2323 PyExc_NameError,
2074 NAME_ERROR_MSG, w); 2324 NAME_ERROR_MSG, w);
2075 break; 2325 break;
2076 } 2326 }
2077 } 2327 }
2078 Py_INCREF(x); 2328 Py_INCREF(x);
2079 } 2329 }
2080 PUSH(x); 2330 PUSH(x);
2081 DISPATCH(); 2331 DISPATCH();
2332 }
2082 2333
2083 TARGET(LOAD_GLOBAL) 2334 TARGET(LOAD_GLOBAL)
2335 {
2084 w = GETITEM(names, oparg); 2336 w = GETITEM(names, oparg);
2085 if (PyUnicode_CheckExact(w)) { 2337 if (PyString_CheckExact(w)) {
2086 /* Inline the PyDict_GetItem() calls. 2338 /* Inline the PyDict_GetItem() calls.
2087 WARNING: this is an extreme speed hack. 2339 WARNING: this is an extreme speed hack.
2088 Do not try this at home. */ 2340 Do not try this at home. */
2089 Py_hash_t hash = ((PyUnicodeObject *)w)->hash; 2341 long hash = ((PyStringObject *)w)->ob_shash;
2090 if (hash != -1) { 2342 if (hash != -1) {
2091 PyDictObject *d; 2343 PyDictObject *d;
2092 PyDictEntry *e; 2344 PyDictEntry *e;
2093 d = (PyDictObject *)(f->f_globals); 2345 d = (PyDictObject *)(f->f_globals);
2094 e = d->ma_lookup(d, w, hash); 2346 e = d->ma_lookup(d, w, hash);
2095 if (e == NULL) { 2347 if (e == NULL) {
2096 x = NULL; 2348 x = NULL;
2097 break; 2349 break;
2098 } 2350 }
2099 x = e->me_value; 2351 x = e->me_value;
(...skipping 25 matching lines...) Expand all
2125 load_global_error: 2377 load_global_error:
2126 format_exc_check_arg( 2378 format_exc_check_arg(
2127 PyExc_NameError, 2379 PyExc_NameError,
2128 GLOBAL_NAME_ERROR_MSG, w); 2380 GLOBAL_NAME_ERROR_MSG, w);
2129 break; 2381 break;
2130 } 2382 }
2131 } 2383 }
2132 Py_INCREF(x); 2384 Py_INCREF(x);
2133 PUSH(x); 2385 PUSH(x);
2134 DISPATCH(); 2386 DISPATCH();
2387 }
2135 2388
2136 TARGET(DELETE_FAST) 2389 TARGET(DELETE_FAST)
2390 {
2137 x = GETLOCAL(oparg); 2391 x = GETLOCAL(oparg);
2138 if (x != NULL) { 2392 if (x != NULL) {
2139 SETLOCAL(oparg, NULL); 2393 SETLOCAL(oparg, NULL);
2140 DISPATCH(); 2394 DISPATCH();
2141 } 2395 }
2142 format_exc_check_arg( 2396 format_exc_check_arg(
2143 PyExc_UnboundLocalError, 2397 PyExc_UnboundLocalError,
2144 UNBOUNDLOCAL_ERROR_MSG, 2398 UNBOUNDLOCAL_ERROR_MSG,
2145 PyTuple_GetItem(co->co_varnames, oparg) 2399 PyTuple_GetItem(co->co_varnames, oparg)
2146 ); 2400 );
2147 break; 2401 break;
2148 2402 }
2149 TARGET(DELETE_DEREF)
2150 x = freevars[oparg];
2151 if (PyCell_GET(x) != NULL) {
2152 PyCell_Set(x, NULL);
2153 DISPATCH();
2154 }
2155 err = -1;
2156 format_exc_unbound(co, oparg);
2157 break;
2158 2403
2159 TARGET(LOAD_CLOSURE) 2404 TARGET(LOAD_CLOSURE)
2405 {
2160 x = freevars[oparg]; 2406 x = freevars[oparg];
2161 Py_INCREF(x); 2407 Py_INCREF(x);
2162 PUSH(x); 2408 PUSH(x);
2163 if (x != NULL) DISPATCH(); 2409 if (x != NULL) DISPATCH();
2164 break; 2410 break;
2411 }
2165 2412
2166 TARGET(LOAD_DEREF) 2413 TARGET(LOAD_DEREF)
2414 {
2167 x = freevars[oparg]; 2415 x = freevars[oparg];
2168 w = PyCell_Get(x); 2416 w = PyCell_Get(x);
2169 if (w != NULL) { 2417 if (w != NULL) {
2170 PUSH(w); 2418 PUSH(w);
2171 DISPATCH(); 2419 DISPATCH();
2172 } 2420 }
2173 err = -1; 2421 err = -1;
2174 format_exc_unbound(co, oparg); 2422 /* Don't stomp existing exception */
2175 break; 2423 if (PyErr_Occurred())
2424 break;
2425 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2426 v = PyTuple_GET_ITEM(co->co_cellvars,
2427 oparg);
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 }
2176 2440
2177 TARGET(STORE_DEREF) 2441 TARGET(STORE_DEREF)
2442 {
2178 w = POP(); 2443 w = POP();
2179 x = freevars[oparg]; 2444 x = freevars[oparg];
2180 PyCell_Set(x, w); 2445 PyCell_Set(x, w);
2181 Py_DECREF(w); 2446 Py_DECREF(w);
2182 DISPATCH(); 2447 DISPATCH();
2448 }
2183 2449
2184 TARGET(BUILD_TUPLE) 2450 TARGET(BUILD_TUPLE)
2451 {
2185 x = PyTuple_New(oparg); 2452 x = PyTuple_New(oparg);
2186 if (x != NULL) { 2453 if (x != NULL) {
2187 for (; --oparg >= 0;) { 2454 for (; --oparg >= 0;) {
2188 w = POP(); 2455 w = POP();
2189 PyTuple_SET_ITEM(x, oparg, w); 2456 PyTuple_SET_ITEM(x, oparg, w);
2190 } 2457 }
2191 PUSH(x); 2458 PUSH(x);
2192 DISPATCH(); 2459 DISPATCH();
2193 } 2460 }
2194 break; 2461 break;
2462 }
2195 2463
2196 TARGET(BUILD_LIST) 2464 TARGET(BUILD_LIST)
2465 {
2197 x = PyList_New(oparg); 2466 x = PyList_New(oparg);
2198 if (x != NULL) { 2467 if (x != NULL) {
2199 for (; --oparg >= 0;) { 2468 for (; --oparg >= 0;) {
2200 w = POP(); 2469 w = POP();
2201 PyList_SET_ITEM(x, oparg, w); 2470 PyList_SET_ITEM(x, oparg, w);
2202 } 2471 }
2203 PUSH(x); 2472 PUSH(x);
2204 DISPATCH(); 2473 DISPATCH();
2205 } 2474 }
2206 break; 2475 break;
2476 }
2207 2477
2208 TARGET(BUILD_SET) 2478 TARGET(BUILD_SET)
2479 {
2209 x = PySet_New(NULL); 2480 x = PySet_New(NULL);
2210 if (x != NULL) { 2481 if (x != NULL) {
2211 for (; --oparg >= 0;) { 2482 for (; --oparg >= 0;) {
2212 w = POP(); 2483 w = POP();
2213 if (err == 0) 2484 if (err == 0)
2214 err = PySet_Add(x, w); 2485 err = PySet_Add(x, w);
2215 Py_DECREF(w); 2486 Py_DECREF(w);
2216 } 2487 }
2217 if (err != 0) { 2488 if (err != 0) {
2218 Py_DECREF(x); 2489 Py_DECREF(x);
2219 break; 2490 break;
2220 } 2491 }
2221 PUSH(x); 2492 PUSH(x);
2222 DISPATCH(); 2493 DISPATCH();
2223 } 2494 }
2224 break; 2495 break;
2496 }
2225 2497
2226 TARGET(BUILD_MAP) 2498 TARGET(BUILD_MAP)
2499 {
2227 x = _PyDict_NewPresized((Py_ssize_t)oparg); 2500 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2228 PUSH(x); 2501 PUSH(x);
2229 if (x != NULL) DISPATCH(); 2502 if (x != NULL) DISPATCH();
2230 break; 2503 break;
2231 2504 }
2232 TARGET(STORE_MAP) 2505
2506 TARGET_NOARG(STORE_MAP)
2507 {
2233 w = TOP(); /* key */ 2508 w = TOP(); /* key */
2234 u = SECOND(); /* value */ 2509 u = SECOND(); /* value */
2235 v = THIRD(); /* dict */ 2510 v = THIRD(); /* dict */
2236 STACKADJ(-2); 2511 STACKADJ(-2);
2237 assert (PyDict_CheckExact(v)); 2512 assert (PyDict_CheckExact(v));
2238 err = PyDict_SetItem(v, w, u); /* v[w] = u */ 2513 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2239 Py_DECREF(u); 2514 Py_DECREF(u);
2240 Py_DECREF(w); 2515 Py_DECREF(w);
2241 if (err == 0) DISPATCH(); 2516 if (err == 0) DISPATCH();
2242 break; 2517 break;
2518 }
2243 2519
2244 TARGET(MAP_ADD) 2520 TARGET(MAP_ADD)
2521 {
2245 w = TOP(); /* key */ 2522 w = TOP(); /* key */
2246 u = SECOND(); /* value */ 2523 u = SECOND(); /* value */
2247 STACKADJ(-2); 2524 STACKADJ(-2);
2248 v = stack_pointer[-oparg]; /* dict */ 2525 v = stack_pointer[-oparg]; /* dict */
2249 assert (PyDict_CheckExact(v)); 2526 assert (PyDict_CheckExact(v));
2250 err = PyDict_SetItem(v, w, u); /* v[w] = u */ 2527 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2251 Py_DECREF(u); 2528 Py_DECREF(u);
2252 Py_DECREF(w); 2529 Py_DECREF(w);
2253 if (err == 0) { 2530 if (err == 0) {
2254 PREDICT(JUMP_ABSOLUTE); 2531 PREDICT(JUMP_ABSOLUTE);
2255 DISPATCH(); 2532 DISPATCH();
2256 } 2533 }
2257 break; 2534 break;
2535 }
2258 2536
2259 TARGET(LOAD_ATTR) 2537 TARGET(LOAD_ATTR)
2538 {
2260 w = GETITEM(names, oparg); 2539 w = GETITEM(names, oparg);
2261 v = TOP(); 2540 v = TOP();
2262 x = PyObject_GetAttr(v, w); 2541 x = PyObject_GetAttr(v, w);
2263 Py_DECREF(v); 2542 Py_DECREF(v);
2264 SET_TOP(x); 2543 SET_TOP(x);
2265 if (x != NULL) DISPATCH(); 2544 if (x != NULL) DISPATCH();
2266 break; 2545 break;
2546 }
2267 2547
2268 TARGET(COMPARE_OP) 2548 TARGET(COMPARE_OP)
2549 {
2269 w = POP(); 2550 w = POP();
2270 v = TOP(); 2551 v = TOP();
2271 x = cmp_outcome(oparg, v, w); 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);
2571 }
2572 else {
2573 slow_compare:
2574 x = cmp_outcome(oparg, v, w);
2575 }
2272 Py_DECREF(v); 2576 Py_DECREF(v);
2273 Py_DECREF(w); 2577 Py_DECREF(w);
2274 SET_TOP(x); 2578 SET_TOP(x);
2275 if (x == NULL) break; 2579 if (x == NULL) break;
2276 PREDICT(POP_JUMP_IF_FALSE); 2580 PREDICT(POP_JUMP_IF_FALSE);
2277 PREDICT(POP_JUMP_IF_TRUE); 2581 PREDICT(POP_JUMP_IF_TRUE);
2278 DISPATCH(); 2582 DISPATCH();
2583 }
2279 2584
2280 TARGET(IMPORT_NAME) 2585 TARGET(IMPORT_NAME)
2586 {
2281 w = GETITEM(names, oparg); 2587 w = GETITEM(names, oparg);
2282 x = PyDict_GetItemString(f->f_builtins, "__import__"); 2588 x = PyDict_GetItemString(f->f_builtins, "__import__");
2283 if (x == NULL) { 2589 if (x == NULL) {
2284 PyErr_SetString(PyExc_ImportError, 2590 PyErr_SetString(PyExc_ImportError,
2285 "__import__ not found"); 2591 "__import__ not found");
2286 break; 2592 break;
2287 } 2593 }
2288 Py_INCREF(x); 2594 Py_INCREF(x);
2289 v = POP(); 2595 v = POP();
2290 u = TOP(); 2596 u = TOP();
2291 if (PyLong_AsLong(u) != -1 || PyErr_Occurred()) 2597 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2292 w = PyTuple_Pack(5, 2598 w = PyTuple_Pack(5,
2293 w, 2599 w,
2294 f->f_globals, 2600 f->f_globals,
2295 f->f_locals == NULL ? 2601 f->f_locals == NULL ?
2296 Py_None : f->f_locals, 2602 Py_None : f->f_locals,
2297 v, 2603 v,
2298 u); 2604 u);
2299 else 2605 else
2300 w = PyTuple_Pack(4, 2606 w = PyTuple_Pack(4,
2301 w, 2607 w,
(...skipping 11 matching lines...) Expand all
2313 } 2619 }
2314 READ_TIMESTAMP(intr0); 2620 READ_TIMESTAMP(intr0);
2315 v = x; 2621 v = x;
2316 x = PyEval_CallObject(v, w); 2622 x = PyEval_CallObject(v, w);
2317 Py_DECREF(v); 2623 Py_DECREF(v);
2318 READ_TIMESTAMP(intr1); 2624 READ_TIMESTAMP(intr1);
2319 Py_DECREF(w); 2625 Py_DECREF(w);
2320 SET_TOP(x); 2626 SET_TOP(x);
2321 if (x != NULL) DISPATCH(); 2627 if (x != NULL) DISPATCH();
2322 break; 2628 break;
2323 2629 }
2324 TARGET(IMPORT_STAR) 2630
2631 TARGET_NOARG(IMPORT_STAR)
2632 {
2325 v = POP(); 2633 v = POP();
2326 PyFrame_FastToLocals(f); 2634 PyFrame_FastToLocals(f);
2327 if ((x = f->f_locals) == NULL) { 2635 if ((x = f->f_locals) == NULL) {
2328 PyErr_SetString(PyExc_SystemError, 2636 PyErr_SetString(PyExc_SystemError,
2329 "no locals found during 'import *'"); 2637 "no locals found during 'import *'");
2330 break; 2638 break;
2331 } 2639 }
2332 READ_TIMESTAMP(intr0); 2640 READ_TIMESTAMP(intr0);
2333 err = import_all_from(x, v); 2641 err = import_all_from(x, v);
2334 READ_TIMESTAMP(intr1); 2642 READ_TIMESTAMP(intr1);
2335 PyFrame_LocalsToFast(f, 0); 2643 PyFrame_LocalsToFast(f, 0);
2336 Py_DECREF(v); 2644 Py_DECREF(v);
2337 if (err == 0) DISPATCH(); 2645 if (err == 0) DISPATCH();
2338 break; 2646 break;
2647 }
2339 2648
2340 TARGET(IMPORT_FROM) 2649 TARGET(IMPORT_FROM)
2650 {
2341 w = GETITEM(names, oparg); 2651 w = GETITEM(names, oparg);
2342 v = TOP(); 2652 v = TOP();
2343 READ_TIMESTAMP(intr0); 2653 READ_TIMESTAMP(intr0);
2344 x = import_from(v, w); 2654 x = import_from(v, w);
2345 READ_TIMESTAMP(intr1); 2655 READ_TIMESTAMP(intr1);
2346 PUSH(x); 2656 PUSH(x);
2347 if (x != NULL) DISPATCH(); 2657 if (x != NULL) DISPATCH();
2348 break; 2658 break;
2659 }
2349 2660
2350 TARGET(JUMP_FORWARD) 2661 TARGET(JUMP_FORWARD)
2662 {
2351 JUMPBY(oparg); 2663 JUMPBY(oparg);
2352 FAST_DISPATCH(); 2664 FAST_DISPATCH();
2665 }
2353 2666
2354 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE); 2667 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2355 TARGET(POP_JUMP_IF_FALSE) 2668 TARGET(POP_JUMP_IF_FALSE)
2669 {
2356 w = POP(); 2670 w = POP();
2357 if (w == Py_True) { 2671 if (w == Py_True) {
2358 Py_DECREF(w); 2672 Py_DECREF(w);
2359 FAST_DISPATCH(); 2673 FAST_DISPATCH();
2360 } 2674 }
2361 if (w == Py_False) { 2675 if (w == Py_False) {
2362 Py_DECREF(w); 2676 Py_DECREF(w);
2363 JUMPTO(oparg); 2677 JUMPTO(oparg);
2364 FAST_DISPATCH(); 2678 FAST_DISPATCH();
2365 } 2679 }
2366 err = PyObject_IsTrue(w); 2680 err = PyObject_IsTrue(w);
2367 Py_DECREF(w); 2681 Py_DECREF(w);
2368 if (err > 0) 2682 if (err > 0)
2369 err = 0; 2683 err = 0;
2370 else if (err == 0) 2684 else if (err == 0)
2371 JUMPTO(oparg); 2685 JUMPTO(oparg);
2372 else 2686 else
2373 break; 2687 break;
2374 DISPATCH(); 2688 DISPATCH();
2689 }
2375 2690
2376 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE); 2691 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2377 TARGET(POP_JUMP_IF_TRUE) 2692 TARGET(POP_JUMP_IF_TRUE)
2693 {
2378 w = POP(); 2694 w = POP();
2379 if (w == Py_False) { 2695 if (w == Py_False) {
2380 Py_DECREF(w); 2696 Py_DECREF(w);
2381 FAST_DISPATCH(); 2697 FAST_DISPATCH();
2382 } 2698 }
2383 if (w == Py_True) { 2699 if (w == Py_True) {
2384 Py_DECREF(w); 2700 Py_DECREF(w);
2385 JUMPTO(oparg); 2701 JUMPTO(oparg);
2386 FAST_DISPATCH(); 2702 FAST_DISPATCH();
2387 } 2703 }
2388 err = PyObject_IsTrue(w); 2704 err = PyObject_IsTrue(w);
2389 Py_DECREF(w); 2705 Py_DECREF(w);
2390 if (err > 0) { 2706 if (err > 0) {
2391 err = 0; 2707 err = 0;
2392 JUMPTO(oparg); 2708 JUMPTO(oparg);
2393 } 2709 }
2394 else if (err == 0) 2710 else if (err == 0)
2395 ; 2711 ;
2396 else 2712 else
2397 break; 2713 break;
2398 DISPATCH(); 2714 DISPATCH();
2715 }
2399 2716
2400 TARGET(JUMP_IF_FALSE_OR_POP) 2717 TARGET(JUMP_IF_FALSE_OR_POP)
2718 {
2401 w = TOP(); 2719 w = TOP();
2402 if (w == Py_True) { 2720 if (w == Py_True) {
2403 STACKADJ(-1); 2721 STACKADJ(-1);
2404 Py_DECREF(w); 2722 Py_DECREF(w);
2405 FAST_DISPATCH(); 2723 FAST_DISPATCH();
2406 } 2724 }
2407 if (w == Py_False) { 2725 if (w == Py_False) {
2408 JUMPTO(oparg); 2726 JUMPTO(oparg);
2409 FAST_DISPATCH(); 2727 FAST_DISPATCH();
2410 } 2728 }
2411 err = PyObject_IsTrue(w); 2729 err = PyObject_IsTrue(w);
2412 if (err > 0) { 2730 if (err > 0) {
2413 STACKADJ(-1); 2731 STACKADJ(-1);
2414 Py_DECREF(w); 2732 Py_DECREF(w);
2415 err = 0; 2733 err = 0;
2416 } 2734 }
2417 else if (err == 0) 2735 else if (err == 0)
2418 JUMPTO(oparg); 2736 JUMPTO(oparg);
2419 else 2737 else
2420 break; 2738 break;
2421 DISPATCH(); 2739 DISPATCH();
2740 }
2422 2741
2423 TARGET(JUMP_IF_TRUE_OR_POP) 2742 TARGET(JUMP_IF_TRUE_OR_POP)
2743 {
2424 w = TOP(); 2744 w = TOP();
2425 if (w == Py_False) { 2745 if (w == Py_False) {
2426 STACKADJ(-1); 2746 STACKADJ(-1);
2427 Py_DECREF(w); 2747 Py_DECREF(w);
2428 FAST_DISPATCH(); 2748 FAST_DISPATCH();
2429 } 2749 }
2430 if (w == Py_True) { 2750 if (w == Py_True) {
2431 JUMPTO(oparg); 2751 JUMPTO(oparg);
2432 FAST_DISPATCH(); 2752 FAST_DISPATCH();
2433 } 2753 }
2434 err = PyObject_IsTrue(w); 2754 err = PyObject_IsTrue(w);
2435 if (err > 0) { 2755 if (err > 0) {
2436 err = 0; 2756 err = 0;
2437 JUMPTO(oparg); 2757 JUMPTO(oparg);
2438 } 2758 }
2439 else if (err == 0) { 2759 else if (err == 0) {
2440 STACKADJ(-1); 2760 STACKADJ(-1);
2441 Py_DECREF(w); 2761 Py_DECREF(w);
2442 } 2762 }
2443 else 2763 else
2444 break; 2764 break;
2445 DISPATCH(); 2765 DISPATCH();
2766 }
2446 2767
2447 PREDICTED_WITH_ARG(JUMP_ABSOLUTE); 2768 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2448 TARGET(JUMP_ABSOLUTE) 2769 TARGET(JUMP_ABSOLUTE)
2770 {
2449 JUMPTO(oparg); 2771 JUMPTO(oparg);
2450 #if FAST_LOOPS 2772 #if FAST_LOOPS
2451 /* 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
2452 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
2453 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
2454 "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
2455 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
2456 that contain only instructions ending with FAST_DISPATCH). 2778 that contain only instructions ending with goto fast_next_opcode) .
2457 */ 2779 */
2458 FAST_DISPATCH(); 2780 goto fast_next_opcode;
2459 #else 2781 #else
2460 DISPATCH(); 2782 DISPATCH();
2461 #endif 2783 #endif
2462 2784 }
2463 TARGET(GET_ITER) 2785
2786 TARGET_NOARG(GET_ITER)
2787 {
2464 /* before: [obj]; after [getiter(obj)] */ 2788 /* before: [obj]; after [getiter(obj)] */
2465 v = TOP(); 2789 v = TOP();
2466 x = PyObject_GetIter(v); 2790 x = PyObject_GetIter(v);
2467 Py_DECREF(v); 2791 Py_DECREF(v);
2468 if (x != NULL) { 2792 if (x != NULL) {
2469 SET_TOP(x); 2793 SET_TOP(x);
2470 PREDICT(FOR_ITER); 2794 PREDICT(FOR_ITER);
2471 DISPATCH(); 2795 DISPATCH();
2472 } 2796 }
2473 STACKADJ(-1); 2797 STACKADJ(-1);
2474 break; 2798 break;
2799 }
2475 2800
2476 PREDICTED_WITH_ARG(FOR_ITER); 2801 PREDICTED_WITH_ARG(FOR_ITER);
2477 TARGET(FOR_ITER) 2802 TARGET(FOR_ITER)
2803 {
2478 /* before: [iter]; after: [iter, iter()] *or* [] */ 2804 /* before: [iter]; after: [iter, iter()] *or* [] */
2479 v = TOP(); 2805 v = TOP();
2480 x = (*v->ob_type->tp_iternext)(v); 2806 x = (*v->ob_type->tp_iternext)(v);
2481 if (x != NULL) { 2807 if (x != NULL) {
2482 PUSH(x); 2808 PUSH(x);
2483 PREDICT(STORE_FAST); 2809 PREDICT(STORE_FAST);
2484 PREDICT(UNPACK_SEQUENCE); 2810 PREDICT(UNPACK_SEQUENCE);
2485 DISPATCH(); 2811 DISPATCH();
2486 } 2812 }
2487 if (PyErr_Occurred()) { 2813 if (PyErr_Occurred()) {
2488 if (!PyErr_ExceptionMatches( 2814 if (!PyErr_ExceptionMatches(
2489 PyExc_StopIteration)) 2815 PyExc_StopIteration))
2490 break; 2816 break;
2491 PyErr_Clear(); 2817 PyErr_Clear();
2492 } 2818 }
2493 /* iterator ended normally */ 2819 /* iterator ended normally */
2494 x = v = POP(); 2820 x = v = POP();
2495 Py_DECREF(v); 2821 Py_DECREF(v);
2496 JUMPBY(oparg); 2822 JUMPBY(oparg);
2497 DISPATCH(); 2823 DISPATCH();
2498 2824 }
2499 TARGET(BREAK_LOOP) 2825
2826 TARGET_NOARG(BREAK_LOOP)
2827 {
2500 why = WHY_BREAK; 2828 why = WHY_BREAK;
2501 goto fast_block_end; 2829 goto fast_block_end;
2830 }
2502 2831
2503 TARGET(CONTINUE_LOOP) 2832 TARGET(CONTINUE_LOOP)
2504 retval = PyLong_FromLong(oparg); 2833 {
2834 retval = PyInt_FromLong(oparg);
2505 if (!retval) { 2835 if (!retval) {
2506 x = NULL; 2836 x = NULL;
2507 break; 2837 break;
2508 } 2838 }
2509 why = WHY_CONTINUE; 2839 why = WHY_CONTINUE;
2510 goto fast_block_end; 2840 goto fast_block_end;
2841 }
2511 2842
2512 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally) 2843 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2513 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally) 2844 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2514 TARGET(SETUP_FINALLY) 2845 TARGET(SETUP_FINALLY)
2515 _setup_finally: 2846 _setup_finally:
2847 {
2516 /* NOTE: If you add any new block-setup opcodes that 2848 /* NOTE: If you add any new block-setup opcodes that
2517 are not try/except/finally handlers, you may need 2849 are not try/except/finally handlers, you may need
2518 to update the PyGen_NeedsFinalizing() function. 2850 to update the PyGen_NeedsFinalizing() function.
2519 */ 2851 */
2520 2852
2521 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, 2853 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2522 STACK_LEVEL()); 2854 STACK_LEVEL());
2523 DISPATCH(); 2855 DISPATCH();
2856 }
2857
2858
2524 2859
2525 TARGET(SETUP_WITH) 2860 TARGET(SETUP_WITH)
2861 {
2526 { 2862 {
2527 static PyObject *exit, *enter; 2863 static PyObject *exit, *enter;
2528 w = TOP(); 2864 w = TOP();
2529 x = special_lookup(w, "__exit__", &exit); 2865 x = special_lookup(w, "__exit__", &exit);
2530 if (!x) 2866 if (!x)
2531 break; 2867 break;
2532 SET_TOP(x); 2868 SET_TOP(x);
2533 u = special_lookup(w, "__enter__", &enter); 2869 u = special_lookup(w, "__enter__", &enter);
2534 Py_DECREF(w); 2870 Py_DECREF(w);
2535 if (!u) { 2871 if (!u) {
2536 x = NULL; 2872 x = NULL;
2537 break; 2873 break;
2538 } 2874 }
2539 x = PyObject_CallFunctionObjArgs(u, NULL); 2875 x = PyObject_CallFunctionObjArgs(u, NULL);
2540 Py_DECREF(u); 2876 Py_DECREF(u);
2541 if (!x) 2877 if (!x)
2542 break; 2878 break;
2543 /* Setup the finally block before pushing the result 2879 /* Setup a finally block (SETUP_WITH as a block is
2544 of __enter__ on the stack. */ 2880 equivalent to SETUP_FINALLY except it normalizes
2545 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, 2881 the exception) before pushing the result of
2882 __enter__ on the stack. */
2883 PyFrame_BlockSetup(f, SETUP_WITH, INSTR_OFFSET() + oparg,
2546 STACK_LEVEL()); 2884 STACK_LEVEL());
2547 2885
2548 PUSH(x); 2886 PUSH(x);
2549 DISPATCH(); 2887 DISPATCH();
2550 } 2888 }
2551 2889 }
2552 TARGET(WITH_CLEANUP) 2890
2891 TARGET_NOARG(WITH_CLEANUP)
2553 { 2892 {
2554 /* At the top of the stack are 1-3 values indicating 2893 /* At the top of the stack are 1-3 values indicating
2555 how/why we entered the finally clause: 2894 how/why we entered the finally clause:
2556 - TOP = None 2895 - TOP = None
2557 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval 2896 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2558 - TOP = WHY_*; no retval below it 2897 - TOP = WHY_*; no retval below it
2559 - (TOP, SECOND, THIRD) = exc_info() 2898 - (TOP, SECOND, THIRD) = exc_info()
2560 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2561 Below them is EXIT, the context.__exit__ bound method. 2899 Below them is EXIT, the context.__exit__ bound method.
2562 In the last case, we must call 2900 In the last case, we must call
2563 EXIT(TOP, SECOND, THIRD) 2901 EXIT(TOP, SECOND, THIRD)
2564 otherwise we must call 2902 otherwise we must call
2565 EXIT(None, None, None) 2903 EXIT(None, None, None)
2566 2904
2567 In the first two cases, we remove EXIT from the 2905 In all cases, we remove EXIT from the stack, leaving
2568 stack, leaving the rest in the same order. In the 2906 the rest in the same order.
2569 third case, we shift the bottom 3 values of the
2570 stack down, and replace the empty spot with NULL.
2571 2907
2572 In addition, if the stack represents an exception, 2908 In addition, if the stack represents an exception,
2573 *and* the function call returns a 'true' value, we 2909 *and* the function call returns a 'true' value, we
2574 push WHY_SILENCED onto the stack. END_FINALLY will 2910 "zap" this information, to prevent END_FINALLY from
2575 then not re-raise the exception. (But non-local 2911 re-raising the exception. (But non-local gotos
2576 gotos should still be resumed.) 2912 should still be resumed.)
2577 */ 2913 */
2578 2914
2579 PyObject *exit_func; 2915 PyObject *exit_func;
2580 u = TOP(); 2916
2917 u = POP();
2581 if (u == Py_None) { 2918 if (u == Py_None) {
2582 (void)POP();
2583 exit_func = TOP(); 2919 exit_func = TOP();
2584 SET_TOP(u); 2920 SET_TOP(u);
2585 v = w = Py_None; 2921 v = w = Py_None;
2586 } 2922 }
2587 else if (PyLong_Check(u)) { 2923 else if (PyInt_Check(u)) {
2588 (void)POP(); 2924 switch(PyInt_AS_LONG(u)) {
2589 switch(PyLong_AsLong(u)) {
2590 case WHY_RETURN: 2925 case WHY_RETURN:
2591 case WHY_CONTINUE: 2926 case WHY_CONTINUE:
2592 /* Retval in TOP. */ 2927 /* Retval in TOP. */
2593 exit_func = SECOND(); 2928 exit_func = SECOND();
2594 SET_SECOND(TOP()); 2929 SET_SECOND(TOP());
2595 SET_TOP(u); 2930 SET_TOP(u);
2596 break; 2931 break;
2597 default: 2932 default:
2598 exit_func = TOP(); 2933 exit_func = TOP();
2599 SET_TOP(u); 2934 SET_TOP(u);
2600 break; 2935 break;
2601 } 2936 }
2602 u = v = w = Py_None; 2937 u = v = w = Py_None;
2603 } 2938 }
2604 else { 2939 else {
2605 PyObject *tp, *exc, *tb; 2940 v = TOP();
2606 PyTryBlock *block; 2941 w = SECOND();
2607 v = SECOND(); 2942 exit_func = THIRD();
2608 w = THIRD(); 2943 SET_TOP(u);
2609 tp = FOURTH(); 2944 SET_SECOND(v);
2610 exc = PEEK(5); 2945 SET_THIRD(w);
2611 tb = PEEK(6);
2612 exit_func = PEEK(7);
2613 SET_VALUE(7, tb);
2614 SET_VALUE(6, exc);
2615 SET_VALUE(5, tp);
2616 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2617 SET_FOURTH(NULL);
2618 /* We just shifted the stack down, so we have
2619 to tell the except handler block that the
2620 values are lower than it expects. */
2621 block = &f->f_blockstack[f->f_iblock - 1];
2622 assert(block->b_type == EXCEPT_HANDLER);
2623 block->b_level--;
2624 } 2946 }
2625 /* XXX Not the fastest way to call it... */ 2947 /* XXX Not the fastest way to call it... */
2626 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w, 2948 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2627 NULL); 2949 NULL);
2628 Py_DECREF(exit_func); 2950 Py_DECREF(exit_func);
2629 if (x == NULL) 2951 if (x == NULL)
2630 break; /* Go to error exit */ 2952 break; /* Go to error exit */
2631 2953
2632 if (u != Py_None) 2954 if (u != Py_None)
2633 err = PyObject_IsTrue(x); 2955 err = PyObject_IsTrue(x);
2634 else 2956 else
2635 err = 0; 2957 err = 0;
2636 Py_DECREF(x); 2958 Py_DECREF(x);
2637 2959
2638 if (err < 0) 2960 if (err < 0)
2639 break; /* Go to error exit */ 2961 break; /* Go to error exit */
2640 else if (err > 0) { 2962 else if (err > 0) {
2641 err = 0; 2963 err = 0;
2642 /* There was an exception and a True return */ 2964 /* There was an exception and a true return */
2643 PUSH(PyLong_FromLong((long) WHY_SILENCED)); 2965 STACKADJ(-2);
2966 Py_INCREF(Py_None);
2967 SET_TOP(Py_None);
2968 Py_DECREF(u);
2969 Py_DECREF(v);
2970 Py_DECREF(w);
2971 } else {
2972 /* The stack was rearranged to remove EXIT
2973 above. Let END_FINALLY do its thing */
2644 } 2974 }
2645 PREDICT(END_FINALLY); 2975 PREDICT(END_FINALLY);
2646 break; 2976 break;
2647 } 2977 }
2648 2978
2649 TARGET(CALL_FUNCTION) 2979 TARGET(CALL_FUNCTION)
2650 { 2980 {
2651 PyObject **sp; 2981 PyObject **sp;
2652 PCALL(PCALL_ALL); 2982 PCALL(PCALL_ALL);
2653 sp = stack_pointer; 2983 sp = stack_pointer;
2654 #ifdef WITH_TSC 2984 #ifdef WITH_TSC
2655 x = call_function(&sp, oparg, &intr0, &intr1); 2985 x = call_function(&sp, oparg, &intr0, &intr1);
2656 #else 2986 #else
2657 x = call_function(&sp, oparg); 2987 x = call_function(&sp, oparg);
2658 #endif 2988 #endif
2659 stack_pointer = sp; 2989 stack_pointer = sp;
2660 PUSH(x); 2990 PUSH(x);
2661 if (x != NULL) 2991 if (x != NULL) DISPATCH();
2662 DISPATCH();
2663 break; 2992 break;
2664 } 2993 }
2665 2994
2666 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw) 2995 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2667 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw) 2996 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2668 TARGET(CALL_FUNCTION_VAR_KW) 2997 TARGET(CALL_FUNCTION_VAR_KW)
2669 _call_function_var_kw: 2998 _call_function_var_kw:
2670 { 2999 {
2671 int na = oparg & 0xff; 3000 int na = oparg & 0xff;
2672 int nk = (oparg>>8) & 0xff; 3001 int nk = (oparg>>8) & 0xff;
(...skipping 10 matching lines...) Expand all
2683 3012
2684 if (PyMethod_Check(func) 3013 if (PyMethod_Check(func)
2685 && PyMethod_GET_SELF(func) != NULL) { 3014 && PyMethod_GET_SELF(func) != NULL) {
2686 PyObject *self = PyMethod_GET_SELF(func); 3015 PyObject *self = PyMethod_GET_SELF(func);
2687 Py_INCREF(self); 3016 Py_INCREF(self);
2688 func = PyMethod_GET_FUNCTION(func); 3017 func = PyMethod_GET_FUNCTION(func);
2689 Py_INCREF(func); 3018 Py_INCREF(func);
2690 Py_DECREF(*pfunc); 3019 Py_DECREF(*pfunc);
2691 *pfunc = self; 3020 *pfunc = self;
2692 na++; 3021 na++;
2693 /* n++; */
2694 } else 3022 } else
2695 Py_INCREF(func); 3023 Py_INCREF(func);
2696 sp = stack_pointer; 3024 sp = stack_pointer;
2697 READ_TIMESTAMP(intr0); 3025 READ_TIMESTAMP(intr0);
2698 x = ext_do_call(func, &sp, flags, na, nk); 3026 x = ext_do_call(func, &sp, flags, na, nk);
2699 READ_TIMESTAMP(intr1); 3027 READ_TIMESTAMP(intr1);
2700 stack_pointer = sp; 3028 stack_pointer = sp;
2701 Py_DECREF(func); 3029 Py_DECREF(func);
2702 3030
2703 while (stack_pointer > pfunc) { 3031 while (stack_pointer > pfunc) {
2704 w = POP(); 3032 w = POP();
2705 Py_DECREF(w); 3033 Py_DECREF(w);
2706 } 3034 }
2707 PUSH(x); 3035 PUSH(x);
2708 if (x != NULL) 3036 if (x != NULL) DISPATCH();
2709 DISPATCH(); 3037 break;
2710 break; 3038 }
2711 } 3039
2712 3040
2713 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2714 TARGET(MAKE_FUNCTION) 3041 TARGET(MAKE_FUNCTION)
2715 _make_function: 3042 {
2716 {
2717 int posdefaults = oparg & 0xff;
2718 int kwdefaults = (oparg>>8) & 0xff;
2719 int num_annotations = (oparg >> 16) & 0x7fff;
2720
2721 v = POP(); /* code object */ 3043 v = POP(); /* code object */
2722 x = PyFunction_New(v, f->f_globals); 3044 x = PyFunction_New(v, f->f_globals);
2723 Py_DECREF(v); 3045 Py_DECREF(v);
2724 3046 /* XXX Maybe this should be a separate opcode? */
2725 if (x != NULL && opcode == MAKE_CLOSURE) { 3047 if (x != NULL && oparg > 0) {
3048 v = PyTuple_New(oparg);
3049 if (v == NULL) {
3050 Py_DECREF(x);
3051 x = NULL;
3052 break;
3053 }
3054 while (--oparg >= 0) {
3055 w = POP();
3056 PyTuple_SET_ITEM(v, oparg, w);
3057 }
3058 err = PyFunction_SetDefaults(x, v);
3059 Py_DECREF(v);
3060 }
3061 PUSH(x);
3062 break;
3063 }
3064
3065 TARGET(MAKE_CLOSURE)
3066 {
3067 v = POP(); /* code object */
3068 x = PyFunction_New(v, f->f_globals);
3069 Py_DECREF(v);
3070 if (x != NULL) {
2726 v = POP(); 3071 v = POP();
2727 if (PyFunction_SetClosure(x, v) != 0) { 3072 if (PyFunction_SetClosure(x, v) != 0) {
2728 /* Can't happen unless bytecode is corrupt. */ 3073 /* Can't happen unless bytecode is corrupt. */
2729 why = WHY_EXCEPTION; 3074 why = WHY_EXCEPTION;
2730 } 3075 }
2731 Py_DECREF(v); 3076 Py_DECREF(v);
2732 } 3077 }
2733 3078 if (x != NULL && oparg > 0) {
2734 if (x != NULL && num_annotations > 0) { 3079 v = PyTuple_New(oparg);
2735 Py_ssize_t name_ix;
2736 u = POP(); /* names of args with annotations */
2737 v = PyDict_New();
2738 if (v == NULL) { 3080 if (v == NULL) {
2739 Py_DECREF(x); 3081 Py_DECREF(x);
2740 x = NULL; 3082 x = NULL;
2741 break; 3083 break;
2742 } 3084 }
2743 name_ix = PyTuple_Size(u); 3085 while (--oparg >= 0) {
2744 assert(num_annotations == name_ix+1);
2745 while (name_ix > 0) {
2746 --name_ix;
2747 t = PyTuple_GET_ITEM(u, name_ix);
2748 w = POP(); 3086 w = POP();
2749 /* XXX(nnorwitz): check for errors */ 3087 PyTuple_SET_ITEM(v, oparg, w);
2750 PyDict_SetItem(v, t, w);
2751 Py_DECREF(w);
2752 }
2753
2754 if (PyFunction_SetAnnotations(x, v) != 0) {
2755 /* Can't happen unless
2756 PyFunction_SetAnnotations changes. */
2757 why = WHY_EXCEPTION;
2758 }
2759 Py_DECREF(v);
2760 Py_DECREF(u);
2761 }
2762
2763 /* XXX Maybe this should be a separate opcode? */
2764 if (x != NULL && posdefaults > 0) {
2765 v = PyTuple_New(posdefaults);
2766 if (v == NULL) {
2767 Py_DECREF(x);
2768 x = NULL;
2769 break;
2770 }
2771 while (--posdefaults >= 0) {
2772 w = POP();
2773 PyTuple_SET_ITEM(v, posdefaults, w);
2774 } 3088 }
2775 if (PyFunction_SetDefaults(x, v) != 0) { 3089 if (PyFunction_SetDefaults(x, v) != 0) {
2776 /* Can't happen unless 3090 /* Can't happen unless
2777 PyFunction_SetDefaults changes. */ 3091 PyFunction_SetDefaults changes. */
2778 why = WHY_EXCEPTION; 3092 why = WHY_EXCEPTION;
2779 } 3093 }
2780 Py_DECREF(v); 3094 Py_DECREF(v);
2781 } 3095 }
2782 if (x != NULL && kwdefaults > 0) {
2783 v = PyDict_New();
2784 if (v == NULL) {
2785 Py_DECREF(x);
2786 x = NULL;
2787 break;
2788 }
2789 while (--kwdefaults >= 0) {
2790 w = POP(); /* default value */
2791 u = POP(); /* kw only arg name */
2792 /* XXX(nnorwitz): check for errors */
2793 PyDict_SetItem(v, u, w);
2794 Py_DECREF(w);
2795 Py_DECREF(u);
2796 }
2797 if (PyFunction_SetKwDefaults(x, v) != 0) {
2798 /* Can't happen unless
2799 PyFunction_SetKwDefaults changes. */
2800 why = WHY_EXCEPTION;
2801 }
2802 Py_DECREF(v);
2803 }
2804 PUSH(x); 3096 PUSH(x);
2805 break; 3097 break;
2806 } 3098 }
2807 3099
2808 TARGET(BUILD_SLICE) 3100 TARGET(BUILD_SLICE)
3101 {
2809 if (oparg == 3) 3102 if (oparg == 3)
2810 w = POP(); 3103 w = POP();
2811 else 3104 else
2812 w = NULL; 3105 w = NULL;
2813 v = POP(); 3106 v = POP();
2814 u = TOP(); 3107 u = TOP();
2815 x = PySlice_New(u, v, w); 3108 x = PySlice_New(u, v, w);
2816 Py_DECREF(u); 3109 Py_DECREF(u);
2817 Py_DECREF(v); 3110 Py_DECREF(v);
2818 Py_XDECREF(w); 3111 Py_XDECREF(w);
2819 SET_TOP(x); 3112 SET_TOP(x);
2820 if (x != NULL) DISPATCH(); 3113 if (x != NULL) DISPATCH();
2821 break; 3114 break;
3115 }
2822 3116
2823 TARGET(EXTENDED_ARG) 3117 TARGET(EXTENDED_ARG)
3118 {
2824 opcode = NEXTOP(); 3119 opcode = NEXTOP();
2825 oparg = oparg<<16 | NEXTARG(); 3120 oparg = oparg<<16 | NEXTARG();
2826 goto dispatch_opcode; 3121 goto dispatch_opcode;
3122 }
2827 3123
2828 #if USE_COMPUTED_GOTOS 3124 #if USE_COMPUTED_GOTOS
2829 _unknown_opcode: 3125 _unknown_opcode:
2830 #endif 3126 #endif
2831 default: 3127 default:
2832 fprintf(stderr, 3128 fprintf(stderr,
2833 "XXX lineno: %d, opcode: %d\n", 3129 "XXX lineno: %d, opcode: %d\n",
2834 PyFrame_GetLineNumber(f), 3130 PyFrame_GetLineNumber(f),
2835 opcode); 3131 opcode);
2836 PyErr_SetString(PyExc_SystemError, "unknown opcode"); 3132 PyErr_SetString(PyExc_SystemError, "unknown opcode");
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
2908 /* Unwind stacks if a (pseudo) exception occurred */ 3204 /* Unwind stacks if a (pseudo) exception occurred */
2909 3205
2910 fast_block_end: 3206 fast_block_end:
2911 while (why != WHY_NOT && f->f_iblock > 0) { 3207 while (why != WHY_NOT && f->f_iblock > 0) {
2912 /* Peek at the current block. */ 3208 /* Peek at the current block. */
2913 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1]; 3209 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
2914 3210
2915 assert(why != WHY_YIELD); 3211 assert(why != WHY_YIELD);
2916 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) { 3212 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2917 why = WHY_NOT; 3213 why = WHY_NOT;
2918 JUMPTO(PyLong_AS_LONG(retval)); 3214 JUMPTO(PyInt_AS_LONG(retval));
2919 Py_DECREF(retval); 3215 Py_DECREF(retval);
2920 break; 3216 break;
2921 } 3217 }
3218
2922 /* Now we have to pop the block. */ 3219 /* Now we have to pop the block. */
2923 f->f_iblock--; 3220 f->f_iblock--;
2924 3221
2925 if (b->b_type == EXCEPT_HANDLER) { 3222 while (STACK_LEVEL() > b->b_level) {
2926 UNWIND_EXCEPT_HANDLER(b); 3223 v = POP();
2927 continue; 3224 Py_XDECREF(v);
2928 } 3225 }
2929 UNWIND_BLOCK(b);
2930 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { 3226 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2931 why = WHY_NOT; 3227 why = WHY_NOT;
2932 JUMPTO(b->b_handler); 3228 JUMPTO(b->b_handler);
2933 break; 3229 break;
2934 } 3230 }
2935 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT 3231 if (b->b_type == SETUP_FINALLY ||
2936 || b->b_type == SETUP_FINALLY)) { 3232 (b->b_type == SETUP_EXCEPT &&
2937 PyObject *exc, *val, *tb; 3233 why == WHY_EXCEPTION) ||
2938 int handler = b->b_handler; 3234 b->b_type == SETUP_WITH) {
2939 /* Beware, this invalidates all b->b_* fields */ 3235 if (why == WHY_EXCEPTION) {
2940 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL()); 3236 PyObject *exc, *val, *tb;
2941 PUSH(tstate->exc_traceback); 3237 PyErr_Fetch(&exc, &val, &tb);
2942 PUSH(tstate->exc_value); 3238 if (val == NULL) {
2943 if (tstate->exc_type != NULL) { 3239 val = Py_None;
2944 PUSH(tstate->exc_type); 3240 Py_INCREF(val);
3241 }
3242 /* Make the raw exception data
3243 available to the handler,
3244 so a program can emulate the
3245 Python main loop. Don't do
3246 this for 'finally'. */
3247 if (b->b_type == SETUP_EXCEPT ||
3248 b->b_type == SETUP_WITH) {
3249 PyErr_NormalizeException(
3250 &exc, &val, &tb);
3251 set_exc_info(tstate,
3252 exc, val, tb);
3253 }
3254 if (tb == NULL) {
3255 Py_INCREF(Py_None);
3256 PUSH(Py_None);
3257 } else
3258 PUSH(tb);
3259 PUSH(val);
3260 PUSH(exc);
2945 } 3261 }
2946 else { 3262 else {
2947 Py_INCREF(Py_None); 3263 if (why & (WHY_RETURN | WHY_CONTINUE))
2948 PUSH(Py_None); 3264 PUSH(retval);
3265 v = PyInt_FromLong((long)why);
3266 PUSH(v);
2949 } 3267 }
2950 PyErr_Fetch(&exc, &val, &tb);
2951 /* Make the raw exception data
2952 available to the handler,
2953 so a program can emulate the
2954 Python main loop. */
2955 PyErr_NormalizeException(
2956 &exc, &val, &tb);
2957 PyException_SetTraceback(val, tb);
2958 Py_INCREF(exc);
2959 tstate->exc_type = exc;
2960 Py_INCREF(val);
2961 tstate->exc_value = val;
2962 tstate->exc_traceback = tb;
2963 if (tb == NULL)
2964 tb = Py_None;
2965 Py_INCREF(tb);
2966 PUSH(tb);
2967 PUSH(val);
2968 PUSH(exc);
2969 why = WHY_NOT;
2970 JUMPTO(handler);
2971 break;
2972 }
2973 if (b->b_type == SETUP_FINALLY) {
2974 if (why & (WHY_RETURN | WHY_CONTINUE))
2975 PUSH(retval);
2976 PUSH(PyLong_FromLong((long)why));
2977 why = WHY_NOT; 3268 why = WHY_NOT;
2978 JUMPTO(b->b_handler); 3269 JUMPTO(b->b_handler);
2979 break; 3270 break;
2980 } 3271 }
2981 } /* unwind stack */ 3272 } /* unwind stack */
2982 3273
2983 /* End the loop if we still have an error (or return) */ 3274 /* End the loop if we still have an error (or return) */
2984 3275
2985 if (why != WHY_NOT) 3276 if (why != WHY_NOT)
2986 break; 3277 break;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
3019 if (tstate->c_profilefunc) { 3310 if (tstate->c_profilefunc) {
3020 if (why == WHY_EXCEPTION) 3311 if (why == WHY_EXCEPTION)
3021 call_trace_protected(tstate->c_profilefunc, 3312 call_trace_protected(tstate->c_profilefunc,
3022 tstate->c_profileobj, f, 3313 tstate->c_profileobj, f,
3023 PyTrace_RETURN, NULL); 3314 PyTrace_RETURN, NULL);
3024 else if (call_trace(tstate->c_profilefunc, 3315 else if (call_trace(tstate->c_profilefunc,
3025 tstate->c_profileobj, f, 3316 tstate->c_profileobj, f,
3026 PyTrace_RETURN, retval)) { 3317 PyTrace_RETURN, retval)) {
3027 Py_XDECREF(retval); 3318 Py_XDECREF(retval);
3028 retval = NULL; 3319 retval = NULL;
3029 /* why = WHY_EXCEPTION; */ 3320 why = WHY_EXCEPTION;
3030 } 3321 }
3031 } 3322 }
3323 }
3324
3325 if (tstate->frame->f_exc_type != NULL)
3326 reset_exc_info(tstate);
3327 else {
3328 assert(tstate->frame->f_exc_value == NULL);
3329 assert(tstate->frame->f_exc_traceback == NULL);
3032 } 3330 }
3033 3331
3034 /* pop frame */ 3332 /* pop frame */
3035 exit_eval_frame: 3333 exit_eval_frame:
3036 Py_LeaveRecursiveCall(); 3334 Py_LeaveRecursiveCall();
3037 tstate->frame = f->f_back; 3335 tstate->frame = f->f_back;
3038 3336
3039 return retval; 3337 return retval;
3040 } 3338 }
3041 3339
3042 /* This is gonna seem *real weird*, but if you put some other code between 3340 /* This is gonna seem *real weird*, but if you put some other code between
3043 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust 3341 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
3044 the test in the if statements in Misc/gdbinit (pystack and pystackv). */ 3342 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
3045 3343
3046 PyObject * 3344 PyObject *
3047 PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals, 3345 PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
3048 PyObject **args, int argcount, PyObject **kws, int kwcount, 3346 PyObject **args, int argcount, PyObject **kws, int kwcount,
3049 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure) 3347 PyObject **defs, int defcount, PyObject *closure)
3050 { 3348 {
3051 PyCodeObject* co = (PyCodeObject*)_co;
3052 register PyFrameObject *f; 3349 register PyFrameObject *f;
3053 register PyObject *retval = NULL; 3350 register PyObject *retval = NULL;
3054 register PyObject **fastlocals, **freevars; 3351 register PyObject **fastlocals, **freevars;
3055 PyThreadState *tstate = PyThreadState_GET(); 3352 PyThreadState *tstate = PyThreadState_GET();
3056 PyObject *x, *u; 3353 PyObject *x, *u;
3057 int total_args = co->co_argcount + co->co_kwonlyargcount;
3058 3354
3059 if (globals == NULL) { 3355 if (globals == NULL) {
3060 PyErr_SetString(PyExc_SystemError, 3356 PyErr_SetString(PyExc_SystemError,
3061 "PyEval_EvalCodeEx: NULL globals"); 3357 "PyEval_EvalCodeEx: NULL globals");
3062 return NULL; 3358 return NULL;
3063 } 3359 }
3064 3360
3065 assert(tstate != NULL); 3361 assert(tstate != NULL);
3066 assert(globals != NULL); 3362 assert(globals != NULL);
3067 f = PyFrame_New(tstate, co, globals, locals); 3363 f = PyFrame_New(tstate, co, globals, locals);
3068 if (f == NULL) 3364 if (f == NULL)
3069 return NULL; 3365 return NULL;
3070 3366
3071 fastlocals = f->f_localsplus; 3367 fastlocals = f->f_localsplus;
3072 freevars = f->f_localsplus + co->co_nlocals; 3368 freevars = f->f_localsplus + co->co_nlocals;
3073 3369
3074 if (total_args || co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) { 3370 if (co->co_argcount > 0 ||
3371 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3075 int i; 3372 int i;
3076 int n = argcount; 3373 int n = argcount;
3077 PyObject *kwdict = NULL; 3374 PyObject *kwdict = NULL;
3078 if (co->co_flags & CO_VARKEYWORDS) { 3375 if (co->co_flags & CO_VARKEYWORDS) {
3079 kwdict = PyDict_New(); 3376 kwdict = PyDict_New();
3080 if (kwdict == NULL) 3377 if (kwdict == NULL)
3081 goto fail; 3378 goto fail;
3082 i = total_args; 3379 i = co->co_argcount;
3083 if (co->co_flags & CO_VARARGS) 3380 if (co->co_flags & CO_VARARGS)
3084 i++; 3381 i++;
3085 SETLOCAL(i, kwdict); 3382 SETLOCAL(i, kwdict);
3086 } 3383 }
3087 if (argcount > co->co_argcount) { 3384 if (argcount > co->co_argcount) {
3088 if (!(co->co_flags & CO_VARARGS)) { 3385 if (!(co->co_flags & CO_VARARGS)) {
3089 PyErr_Format(PyExc_TypeError, 3386 PyErr_Format(PyExc_TypeError,
3090 "%U() takes %s %d " 3387 "%.200s() takes %s %d "
3091 "positional argument%s (%d given)", 3388 "argument%s (%d given)",
3092 co->co_name, 3389 PyString_AsString(co->co_name),
3093 defcount ? "at most" : "exactly", 3390 defcount ? "at most" : "exactly",
3094 co->co_argcount, 3391 co->co_argcount,
3095 co->co_argcount == 1 ? "" : "s", 3392 co->co_argcount == 1 ? "" : "s",
3096 argcount + kwcount); 3393 argcount + kwcount);
3097 goto fail; 3394 goto fail;
3098 } 3395 }
3099 n = co->co_argcount; 3396 n = co->co_argcount;
3100 } 3397 }
3101 for (i = 0; i < n; i++) { 3398 for (i = 0; i < n; i++) {
3102 x = args[i]; 3399 x = args[i];
3103 Py_INCREF(x); 3400 Py_INCREF(x);
3104 SETLOCAL(i, x); 3401 SETLOCAL(i, x);
3105 } 3402 }
3106 if (co->co_flags & CO_VARARGS) { 3403 if (co->co_flags & CO_VARARGS) {
3107 u = PyTuple_New(argcount - n); 3404 u = PyTuple_New(argcount - n);
3108 if (u == NULL) 3405 if (u == NULL)
3109 goto fail; 3406 goto fail;
3110 SETLOCAL(total_args, u); 3407 SETLOCAL(co->co_argcount, u);
3111 for (i = n; i < argcount; i++) { 3408 for (i = n; i < argcount; i++) {
3112 x = args[i]; 3409 x = args[i];
3113 Py_INCREF(x); 3410 Py_INCREF(x);
3114 PyTuple_SET_ITEM(u, i-n, x); 3411 PyTuple_SET_ITEM(u, i-n, x);
3115 } 3412 }
3116 } 3413 }
3117 for (i = 0; i < kwcount; i++) { 3414 for (i = 0; i < kwcount; i++) {
3118 PyObject **co_varnames; 3415 PyObject **co_varnames;
3119 PyObject *keyword = kws[2*i]; 3416 PyObject *keyword = kws[2*i];
3120 PyObject *value = kws[2*i + 1]; 3417 PyObject *value = kws[2*i + 1];
3121 int j; 3418 int j;
3122 if (keyword == NULL || !PyUnicode_Check(keyword)) { 3419 if (keyword == NULL || !(PyString_Check(keyword)
3420 #ifdef Py_USING_UNICODE
3421 || PyUnicode_Check(keyword)
3422 #endif
3423 )) {
3123 PyErr_Format(PyExc_TypeError, 3424 PyErr_Format(PyExc_TypeError,
3124 "%U() keywords must be strings", 3425 "%.200s() keywords must be strings",
3125 co->co_name); 3426 PyString_AsString(co->co_name));
3126 goto fail; 3427 goto fail;
3127 } 3428 }
3128 /* Speed hack: do raw pointer compares. As names are 3429 /* Speed hack: do raw pointer compares. As names are
3129 normally interned this should almost always hit. */ 3430 normally interned this should almost always hit. */
3130 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; 3431 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3131 for (j = 0; j < total_args; j++) { 3432 for (j = 0; j < co->co_argcount; j++) {
3132 PyObject *nm = co_varnames[j]; 3433 PyObject *nm = co_varnames[j];
3133 if (nm == keyword) 3434 if (nm == keyword)
3134 goto kw_found; 3435 goto kw_found;
3135 } 3436 }
3136 /* Slow fallback, just in case */ 3437 /* Slow fallback, just in case */
3137 for (j = 0; j < total_args; j++) { 3438 for (j = 0; j < co->co_argcount; j++) {
3138 PyObject *nm = co_varnames[j]; 3439 PyObject *nm = co_varnames[j];
3139 int cmp = PyObject_RichCompareBool( 3440 int cmp = PyObject_RichCompareBool(
3140 keyword, nm, Py_EQ); 3441 keyword, nm, Py_EQ);
3141 if (cmp > 0) 3442 if (cmp > 0)
3142 goto kw_found; 3443 goto kw_found;
3143 else if (cmp < 0) 3444 else if (cmp < 0)
3144 goto fail; 3445 goto fail;
3145 } 3446 }
3146 if (j >= total_args && kwdict == NULL) { 3447 if (kwdict == NULL) {
3147 PyErr_Format(PyExc_TypeError, 3448 PyObject *kwd_str = kwd_as_string(keyword);
3148 "%U() got an unexpected " 3449 if (kwd_str) {
3149 "keyword argument '%S'", 3450 PyErr_Format(PyExc_TypeError,
3150 co->co_name, 3451 "%.200s() got an unexpected "
3151 keyword); 3452 "keyword argument '%.400s'",
3453 PyString_AsString(co->co_name),
3454 PyString_AsString(kwd_str));
3455 Py_DECREF(kwd_str);
3456 }
3152 goto fail; 3457 goto fail;
3153 } 3458 }
3154 PyDict_SetItem(kwdict, keyword, value); 3459 PyDict_SetItem(kwdict, keyword, value);
3155 continue; 3460 continue;
3156 kw_found: 3461 kw_found:
3157 if (GETLOCAL(j) != NULL) { 3462 if (GETLOCAL(j) != NULL) {
3158 PyErr_Format(PyExc_TypeError, 3463 PyObject *kwd_str = kwd_as_string(keyword);
3159 "%U() got multiple " 3464 if (kwd_str) {
3160 "values for keyword " 3465 PyErr_Format(PyExc_TypeError,
3161 "argument '%S'", 3466 "%.200s() got multiple "
3162 co->co_name, 3467 "values for keyword "
3163 keyword); 3468 "argument '%.400s'",
3469 PyString_AsString(co->co_name),
3470 PyString_AsString(kwd_str));
3471 Py_DECREF(kwd_str);
3472 }
3164 goto fail; 3473 goto fail;
3165 } 3474 }
3166 Py_INCREF(value); 3475 Py_INCREF(value);
3167 SETLOCAL(j, value); 3476 SETLOCAL(j, value);
3168 }
3169 if (co->co_kwonlyargcount > 0) {
3170 for (i = co->co_argcount; i < total_args; i++) {
3171 PyObject *name;
3172 if (GETLOCAL(i) != NULL)
3173 continue;
3174 name = PyTuple_GET_ITEM(co->co_varnames, i);
3175 if (kwdefs != NULL) {
3176 PyObject *def = PyDict_GetItem(kwdefs, name);
3177 if (def) {
3178 Py_INCREF(def);
3179 SETLOCAL(i, def);
3180 continue;
3181 }
3182 }
3183 PyErr_Format(PyExc_TypeError,
3184 "%U() needs keyword-only argument %S",
3185 co->co_name, name);
3186 goto fail;
3187 }
3188 } 3477 }
3189 if (argcount < co->co_argcount) { 3478 if (argcount < co->co_argcount) {
3190 int m = co->co_argcount - defcount; 3479 int m = co->co_argcount - defcount;
3191 for (i = argcount; i < m; i++) { 3480 for (i = argcount; i < m; i++) {
3192 if (GETLOCAL(i) == NULL) { 3481 if (GETLOCAL(i) == NULL) {
3193 int j, given = 0; 3482 int j, given = 0;
3194 for (j = 0; j < co->co_argcount; j++) 3483 for (j = 0; j < co->co_argcount; j++)
3195 if (GETLOCAL(j)) 3484 if (GETLOCAL(j))
3196 given++; 3485 given++;
3197 PyErr_Format(PyExc_TypeError, 3486 PyErr_Format(PyExc_TypeError,
3198 "%U() takes %s %d " 3487 "%.200s() takes %s %d "
3199 "argument%s " 3488 "argument%s (%d given)",
3200 "(%d given)", 3489 PyString_AsString(co->co_name),
3201 co->co_name,
3202 ((co->co_flags & CO_VARARGS) || 3490 ((co->co_flags & CO_VARARGS) ||
3203 defcount) ? "at least" 3491 defcount) ? "at least"
3204 : "exactly", 3492 : "exactly",
3205 m, m == 1 ? "" : "s", given); 3493 m, m == 1 ? "" : "s", given);
3206 goto fail; 3494 goto fail;
3207 } 3495 }
3208 } 3496 }
3209 if (n > m) 3497 if (n > m)
3210 i = n - m; 3498 i = n - m;
3211 else 3499 else
3212 i = 0; 3500 i = 0;
3213 for (; i < defcount; i++) { 3501 for (; i < defcount; i++) {
3214 if (GETLOCAL(m+i) == NULL) { 3502 if (GETLOCAL(m+i) == NULL) {
3215 PyObject *def = defs[i]; 3503 PyObject *def = defs[i];
3216 Py_INCREF(def); 3504 Py_INCREF(def);
3217 SETLOCAL(m+i, def); 3505 SETLOCAL(m+i, def);
3218 } 3506 }
3219 } 3507 }
3220 } 3508 }
3221 } 3509 }
3222 else if (argcount > 0 || kwcount > 0) { 3510 else if (argcount > 0 || kwcount > 0) {
3223 PyErr_Format(PyExc_TypeError, 3511 PyErr_Format(PyExc_TypeError,
3224 "%U() takes no arguments (%d given)", 3512 "%.200s() takes no arguments (%d given)",
3225 co->co_name, 3513 PyString_AsString(co->co_name),
3226 argcount + kwcount); 3514 argcount + kwcount);
3227 goto fail; 3515 goto fail;
3228 } 3516 }
3229 /* Allocate and initialize storage for cell vars, and copy free 3517 /* Allocate and initialize storage for cell vars, and copy free
3230 vars into frame. This isn't too efficient right now. */ 3518 vars into frame. This isn't too efficient right now. */
3231 if (PyTuple_GET_SIZE(co->co_cellvars)) { 3519 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3232 int i, j, nargs, found; 3520 int i, j, nargs, found;
3233 Py_UNICODE *cellname, *argname; 3521 char *cellname, *argname;
3234 PyObject *c; 3522 PyObject *c;
3235 3523
3236 nargs = total_args; 3524 nargs = co->co_argcount;
3237 if (co->co_flags & CO_VARARGS) 3525 if (co->co_flags & CO_VARARGS)
3238 nargs++; 3526 nargs++;
3239 if (co->co_flags & CO_VARKEYWORDS) 3527 if (co->co_flags & CO_VARKEYWORDS)
3240 nargs++; 3528 nargs++;
3241 3529
3242 /* Initialize each cell var, taking into account 3530 /* Initialize each cell var, taking into account
3243 cell vars that are initialized from arguments. 3531 cell vars that are initialized from arguments.
3244 3532
3245 Should arrange for the compiler to put cellvars 3533 Should arrange for the compiler to put cellvars
3246 that are arguments at the beginning of the cellvars 3534 that are arguments at the beginning of the cellvars
3247 list so that we can march over it more efficiently? 3535 list so that we can march over it more efficiently?
3248 */ 3536 */
3249 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { 3537 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3250 cellname = PyUnicode_AS_UNICODE( 3538 cellname = PyString_AS_STRING(
3251 PyTuple_GET_ITEM(co->co_cellvars, i)); 3539 PyTuple_GET_ITEM(co->co_cellvars, i));
3252 found = 0; 3540 found = 0;
3253 for (j = 0; j < nargs; j++) { 3541 for (j = 0; j < nargs; j++) {
3254 argname = PyUnicode_AS_UNICODE( 3542 argname = PyString_AS_STRING(
3255 PyTuple_GET_ITEM(co->co_varnames, j)); 3543 PyTuple_GET_ITEM(co->co_varnames, j));
3256 if (Py_UNICODE_strcmp(cellname, argname) == 0) { 3544 if (strcmp(cellname, argname) == 0) {
3257 c = PyCell_New(GETLOCAL(j)); 3545 c = PyCell_New(GETLOCAL(j));
3258 if (c == NULL) 3546 if (c == NULL)
3259 goto fail; 3547 goto fail;
3260 GETLOCAL(co->co_nlocals + i) = c; 3548 GETLOCAL(co->co_nlocals + i) = c;
3261 found = 1; 3549 found = 1;
3262 break; 3550 break;
3263 } 3551 }
3264 } 3552 }
3265 if (found == 0) { 3553 if (found == 0) {
3266 c = PyCell_New(NULL); 3554 c = PyCell_New(NULL);
3267 if (c == NULL) 3555 if (c == NULL)
3268 goto fail; 3556 goto fail;
3269 SETLOCAL(co->co_nlocals + i, c); 3557 SETLOCAL(co->co_nlocals + i, c);
3270 } 3558 }
3271 } 3559 }
3272 } 3560 }
3273 if (PyTuple_GET_SIZE(co->co_freevars)) { 3561 if (PyTuple_GET_SIZE(co->co_freevars)) {
3274 int i; 3562 int i;
3275 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { 3563 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3276 PyObject *o = PyTuple_GET_ITEM(closure, i); 3564 PyObject *o = PyTuple_GET_ITEM(closure, i);
3277 Py_INCREF(o); 3565 Py_INCREF(o);
3278 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; 3566 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3279 } 3567 }
3280 } 3568 }
3281 3569
3282 if (co->co_flags & CO_GENERATOR) { 3570 if (co->co_flags & CO_GENERATOR) {
3283 /* Don't need to keep the reference to f_back, it will be set 3571 /* Don't need to keep the reference to f_back, it will be set
3284 * when the generator is resumed. */ 3572 * when the generator is resumed. */
3285 Py_XDECREF(f->f_back); 3573 Py_CLEAR(f->f_back);
3286 f->f_back = NULL;
3287 3574
3288 PCALL(PCALL_GENERATOR); 3575 PCALL(PCALL_GENERATOR);
3289 3576
3290 /* Create a new generator that owns the ready to run frame 3577 /* Create a new generator that owns the ready to run frame
3291 * and return that as the value. */ 3578 * and return that as the value. */
3292 return PyGen_New(f); 3579 return PyGen_New(f);
3293 } 3580 }
3294 3581
3295 retval = PyEval_EvalFrameEx(f,0); 3582 retval = PyEval_EvalFrameEx(f,0);
3296 3583
3297 fail: /* Jump here from prelude on failure */ 3584 fail: /* Jump here from prelude on failure */
3298 3585
3299 /* decref'ing the frame can cause __del__ methods to get invoked, 3586 /* decref'ing the frame can cause __del__ methods to get invoked,
3300 which can call back into Python. While we're done with the 3587 which can call back into Python. While we're done with the
3301 current Python frame (f), the associated C stack is still in use, 3588 current Python frame (f), the associated C stack is still in use,
3302 so recursion_depth must be boosted for the duration. 3589 so recursion_depth must be boosted for the duration.
3303 */ 3590 */
3304 assert(tstate != NULL); 3591 assert(tstate != NULL);
3305 ++tstate->recursion_depth; 3592 ++tstate->recursion_depth;
3306 Py_DECREF(f); 3593 Py_DECREF(f);
3307 --tstate->recursion_depth; 3594 --tstate->recursion_depth;
3308 return retval; 3595 return retval;
3309 } 3596 }
3310 3597
3311 3598
3312 static PyObject * 3599 static PyObject *
3313 special_lookup(PyObject *o, char *meth, PyObject **cache) 3600 special_lookup(PyObject *o, char *meth, PyObject **cache)
3314 { 3601 {
3315 PyObject *res; 3602 PyObject *res;
3603 if (PyInstance_Check(o)) {
3604 if (!*cache)
3605 return PyObject_GetAttrString(o, meth);
3606 else
3607 return PyObject_GetAttr(o, *cache);
3608 }
3316 res = _PyObject_LookupSpecial(o, meth, cache); 3609 res = _PyObject_LookupSpecial(o, meth, cache);
3317 if (res == NULL && !PyErr_Occurred()) { 3610 if (res == NULL && !PyErr_Occurred()) {
3318 PyErr_SetObject(PyExc_AttributeError, *cache); 3611 PyErr_SetObject(PyExc_AttributeError, *cache);
3319 return NULL; 3612 return NULL;
3320 } 3613 }
3321 return res; 3614 return res;
3322 } 3615 }
3323 3616
3324 3617
3618 static PyObject *
3619 kwd_as_string(PyObject *kwd) {
3620 #ifdef Py_USING_UNICODE
3621 if (PyString_Check(kwd)) {
3622 #else
3623 assert(PyString_Check(kwd));
3624 #endif
3625 Py_INCREF(kwd);
3626 return kwd;
3627 #ifdef Py_USING_UNICODE
3628 }
3629 return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
3630 #endif
3631 }
3632
3633
3634 /* Implementation notes for set_exc_info() and reset_exc_info():
3635
3636 - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3637 'exc_traceback'. These always travel together.
3638
3639 - tstate->curexc_ZZZ is the "hot" exception that is set by
3640 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3641
3642 - Once an exception is caught by an except clause, it is transferred
3643 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3644 can pick it up. This is the primary task of set_exc_info().
3645 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
3646
3647 - Now let me explain the complicated dance with frame->f_exc_ZZZ.
3648
3649 Long ago, when none of this existed, there were just a few globals:
3650 one set corresponding to the "hot" exception, and one set
3651 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3652 globals; they were simply stored as sys.exc_ZZZ. For backwards
3653 compatibility, they still are!) The problem was that in code like
3654 this:
3655
3656 try:
3657 "something that may fail"
3658 except "some exception":
3659 "do something else first"
3660 "print the exception from sys.exc_ZZZ."
3661
3662 if "do something else first" invoked something that raised and caught
3663 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3664 cause of subtle bugs. I fixed this by changing the semantics as
3665 follows:
3666
3667 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3668 *in that frame*.
3669
3670 - But initially, and as long as no exception is caught in a given
3671 frame, sys.exc_ZZZ will hold the last exception caught in the
3672 previous frame (or the frame before that, etc.).
3673
3674 The first bullet fixed the bug in the above example. The second
3675 bullet was for backwards compatibility: it was (and is) common to
3676 have a function that is called when an exception is caught, and to
3677 have that function access the caught exception via sys.exc_ZZZ.
3678 (Example: traceback.print_exc()).
3679
3680 At the same time I fixed the problem that sys.exc_ZZZ weren't
3681 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3682 but that's really a separate improvement.
3683
3684 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3685 variables to what they were before the current frame was called. The
3686 set_exc_info() function saves them on the frame so that
3687 reset_exc_info() can restore them. The invariant is that
3688 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3689 exception (where "catching" an exception applies only to successful
3690 except clauses); and if the current frame ever caught an exception,
3691 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3692 at the start of the current frame.
3693
3694 */
3695
3696 static void
3697 set_exc_info(PyThreadState *tstate,
3698 PyObject *type, PyObject *value, PyObject *tb)
3699 {
3700 PyFrameObject *frame = tstate->frame;
3701 PyObject *tmp_type, *tmp_value, *tmp_tb;
3702
3703 assert(type != NULL);
3704 assert(frame != NULL);
3705 if (frame->f_exc_type == NULL) {
3706 assert(frame->f_exc_value == NULL);
3707 assert(frame->f_exc_traceback == NULL);
3708 /* This frame didn't catch an exception before. */
3709 /* Save previous exception of this thread in this frame. */
3710 if (tstate->exc_type == NULL) {
3711 /* XXX Why is this set to Py_None? */
3712 Py_INCREF(Py_None);
3713 tstate->exc_type = Py_None;
3714 }
3715 Py_INCREF(tstate->exc_type);
3716 Py_XINCREF(tstate->exc_value);
3717 Py_XINCREF(tstate->exc_traceback);
3718 frame->f_exc_type = tstate->exc_type;
3719 frame->f_exc_value = tstate->exc_value;
3720 frame->f_exc_traceback = tstate->exc_traceback;
3721 }
3722 /* Set new exception for this thread. */
3723 tmp_type = tstate->exc_type;
3724 tmp_value = tstate->exc_value;
3725 tmp_tb = tstate->exc_traceback;
3726 Py_INCREF(type);
3727 Py_XINCREF(value);
3728 Py_XINCREF(tb);
3729 tstate->exc_type = type;
3730 tstate->exc_value = value;
3731 tstate->exc_traceback = tb;
3732 Py_XDECREF(tmp_type);
3733 Py_XDECREF(tmp_value);
3734 Py_XDECREF(tmp_tb);
3735 /* For b/w compatibility */
3736 PySys_SetObject("exc_type", type);
3737 PySys_SetObject("exc_value", value);
3738 PySys_SetObject("exc_traceback", tb);
3739 }
3740
3741 static void
3742 reset_exc_info(PyThreadState *tstate)
3743 {
3744 PyFrameObject *frame;
3745 PyObject *tmp_type, *tmp_value, *tmp_tb;
3746
3747 /* It's a precondition that the thread state's frame caught an
3748 * exception -- verify in a debug build.
3749 */
3750 assert(tstate != NULL);
3751 frame = tstate->frame;
3752 assert(frame != NULL);
3753 assert(frame->f_exc_type != NULL);
3754
3755 /* Copy the frame's exception info back to the thread state. */
3756 tmp_type = tstate->exc_type;
3757 tmp_value = tstate->exc_value;
3758 tmp_tb = tstate->exc_traceback;
3759 Py_INCREF(frame->f_exc_type);
3760 Py_XINCREF(frame->f_exc_value);
3761 Py_XINCREF(frame->f_exc_traceback);
3762 tstate->exc_type = frame->f_exc_type;
3763 tstate->exc_value = frame->f_exc_value;
3764 tstate->exc_traceback = frame->f_exc_traceback;
3765 Py_XDECREF(tmp_type);
3766 Py_XDECREF(tmp_value);
3767 Py_XDECREF(tmp_tb);
3768
3769 /* For b/w compatibility */
3770 PySys_SetObject("exc_type", frame->f_exc_type);
3771 PySys_SetObject("exc_value", frame->f_exc_value);
3772 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3773
3774 /* Clear the frame's exception info. */
3775 tmp_type = frame->f_exc_type;
3776 tmp_value = frame->f_exc_value;
3777 tmp_tb = frame->f_exc_traceback;
3778 frame->f_exc_type = NULL;
3779 frame->f_exc_value = NULL;
3780 frame->f_exc_traceback = NULL;
3781 Py_DECREF(tmp_type);
3782 Py_XDECREF(tmp_value);
3783 Py_XDECREF(tmp_tb);
3784 }
3785
3325 /* Logic for the raise statement (too complicated for inlining). 3786 /* Logic for the raise statement (too complicated for inlining).
3326 This *consumes* a reference count to each of its arguments. */ 3787 This *consumes* a reference count to each of its arguments. */
3327 static enum why_code 3788 static enum why_code
3328 do_raise(PyObject *exc, PyObject *cause) 3789 do_raise(PyObject *type, PyObject *value, PyObject *tb)
3329 { 3790 {
3330 PyObject *type = NULL, *value = NULL; 3791 if (type == NULL) {
3331
3332 if (exc == NULL) {
3333 /* Reraise */ 3792 /* Reraise */
3334 PyThreadState *tstate = PyThreadState_GET(); 3793 PyThreadState *tstate = PyThreadState_GET();
3335 PyObject *tb; 3794 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3336 type = tstate->exc_type;
3337 value = tstate->exc_value; 3795 value = tstate->exc_value;
3338 tb = tstate->exc_traceback; 3796 tb = tstate->exc_traceback;
3339 if (type == Py_None) {
3340 PyErr_SetString(PyExc_RuntimeError,
3341 "No active exception to reraise");
3342 return WHY_EXCEPTION;
3343 }
3344 Py_XINCREF(type); 3797 Py_XINCREF(type);
3345 Py_XINCREF(value); 3798 Py_XINCREF(value);
3346 Py_XINCREF(tb); 3799 Py_XINCREF(tb);
3347 PyErr_Restore(type, value, tb);
3348 return WHY_RERAISE;
3349 } 3800 }
3350 3801
3351 /* We support the following forms of raise: 3802 /* We support the following forms of raise:
3352 raise 3803 raise <class>, <classinstance>
3353 raise <instance> 3804 raise <class>, <argument tuple>
3354 raise <type> */ 3805 raise <class>, None
3355 3806 raise <class>, <argument>