Changeset 388 for python/vendor/current/Python/ceval.c
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Python/ceval.c
r2 r388 28 28 typedef unsigned long long uint64; 29 29 30 #if defined(__ppc__) /* <- Don't know if this is the correct symbol; this 31 section should work for GCC on any PowerPC 32 platform, irrespective of OS. 33 POWER? Who knows :-) */ 30 /* PowerPC support. 31 "__ppc__" appears to be the preprocessor definition to detect on OS X, whereas 32 "__powerpc__" appears to be the correct one for Linux with GCC 33 */ 34 #if defined(__ppc__) || defined (__powerpc__) 34 35 35 36 #define READ_TIMESTAMP(var) ppc_getcounter(&var) … … 38 39 ppc_getcounter(uint64 *v) 39 40 { 40 41 register unsigned long tbu, tb, tbu2; 41 42 42 43 loop: 43 44 45 46 47 48 49 50 51 44 asm volatile ("mftbu %0" : "=r" (tbu) ); 45 asm volatile ("mftb %0" : "=r" (tb) ); 46 asm volatile ("mftbu %0" : "=r" (tbu2)); 47 if (__builtin_expect(tbu != tbu2, 0)) goto loop; 48 49 /* The slightly peculiar way of writing the next lines is 50 compiled better by GCC than any other way I tried. */ 51 ((long*)(v))[0] = tbu; 52 ((long*)(v))[1] = tb; 52 53 } 53 54 … … 78 79 79 80 void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1, 80 81 { 82 83 84 85 86 87 88 89 90 81 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1) 82 { 83 uint64 intr, inst, loop; 84 PyThreadState *tstate = PyThreadState_Get(); 85 if (!tstate->interp->tscdump) 86 return; 87 intr = intr1 - intr0; 88 inst = inst1 - inst0 - intr; 89 loop = loop1 - loop0 - intr; 90 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n", 91 opcode, ticked, inst, loop); 91 92 } 92 93 … … 98 99 #ifdef Py_DEBUG 99 100 /* For debugging the interpreter: */ 100 #define LLTRACE 1 101 #define CHECKEXC 1 101 #define LLTRACE 1 /* Low-level trace feature */ 102 #define CHECKEXC 1 /* Double-check exception checking */ 102 103 #endif 103 104 … … 114 115 static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int); 115 116 static PyObject * update_keyword_args(PyObject *, int, PyObject ***, 116 117 PyObject *); 117 118 static PyObject * update_star_args(int, int, PyObject *, PyObject ***); 118 119 static PyObject * load_args(PyObject ***, int); … … 125 126 #endif 126 127 static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *, 127 128 int, PyObject *); 128 129 static int call_trace_protected(Py_tracefunc, PyObject *, 129 130 PyFrameObject *, int, PyObject *); 130 131 static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *); 131 132 static int maybe_call_line_trace(Py_tracefunc, PyObject *, 132 133 PyFrameObject *, int *, int *, int *); 133 134 134 135 static PyObject * apply_slice(PyObject *, PyObject *, PyObject *); 135 136 static int assign_slice(PyObject *, PyObject *, 136 137 PyObject *, PyObject *); 137 138 static PyObject * cmp_outcome(int, PyObject *, PyObject *); 138 139 static PyObject * import_from(PyObject *, PyObject *); … … 140 141 static PyObject * build_class(PyObject *, PyObject *, PyObject *); 141 142 static int exec_statement(PyFrameObject *, 142 143 PyObject *, PyObject *, PyObject *); 143 144 static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *); 144 145 static void reset_exc_info(PyThreadState *); 145 146 static void format_exc_check_arg(PyObject *, char *, PyObject *); 146 147 static PyObject * string_concatenate(PyObject *, PyObject *, 147 148 PyFrameObject *, unsigned char *); 148 149 static PyObject * kwd_as_string(PyObject *); 150 static PyObject * special_lookup(PyObject *, char *, PyObject **); 149 151 150 152 #define NAME_ERROR_MSG \ 151 153 "name '%.200s' is not defined" 152 154 #define GLOBAL_NAME_ERROR_MSG \ 153 155 "global name '%.200s' is not defined" 154 156 #define UNBOUNDLOCAL_ERROR_MSG \ 155 157 "local variable '%.200s' referenced before assignment" 156 158 #define UNBOUNDFREE_ERROR_MSG \ 157 158 159 "free variable '%.200s' referenced before assignment" \ 160 " in enclosing scope" 159 161 160 162 /* Dynamic execution profile */ … … 208 210 PyEval_GetCallStats(PyObject *self) 209 211 { 210 211 212 213 212 return Py_BuildValue("iiiiiiiiiii", 213 pcall[0], pcall[1], pcall[2], pcall[3], 214 pcall[4], pcall[5], pcall[6], pcall[7], 215 pcall[8], pcall[9], pcall[10]); 214 216 } 215 217 #else … … 219 221 PyEval_GetCallStats(PyObject *self) 220 222 { 221 222 223 Py_INCREF(Py_None); 224 return Py_None; 223 225 } 224 226 #endif … … 233 235 234 236 static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */ 237 static PyThread_type_lock pending_lock = 0; /* for pending calls */ 235 238 static long main_thread = 0; 236 239 … … 238 241 PyEval_ThreadsInitialized(void) 239 242 { 240 243 return interpreter_lock != 0; 241 244 } 242 245 … … 244 247 PyEval_InitThreads(void) 245 248 { 246 247 248 249 250 249 if (interpreter_lock) 250 return; 251 interpreter_lock = PyThread_allocate_lock(); 252 PyThread_acquire_lock(interpreter_lock, 1); 253 main_thread = PyThread_get_thread_ident(); 251 254 } 252 255 … … 254 257 PyEval_AcquireLock(void) 255 258 { 256 259 PyThread_acquire_lock(interpreter_lock, 1); 257 260 } 258 261 … … 260 263 PyEval_ReleaseLock(void) 261 264 { 262 265 PyThread_release_lock(interpreter_lock); 263 266 } 264 267 … … 266 269 PyEval_AcquireThread(PyThreadState *tstate) 267 270 { 268 269 270 271 272 273 274 275 271 if (tstate == NULL) 272 Py_FatalError("PyEval_AcquireThread: NULL new thread state"); 273 /* Check someone has called PyEval_InitThreads() to create the lock */ 274 assert(interpreter_lock); 275 PyThread_acquire_lock(interpreter_lock, 1); 276 if (PyThreadState_Swap(tstate) != NULL) 277 Py_FatalError( 278 "PyEval_AcquireThread: non-NULL old thread state"); 276 279 } 277 280 … … 279 282 PyEval_ReleaseThread(PyThreadState *tstate) 280 283 { 281 282 283 284 285 284 if (tstate == NULL) 285 Py_FatalError("PyEval_ReleaseThread: NULL thread state"); 286 if (PyThreadState_Swap(NULL) != tstate) 287 Py_FatalError("PyEval_ReleaseThread: wrong thread state"); 288 PyThread_release_lock(interpreter_lock); 286 289 } 287 290 … … 294 297 PyEval_ReInitThreads(void) 295 298 { 296 PyObject *threading, *result; 297 PyThreadState *tstate; 298 299 if (!interpreter_lock) 300 return; 301 /*XXX Can't use PyThread_free_lock here because it does too 302 much error-checking. Doing this cleanly would require 303 adding a new function to each thread_*.h. Instead, just 304 create a new lock and waste a little bit of memory */ 305 interpreter_lock = PyThread_allocate_lock(); 306 PyThread_acquire_lock(interpreter_lock, 1); 307 main_thread = PyThread_get_thread_ident(); 308 309 /* Update the threading module with the new state. 310 */ 311 tstate = PyThreadState_GET(); 312 threading = PyMapping_GetItemString(tstate->interp->modules, 313 "threading"); 314 if (threading == NULL) { 315 /* threading not imported */ 316 PyErr_Clear(); 317 return; 318 } 319 result = PyObject_CallMethod(threading, "_after_fork", NULL); 320 if (result == NULL) 321 PyErr_WriteUnraisable(threading); 322 else 323 Py_DECREF(result); 324 Py_DECREF(threading); 299 PyObject *threading, *result; 300 PyThreadState *tstate; 301 302 if (!interpreter_lock) 303 return; 304 /*XXX Can't use PyThread_free_lock here because it does too 305 much error-checking. Doing this cleanly would require 306 adding a new function to each thread_*.h. Instead, just 307 create a new lock and waste a little bit of memory */ 308 interpreter_lock = PyThread_allocate_lock(); 309 pending_lock = PyThread_allocate_lock(); 310 PyThread_acquire_lock(interpreter_lock, 1); 311 main_thread = PyThread_get_thread_ident(); 312 313 /* Update the threading module with the new state. 314 */ 315 tstate = PyThreadState_GET(); 316 threading = PyMapping_GetItemString(tstate->interp->modules, 317 "threading"); 318 if (threading == NULL) { 319 /* threading not imported */ 320 PyErr_Clear(); 321 return; 322 } 323 result = PyObject_CallMethod(threading, "_after_fork", NULL); 324 if (result == NULL) 325 PyErr_WriteUnraisable(threading); 326 else 327 Py_DECREF(result); 328 Py_DECREF(threading); 325 329 } 326 330 #endif … … 333 337 PyEval_SaveThread(void) 334 338 { 335 336 337 339 PyThreadState *tstate = PyThreadState_Swap(NULL); 340 if (tstate == NULL) 341 Py_FatalError("PyEval_SaveThread: NULL tstate"); 338 342 #ifdef WITH_THREAD 339 340 343 if (interpreter_lock) 344 PyThread_release_lock(interpreter_lock); 341 345 #endif 342 346 return tstate; 343 347 } 344 348 … … 346 350 PyEval_RestoreThread(PyThreadState *tstate) 347 351 { 348 349 352 if (tstate == NULL) 353 Py_FatalError("PyEval_RestoreThread: NULL tstate"); 350 354 #ifdef WITH_THREAD 351 352 353 354 355 355 if (interpreter_lock) { 356 int err = errno; 357 PyThread_acquire_lock(interpreter_lock, 1); 358 errno = err; 359 } 356 360 #endif 357 361 PyThreadState_Swap(tstate); 358 362 } 359 363 … … 376 380 Any thread can schedule pending calls, but only the main thread 377 381 will execute them. 382 There is no facility to schedule calls to a particular thread, but 383 that should be easy to change, should that ever be required. In 384 that case, the static variables here should go into the python 385 threadstate. 378 386 #endif 379 380 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE! 387 */ 388 389 #ifdef WITH_THREAD 390 391 /* The WITH_THREAD implementation is thread-safe. It allows 392 scheduling to be made from any thread, and even from an executing 393 callback. 394 */ 395 396 #define NPENDINGCALLS 32 397 static struct { 398 int (*func)(void *); 399 void *arg; 400 } pendingcalls[NPENDINGCALLS]; 401 static int pendingfirst = 0; 402 static int pendinglast = 0; 403 static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */ 404 static char pendingbusy = 0; 405 406 int 407 Py_AddPendingCall(int (*func)(void *), void *arg) 408 { 409 int i, j, result=0; 410 PyThread_type_lock lock = pending_lock; 411 412 /* try a few times for the lock. Since this mechanism is used 413 * for signal handling (on the main thread), there is a (slim) 414 * chance that a signal is delivered on the same thread while we 415 * hold the lock during the Py_MakePendingCalls() function. 416 * This avoids a deadlock in that case. 417 * Note that signals can be delivered on any thread. In particular, 418 * on Windows, a SIGINT is delivered on a system-created worker 419 * thread. 420 * We also check for lock being NULL, in the unlikely case that 421 * this function is called before any bytecode evaluation takes place. 422 */ 423 if (lock != NULL) { 424 for (i = 0; i<100; i++) { 425 if (PyThread_acquire_lock(lock, NOWAIT_LOCK)) 426 break; 427 } 428 if (i == 100) 429 return -1; 430 } 431 432 i = pendinglast; 433 j = (i + 1) % NPENDINGCALLS; 434 if (j == pendingfirst) { 435 result = -1; /* Queue full */ 436 } else { 437 pendingcalls[i].func = func; 438 pendingcalls[i].arg = arg; 439 pendinglast = j; 440 } 441 /* signal main loop */ 442 _Py_Ticker = 0; 443 pendingcalls_to_do = 1; 444 if (lock != NULL) 445 PyThread_release_lock(lock); 446 return result; 447 } 448 449 int 450 Py_MakePendingCalls(void) 451 { 452 int i; 453 int r = 0; 454 455 if (!pending_lock) { 456 /* initial allocation of the lock */ 457 pending_lock = PyThread_allocate_lock(); 458 if (pending_lock == NULL) 459 return -1; 460 } 461 462 /* only service pending calls on main thread */ 463 if (main_thread && PyThread_get_thread_ident() != main_thread) 464 return 0; 465 /* don't perform recursive pending calls */ 466 if (pendingbusy) 467 return 0; 468 pendingbusy = 1; 469 /* perform a bounded number of calls, in case of recursion */ 470 for (i=0; i<NPENDINGCALLS; i++) { 471 int j; 472 int (*func)(void *); 473 void *arg = NULL; 474 475 /* pop one item off the queue while holding the lock */ 476 PyThread_acquire_lock(pending_lock, WAIT_LOCK); 477 j = pendingfirst; 478 if (j == pendinglast) { 479 func = NULL; /* Queue empty */ 480 } else { 481 func = pendingcalls[j].func; 482 arg = pendingcalls[j].arg; 483 pendingfirst = (j + 1) % NPENDINGCALLS; 484 } 485 pendingcalls_to_do = pendingfirst != pendinglast; 486 PyThread_release_lock(pending_lock); 487 /* having released the lock, perform the callback */ 488 if (func == NULL) 489 break; 490 r = func(arg); 491 if (r) 492 break; 493 } 494 pendingbusy = 0; 495 return r; 496 } 497 498 #else /* if ! defined WITH_THREAD */ 499 500 /* 501 WARNING! ASYNCHRONOUSLY EXECUTING CODE! 502 This code is used for signal handling in python that isn't built 503 with WITH_THREAD. 504 Don't use this implementation when Py_AddPendingCalls() can happen 505 on a different thread! 506 381 507 There are two possible race conditions: 382 (1) nested asynchronous registry calls; 383 (2) registry calls made while pending calls are being processed. 384 While (1) is very unlikely, (2) is a real possibility. 508 (1) nested asynchronous calls to Py_AddPendingCall() 509 (2) AddPendingCall() calls made while pending calls are being processed. 510 511 (1) is very unlikely because typically signal delivery 512 is blocked during signal handling. So it should be impossible. 513 (2) is a real possibility. 385 514 The current code is safe against (2), but not against (1). 386 515 The safety against (2) is derived from the fact that only one 387 thread (the main thread) ever takes things out of the queue. 388 389 XXX Darn! With the advent of thread state, we should have an array 390 of pending calls per thread in the thread state! Later... 516 thread is present, interrupted by signals, and that the critical 517 section is protected with the "busy" variable. On Windows, which 518 delivers SIGINT on a system thread, this does not hold and therefore 519 Windows really shouldn't use this version. 520 The two threads could theoretically wiggle around the "busy" variable. 391 521 */ 392 522 393 523 #define NPENDINGCALLS 32 394 524 static struct { 395 396 525 int (*func)(void *); 526 void *arg; 397 527 } pendingcalls[NPENDINGCALLS]; 398 528 static volatile int pendingfirst = 0; 399 529 static volatile int pendinglast = 0; 400 static volatile int things_to_do = 0;530 static volatile int pendingcalls_to_do = 0; 401 531 402 532 int 403 533 Py_AddPendingCall(int (*func)(void *), void *arg) 404 534 { 405 static volatile int busy = 0; 406 int i, j; 407 /* XXX Begin critical section */ 408 /* XXX If you want this to be safe against nested 409 XXX asynchronous calls, you'll have to work harder! */ 410 if (busy) 411 return -1; 412 busy = 1; 413 i = pendinglast; 414 j = (i + 1) % NPENDINGCALLS; 415 if (j == pendingfirst) { 416 busy = 0; 417 return -1; /* Queue full */ 418 } 419 pendingcalls[i].func = func; 420 pendingcalls[i].arg = arg; 421 pendinglast = j; 422 423 _Py_Ticker = 0; 424 things_to_do = 1; /* Signal main loop */ 425 busy = 0; 426 /* XXX End critical section */ 427 return 0; 535 static volatile int busy = 0; 536 int i, j; 537 /* XXX Begin critical section */ 538 if (busy) 539 return -1; 540 busy = 1; 541 i = pendinglast; 542 j = (i + 1) % NPENDINGCALLS; 543 if (j == pendingfirst) { 544 busy = 0; 545 return -1; /* Queue full */ 546 } 547 pendingcalls[i].func = func; 548 pendingcalls[i].arg = arg; 549 pendinglast = j; 550 551 _Py_Ticker = 0; 552 pendingcalls_to_do = 1; /* Signal main loop */ 553 busy = 0; 554 /* XXX End critical section */ 555 return 0; 428 556 } 429 557 … … 431 559 Py_MakePendingCalls(void) 432 560 { 433 static int busy = 0; 434 #ifdef WITH_THREAD 435 if (main_thread && PyThread_get_thread_ident() != main_thread) 436 return 0; 437 #endif 438 if (busy) 439 return 0; 440 busy = 1; 441 things_to_do = 0; 442 for (;;) { 443 int i; 444 int (*func)(void *); 445 void *arg; 446 i = pendingfirst; 447 if (i == pendinglast) 448 break; /* Queue empty */ 449 func = pendingcalls[i].func; 450 arg = pendingcalls[i].arg; 451 pendingfirst = (i + 1) % NPENDINGCALLS; 452 if (func(arg) < 0) { 453 busy = 0; 454 things_to_do = 1; /* We're not done yet */ 455 return -1; 456 } 457 } 458 busy = 0; 459 return 0; 460 } 561 static int busy = 0; 562 if (busy) 563 return 0; 564 busy = 1; 565 pendingcalls_to_do = 0; 566 for (;;) { 567 int i; 568 int (*func)(void *); 569 void *arg; 570 i = pendingfirst; 571 if (i == pendinglast) 572 break; /* Queue empty */ 573 func = pendingcalls[i].func; 574 arg = pendingcalls[i].arg; 575 pendingfirst = (i + 1) % NPENDINGCALLS; 576 if (func(arg) < 0) { 577 busy = 0; 578 pendingcalls_to_do = 1; /* We're not done yet */ 579 return -1; 580 } 581 } 582 busy = 0; 583 return 0; 584 } 585 586 #endif /* WITH_THREAD */ 461 587 462 588 … … 472 598 Py_GetRecursionLimit(void) 473 599 { 474 600 return recursion_limit; 475 601 } 476 602 … … 478 604 Py_SetRecursionLimit(int new_limit) 479 605 { 480 481 606 recursion_limit = new_limit; 607 _Py_CheckRecursionLimit = recursion_limit; 482 608 } 483 609 … … 490 616 _Py_CheckRecursiveCall(char *where) 491 617 { 492 618 PyThreadState *tstate = PyThreadState_GET(); 493 619 494 620 #ifdef USE_STACKCHECK 495 496 497 498 499 621 if (PyOS_CheckStack()) { 622 --tstate->recursion_depth; 623 PyErr_SetString(PyExc_MemoryError, "Stack overflow"); 624 return -1; 625 } 500 626 #endif 501 502 503 504 505 506 507 508 509 627 if (tstate->recursion_depth > recursion_limit) { 628 --tstate->recursion_depth; 629 PyErr_Format(PyExc_RuntimeError, 630 "maximum recursion depth exceeded%s", 631 where); 632 return -1; 633 } 634 _Py_CheckRecursionLimit = recursion_limit; 635 return 0; 510 636 } 511 637 512 638 /* Status code for main loop (reason for stack unwind) */ 513 639 enum why_code { 514 WHY_NOT = 0x0001,/* No error */515 WHY_EXCEPTION = 0x0002,/* Exception occurred */516 WHY_RERAISE = 0x0004,/* Exception re-raised by 'finally' */517 WHY_RETURN = 0x0008,/* 'return' statement */518 WHY_BREAK = 0x0010,/* 'break' statement */519 WHY_CONTINUE = 0x0020,/* 'continue' statement */520 WHY_YIELD = 0x0040/* 'yield' operator */640 WHY_NOT = 0x0001, /* No error */ 641 WHY_EXCEPTION = 0x0002, /* Exception occurred */ 642 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */ 643 WHY_RETURN = 0x0008, /* 'return' statement */ 644 WHY_BREAK = 0x0010, /* 'break' statement */ 645 WHY_CONTINUE = 0x0020, /* 'continue' statement */ 646 WHY_YIELD = 0x0040 /* 'yield' operator */ 521 647 }; 522 648 … … 534 660 per thread, now just a pair o' globals */ 535 661 int _Py_CheckInterval = 100; 536 volatile int _Py_Ticker = 100;662 volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */ 537 663 538 664 PyObject * 539 665 PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals) 540 666 { 541 542 543 544 545 546 667 return PyEval_EvalCodeEx(co, 668 globals, locals, 669 (PyObject **)NULL, 0, 670 (PyObject **)NULL, 0, 671 (PyObject **)NULL, 0, 672 NULL); 547 673 } 548 674 … … 552 678 PyObject * 553 679 PyEval_EvalFrame(PyFrameObject *f) { 554 555 556 557 680 /* This is for backward compatibility with extension modules that 681 used this API; core interpreter code should call 682 PyEval_EvalFrameEx() */ 683 return PyEval_EvalFrameEx(f, 0); 558 684 } 559 685 … … 562 688 { 563 689 #ifdef DXPAIRS 564 690 int lastopcode = 0; 565 691 #endif 566 567 568 register int opcode;/* Current opcode */569 register int oparg;/* Current opcode argument, if any */570 571 register int err;/* Error status -- nonzero if error */572 register PyObject *x;/* Result object -- NULL if error */573 register PyObject *v;/* Temporary objects popped off stack */574 575 576 577 578 579 PyObject *retval = NULL;/* Return value */580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 692 register PyObject **stack_pointer; /* Next free slot in value stack */ 693 register unsigned char *next_instr; 694 register int opcode; /* Current opcode */ 695 register int oparg; /* Current opcode argument, if any */ 696 register enum why_code why; /* Reason for block stack unwind */ 697 register int err; /* Error status -- nonzero if error */ 698 register PyObject *x; /* Result object -- NULL if error */ 699 register PyObject *v; /* Temporary objects popped off stack */ 700 register PyObject *w; 701 register PyObject *u; 702 register PyObject *t; 703 register PyObject *stream = NULL; /* for PRINT opcodes */ 704 register PyObject **fastlocals, **freevars; 705 PyObject *retval = NULL; /* Return value */ 706 PyThreadState *tstate = PyThreadState_GET(); 707 PyCodeObject *co; 708 709 /* when tracing we set things up so that 710 711 not (instr_lb <= current_bytecode_offset < instr_ub) 712 713 is true when the line being executed has changed. The 714 initial values are such as to make this false the first 715 time it is tested. */ 716 int instr_ub = -1, instr_lb = 0, instr_prev = -1; 717 718 unsigned char *first_instr; 719 PyObject *names; 720 PyObject *consts; 595 721 #if defined(Py_DEBUG) || defined(LLTRACE) 596 597 722 /* Make it easier to find out where we are with a debugger */ 723 char *filename; 598 724 #endif 599 725 … … 635 761 636 762 */ 637 638 639 640 641 642 643 644 645 646 763 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0; 764 int ticked = 0; 765 766 READ_TIMESTAMP(inst0); 767 READ_TIMESTAMP(inst1); 768 READ_TIMESTAMP(loop0); 769 READ_TIMESTAMP(loop1); 770 771 /* shut up the compiler */ 772 opcode = 0; 647 773 #endif 648 774 649 775 /* Code access macros */ 650 776 651 #define INSTR_OFFSET() 652 #define NEXTOP() 653 #define NEXTARG() 654 #define PEEKARG() 655 #define JUMPTO(x) 656 #define JUMPBY(x) 777 #define INSTR_OFFSET() ((int)(next_instr - first_instr)) 778 #define NEXTOP() (*next_instr++) 779 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2]) 780 #define PEEKARG() ((next_instr[2]<<8) + next_instr[1]) 781 #define JUMPTO(x) (next_instr = first_instr + (x)) 782 #define JUMPBY(x) (next_instr += (x)) 657 783 658 784 /* OpCode prediction macros 659 660 661 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, 662 those opcodes are often followed by a POP_TOP.663 664 665 666 667 668 669 670 671 672 785 Some opcodes tend to come in pairs thus making it possible to 786 predict the second code when the first is run. For example, 787 GET_ITER is often followed by FOR_ITER. And FOR_ITER is often 788 followed by STORE_FAST or UNPACK_SEQUENCE. 789 790 Verifying the prediction costs a single high-speed test of a register 791 variable against a constant. If the pairing was good, then the 792 processor's own internal branch predication has a high likelihood of 793 success, resulting in a nearly zero-overhead transition to the 794 next opcode. A successful prediction saves a trip through the eval-loop 795 including its two unpredictable branches, the HAS_ARG test and the 796 switch-case. Combined with the processor's internal branch prediction, 797 a successful PREDICT has the effect of making the two opcodes run as if 798 they were a single new opcode with the bodies combined. 673 799 674 800 If collecting opcode statistics, your choices are to either keep the 675 676 677 801 predictions turned-on and interpret the results as if some opcodes 802 had been combined or turn-off predictions so that the opcode frequency 803 counter updates for both opcodes. 678 804 */ 679 805 680 806 #ifdef DYNAMIC_EXECUTION_PROFILE 681 #define PREDICT(op) 807 #define PREDICT(op) if (0) goto PRED_##op 682 808 #else 683 #define PREDICT(op) 809 #define PREDICT(op) if (*next_instr == op) goto PRED_##op 684 810 #endif 685 811 686 #define PREDICTED(op) 687 #define PREDICTED_WITH_ARG(op) 812 #define PREDICTED(op) PRED_##op: next_instr++ 813 #define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3 688 814 689 815 /* Stack manipulation macros */ … … 691 817 /* The stack can grow at most MAXINT deep, as co_nlocals and 692 818 co_stacksize are ints. */ 693 #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) 694 #define EMPTY() (STACK_LEVEL() == 0) 695 #define TOP() (stack_pointer[-1]) 696 #define SECOND() (stack_pointer[-2]) 697 #define THIRD() (stack_pointer[-3]) 698 #define FOURTH() (stack_pointer[-4]) 699 #define SET_TOP(v) (stack_pointer[-1] = (v)) 700 #define SET_SECOND(v) (stack_pointer[-2] = (v)) 701 #define SET_THIRD(v) (stack_pointer[-3] = (v)) 702 #define SET_FOURTH(v) (stack_pointer[-4] = (v)) 703 #define BASIC_STACKADJ(n) (stack_pointer += n) 704 #define BASIC_PUSH(v) (*stack_pointer++ = (v)) 705 #define BASIC_POP() (*--stack_pointer) 819 #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) 820 #define EMPTY() (STACK_LEVEL() == 0) 821 #define TOP() (stack_pointer[-1]) 822 #define SECOND() (stack_pointer[-2]) 823 #define THIRD() (stack_pointer[-3]) 824 #define FOURTH() (stack_pointer[-4]) 825 #define PEEK(n) (stack_pointer[-(n)]) 826 #define SET_TOP(v) (stack_pointer[-1] = (v)) 827 #define SET_SECOND(v) (stack_pointer[-2] = (v)) 828 #define SET_THIRD(v) (stack_pointer[-3] = (v)) 829 #define SET_FOURTH(v) (stack_pointer[-4] = (v)) 830 #define SET_VALUE(n, v) (stack_pointer[-(n)] = (v)) 831 #define BASIC_STACKADJ(n) (stack_pointer += n) 832 #define BASIC_PUSH(v) (*stack_pointer++ = (v)) 833 #define BASIC_POP() (*--stack_pointer) 706 834 707 835 #ifdef LLTRACE 708 #define PUSH(v) 709 710 711 #define POP() 712 713 #define STACKADJ(n) 714 715 836 #define PUSH(v) { (void)(BASIC_PUSH(v), \ 837 lltrace && prtrace(TOP(), "push")); \ 838 assert(STACK_LEVEL() <= co->co_stacksize); } 839 #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \ 840 BASIC_POP()) 841 #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \ 842 lltrace && prtrace(TOP(), "stackadj")); \ 843 assert(STACK_LEVEL() <= co->co_stacksize); } 716 844 #define EXT_POP(STACK_POINTER) ((void)(lltrace && \ 717 718 845 prtrace((STACK_POINTER)[-1], "ext_pop")), \ 846 *--(STACK_POINTER)) 719 847 #else 720 #define PUSH(v) 721 #define POP() 722 #define STACKADJ(n) 848 #define PUSH(v) BASIC_PUSH(v) 849 #define POP() BASIC_POP() 850 #define STACKADJ(n) BASIC_STACKADJ(n) 723 851 #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) 724 852 #endif … … 726 854 /* Local variable macros */ 727 855 728 #define GETLOCAL(i) 856 #define GETLOCAL(i) (fastlocals[i]) 729 857 730 858 /* The SETLOCAL() macro must not DECREF the local variable in-place and … … 734 862 accessed by other code (e.g. a __del__ method or gc.collect()) and the 735 863 variable would be pointing to already-freed memory. */ 736 #define SETLOCAL(i, value) 737 864 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ 865 GETLOCAL(i) = value; \ 738 866 Py_XDECREF(tmp); } while (0) 739 867 740 868 /* Start of code */ 741 869 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 f->f_stacktop = NULL;/* remains NULL unless yield suspends frame */870 if (f == NULL) 871 return NULL; 872 873 /* push frame */ 874 if (Py_EnterRecursiveCall("")) 875 return NULL; 876 877 tstate->frame = f; 878 879 if (tstate->use_tracing) { 880 if (tstate->c_tracefunc != NULL) { 881 /* tstate->c_tracefunc, if defined, is a 882 function that will be called on *every* entry 883 to a code block. Its return value, if not 884 None, is a function that will be called at 885 the start of each executed line of code. 886 (Actually, the function must return itself 887 in order to continue tracing.) The trace 888 functions are called with three arguments: 889 a pointer to the current frame, a string 890 indicating why the function is called, and 891 an argument which depends on the situation. 892 The global trace function is also called 893 whenever an exception is detected. */ 894 if (call_trace_protected(tstate->c_tracefunc, 895 tstate->c_traceobj, 896 f, PyTrace_CALL, Py_None)) { 897 /* Trace function raised an error */ 898 goto exit_eval_frame; 899 } 900 } 901 if (tstate->c_profilefunc != NULL) { 902 /* Similar for c_profilefunc, except it needn't 903 return itself and isn't called for "line" events */ 904 if (call_trace_protected(tstate->c_profilefunc, 905 tstate->c_profileobj, 906 f, PyTrace_CALL, Py_None)) { 907 /* Profile function raised an error */ 908 goto exit_eval_frame; 909 } 910 } 911 } 912 913 co = f->f_code; 914 names = co->co_names; 915 consts = co->co_consts; 916 fastlocals = f->f_localsplus; 917 freevars = f->f_localsplus + co->co_nlocals; 918 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code); 919 /* An explanation is in order for the next line. 920 921 f->f_lasti now refers to the index of the last instruction 922 executed. You might think this was obvious from the name, but 923 this wasn't always true before 2.3! PyFrame_New now sets 924 f->f_lasti to -1 (i.e. the index *before* the first instruction) 925 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this 926 does work. Promise. 927 928 When the PREDICT() macros are enabled, some opcode pairs follow in 929 direct succession without updating f->f_lasti. A successful 930 prediction effectively links the two codes together as if they 931 were a single new opcode; accordingly,f->f_lasti will point to 932 the first code in the pair (for instance, GET_ITER followed by 933 FOR_ITER is effectively a single opcode and f->f_lasti will point 934 at to the beginning of the combined pair.) 935 */ 936 next_instr = first_instr + f->f_lasti + 1; 937 stack_pointer = f->f_stacktop; 938 assert(stack_pointer != NULL); 939 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ 812 940 813 941 #ifdef LLTRACE 814 942 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL; 815 943 #endif 816 944 #if defined(Py_DEBUG) || defined(LLTRACE) 817 945 filename = PyString_AsString(co->co_filename); 818 946 #endif 819 947 820 821 822 x = Py_None;/* Not a reference, just anything non-NULL */823 824 825 826 827 828 829 830 948 why = WHY_NOT; 949 err = 0; 950 x = Py_None; /* Not a reference, just anything non-NULL */ 951 w = NULL; 952 953 if (throwflag) { /* support for generator.throw() */ 954 why = WHY_EXCEPTION; 955 goto on_error; 956 } 957 958 for (;;) { 831 959 #ifdef WITH_TSC 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 960 if (inst1 == 0) { 961 /* Almost surely, the opcode executed a break 962 or a continue, preventing inst1 from being set 963 on the way out of the loop. 964 */ 965 READ_TIMESTAMP(inst1); 966 loop1 = inst1; 967 } 968 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1, 969 intr0, intr1); 970 ticked = 0; 971 inst1 = 0; 972 intr0 = 0; 973 intr1 = 0; 974 READ_TIMESTAMP(loop0); 847 975 #endif 848 849 850 851 852 853 854 ``things_to_do'' is set, i.e. when an asynchronous855 856 857 858 859 860 861 862 a try: finally: block uninterruptable. */863 864 865 866 976 assert(stack_pointer >= f->f_valuestack); /* else underflow */ 977 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ 978 979 /* Do periodic things. Doing this every time through 980 the loop would add too much overhead, so we do it 981 only every Nth instruction. We also do it if 982 ``pendingcalls_to_do'' is set, i.e. when an asynchronous 983 event needs attention (e.g. a signal handler or 984 async I/O handler); see Py_AddPendingCall() and 985 Py_MakePendingCalls() above. */ 986 987 if (--_Py_Ticker < 0) { 988 if (*next_instr == SETUP_FINALLY) { 989 /* Make the last opcode before 990 a try: finally: block uninterruptible. */ 991 goto fast_next_opcode; 992 } 993 _Py_Ticker = _Py_CheckInterval; 994 tstate->tick_counter++; 867 995 #ifdef WITH_TSC 868 996 ticked = 1; 869 997 #endif 870 if (things_to_do) {871 872 873 874 875 if (things_to_do)876 877 878 879 880 881 998 if (pendingcalls_to_do) { 999 if (Py_MakePendingCalls() < 0) { 1000 why = WHY_EXCEPTION; 1001 goto on_error; 1002 } 1003 if (pendingcalls_to_do) 1004 /* MakePendingCalls() didn't succeed. 1005 Force early re-execution of this 1006 "periodic" code, possibly after 1007 a thread switch */ 1008 _Py_Ticker = 0; 1009 } 882 1010 #ifdef WITH_THREAD 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 1011 if (interpreter_lock) { 1012 /* Give another thread a chance */ 1013 1014 if (PyThreadState_Swap(NULL) != tstate) 1015 Py_FatalError("ceval: tstate mix-up"); 1016 PyThread_release_lock(interpreter_lock); 1017 1018 /* Other threads may run now */ 1019 1020 PyThread_acquire_lock(interpreter_lock, 1); 1021 if (PyThreadState_Swap(tstate) != NULL) 1022 Py_FatalError("ceval: orphan tstate"); 1023 1024 /* Check for thread interrupts */ 1025 1026 if (tstate->async_exc != NULL) { 1027 x = tstate->async_exc; 1028 tstate->async_exc = NULL; 1029 PyErr_SetNone(x); 1030 Py_DECREF(x); 1031 why = WHY_EXCEPTION; 1032 goto on_error; 1033 } 1034 } 907 1035 #endif 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 1036 } 1037 1038 fast_next_opcode: 1039 f->f_lasti = INSTR_OFFSET(); 1040 1041 /* line-by-line tracing support */ 1042 1043 if (_Py_TracingPossible && 1044 tstate->c_tracefunc != NULL && !tstate->tracing) { 1045 /* see maybe_call_line_trace 1046 for expository comments */ 1047 f->f_stacktop = stack_pointer; 1048 1049 err = maybe_call_line_trace(tstate->c_tracefunc, 1050 tstate->c_traceobj, 1051 f, &instr_lb, &instr_ub, 1052 &instr_prev); 1053 /* Reload possibly changed frame fields */ 1054 JUMPTO(f->f_lasti); 1055 if (f->f_stacktop != NULL) { 1056 stack_pointer = f->f_stacktop; 1057 f->f_stacktop = NULL; 1058 } 1059 if (err) { 1060 /* trace function raised an exception */ 1061 goto on_error; 1062 } 1063 } 1064 1065 /* Extract opcode and argument */ 1066 1067 opcode = NEXTOP(); 1068 oparg = 0; /* allows oparg to be stored in a register because 1069 it doesn't have to be remembered across a full loop */ 1070 if (HAS_ARG(opcode)) 1071 oparg = NEXTARG(); 1072 dispatch_opcode: 945 1073 #ifdef DYNAMIC_EXECUTION_PROFILE 946 1074 #ifdef DXPAIRS 947 948 1075 dxpairs[lastopcode][opcode]++; 1076 lastopcode = opcode; 949 1077 #endif 950 1078 dxp[opcode]++; 951 1079 #endif 952 1080 953 1081 #ifdef LLTRACE 954 955 956 957 958 959 960 961 962 963 964 965 1082 /* Instruction tracing */ 1083 1084 if (lltrace) { 1085 if (HAS_ARG(opcode)) { 1086 printf("%d: %d, %d\n", 1087 f->f_lasti, opcode, oparg); 1088 } 1089 else { 1090 printf("%d: %d\n", 1091 f->f_lasti, opcode); 1092 } 1093 } 966 1094 #endif 967 1095 968 /* Main switch on opcode */ 969 READ_TIMESTAMP(inst0); 970 971 switch (opcode) { 972 973 /* BEWARE! 974 It is essential that any operation that fails sets either 975 x to NULL, err to nonzero, or why to anything but WHY_NOT, 976 and that no operation that succeeds does this! */ 977 978 /* case STOP_CODE: this is an error! */ 979 980 case NOP: 981 goto fast_next_opcode; 982 983 case LOAD_FAST: 984 x = GETLOCAL(oparg); 985 if (x != NULL) { 986 Py_INCREF(x); 987 PUSH(x); 988 goto fast_next_opcode; 989 } 990 format_exc_check_arg(PyExc_UnboundLocalError, 991 UNBOUNDLOCAL_ERROR_MSG, 992 PyTuple_GetItem(co->co_varnames, oparg)); 993 break; 994 995 case LOAD_CONST: 996 x = GETITEM(consts, oparg); 997 Py_INCREF(x); 998 PUSH(x); 999 goto fast_next_opcode; 1000 1001 PREDICTED_WITH_ARG(STORE_FAST); 1002 case STORE_FAST: 1003 v = POP(); 1004 SETLOCAL(oparg, v); 1005 goto fast_next_opcode; 1006 1007 PREDICTED(POP_TOP); 1008 case POP_TOP: 1009 v = POP(); 1010 Py_DECREF(v); 1011 goto fast_next_opcode; 1012 1013 case ROT_TWO: 1014 v = TOP(); 1015 w = SECOND(); 1016 SET_TOP(w); 1017 SET_SECOND(v); 1018 goto fast_next_opcode; 1019 1020 case ROT_THREE: 1021 v = TOP(); 1022 w = SECOND(); 1023 x = THIRD(); 1024 SET_TOP(w); 1025 SET_SECOND(x); 1026 SET_THIRD(v); 1027 goto fast_next_opcode; 1028 1029 case ROT_FOUR: 1030 u = TOP(); 1031 v = SECOND(); 1032 w = THIRD(); 1033 x = FOURTH(); 1034 SET_TOP(v); 1035 SET_SECOND(w); 1036 SET_THIRD(x); 1037 SET_FOURTH(u); 1038 goto fast_next_opcode; 1039 1040 case DUP_TOP: 1041 v = TOP(); 1042 Py_INCREF(v); 1043 PUSH(v); 1044 goto fast_next_opcode; 1045 1046 case DUP_TOPX: 1047 if (oparg == 2) { 1048 x = TOP(); 1049 Py_INCREF(x); 1050 w = SECOND(); 1051 Py_INCREF(w); 1052 STACKADJ(2); 1053 SET_TOP(x); 1054 SET_SECOND(w); 1055 goto fast_next_opcode; 1056 } else if (oparg == 3) { 1057 x = TOP(); 1058 Py_INCREF(x); 1059 w = SECOND(); 1060 Py_INCREF(w); 1061 v = THIRD(); 1062 Py_INCREF(v); 1063 STACKADJ(3); 1064 SET_TOP(x); 1065 SET_SECOND(w); 1066 SET_THIRD(v); 1067 goto fast_next_opcode; 1068 } 1069 Py_FatalError("invalid argument to DUP_TOPX" 1070 " (bytecode corruption?)"); 1071 /* Never returns, so don't bother to set why. */ 1072 break; 1073 1074 case UNARY_POSITIVE: 1075 v = TOP(); 1076 x = PyNumber_Positive(v); 1077 Py_DECREF(v); 1078 SET_TOP(x); 1079 if (x != NULL) continue; 1080 break; 1081 1082 case UNARY_NEGATIVE: 1083 v = TOP(); 1084 x = PyNumber_Negative(v); 1085 Py_DECREF(v); 1086 SET_TOP(x); 1087 if (x != NULL) continue; 1088 break; 1089 1090 case UNARY_NOT: 1091 v = TOP(); 1092 err = PyObject_IsTrue(v); 1093 Py_DECREF(v); 1094 if (err == 0) { 1095 Py_INCREF(Py_True); 1096 SET_TOP(Py_True); 1097 continue; 1098 } 1099 else if (err > 0) { 1100 Py_INCREF(Py_False); 1101 SET_TOP(Py_False); 1102 err = 0; 1103 continue; 1104 } 1105 STACKADJ(-1); 1106 break; 1107 1108 case UNARY_CONVERT: 1109 v = TOP(); 1110 x = PyObject_Repr(v); 1111 Py_DECREF(v); 1112 SET_TOP(x); 1113 if (x != NULL) continue; 1114 break; 1115 1116 case UNARY_INVERT: 1117 v = TOP(); 1118 x = PyNumber_Invert(v); 1119 Py_DECREF(v); 1120 SET_TOP(x); 1121 if (x != NULL) continue; 1122 break; 1123 1124 case BINARY_POWER: 1125 w = POP(); 1126 v = TOP(); 1127 x = PyNumber_Power(v, w, Py_None); 1128 Py_DECREF(v); 1129 Py_DECREF(w); 1130 SET_TOP(x); 1131 if (x != NULL) continue; 1132 break; 1133 1134 case BINARY_MULTIPLY: 1135 w = POP(); 1136 v = TOP(); 1137 x = PyNumber_Multiply(v, w); 1138 Py_DECREF(v); 1139 Py_DECREF(w); 1140 SET_TOP(x); 1141 if (x != NULL) continue; 1142 break; 1143 1144 case BINARY_DIVIDE: 1145 if (!_Py_QnewFlag) { 1146 w = POP(); 1147 v = TOP(); 1148 x = PyNumber_Divide(v, w); 1149 Py_DECREF(v); 1150 Py_DECREF(w); 1151 SET_TOP(x); 1152 if (x != NULL) continue; 1153 break; 1154 } 1155 /* -Qnew is in effect: fall through to 1156 BINARY_TRUE_DIVIDE */ 1157 case BINARY_TRUE_DIVIDE: 1158 w = POP(); 1159 v = TOP(); 1160 x = PyNumber_TrueDivide(v, w); 1161 Py_DECREF(v); 1162 Py_DECREF(w); 1163 SET_TOP(x); 1164 if (x != NULL) continue; 1165 break; 1166 1167 case BINARY_FLOOR_DIVIDE: 1168 w = POP(); 1169 v = TOP(); 1170 x = PyNumber_FloorDivide(v, w); 1171 Py_DECREF(v); 1172 Py_DECREF(w); 1173 SET_TOP(x); 1174 if (x != NULL) continue; 1175 break; 1176 1177 case BINARY_MODULO: 1178 w = POP(); 1179 v = TOP(); 1180 x = PyNumber_Remainder(v, w); 1181 Py_DECREF(v); 1182 Py_DECREF(w); 1183 SET_TOP(x); 1184 if (x != NULL) continue; 1185 break; 1186 1187 case BINARY_ADD: 1188 w = POP(); 1189 v = TOP(); 1190 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { 1191 /* INLINE: int + int */ 1192 register long a, b, i; 1193 a = PyInt_AS_LONG(v); 1194 b = PyInt_AS_LONG(w); 1195 /* cast to avoid undefined behaviour 1196 on overflow */ 1197 i = (long)((unsigned long)a + b); 1198 if ((i^a) < 0 && (i^b) < 0) 1199 goto slow_add; 1200 x = PyInt_FromLong(i); 1201 } 1202 else if (PyString_CheckExact(v) && 1203 PyString_CheckExact(w)) { 1204 x = string_concatenate(v, w, f, next_instr); 1205 /* string_concatenate consumed the ref to v */ 1206 goto skip_decref_vx; 1207 } 1208 else { 1209 slow_add: 1210 x = PyNumber_Add(v, w); 1211 } 1212 Py_DECREF(v); 1213 skip_decref_vx: 1214 Py_DECREF(w); 1215 SET_TOP(x); 1216 if (x != NULL) continue; 1217 break; 1218 1219 case BINARY_SUBTRACT: 1220 w = POP(); 1221 v = TOP(); 1222 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { 1223 /* INLINE: int - int */ 1224 register long a, b, i; 1225 a = PyInt_AS_LONG(v); 1226 b = PyInt_AS_LONG(w); 1227 /* cast to avoid undefined behaviour 1228 on overflow */ 1229 i = (long)((unsigned long)a - b); 1230 if ((i^a) < 0 && (i^~b) < 0) 1231 goto slow_sub; 1232 x = PyInt_FromLong(i); 1233 } 1234 else { 1235 slow_sub: 1236 x = PyNumber_Subtract(v, w); 1237 } 1238 Py_DECREF(v); 1239 Py_DECREF(w); 1240 SET_TOP(x); 1241 if (x != NULL) continue; 1242 break; 1243 1244 case BINARY_SUBSCR: 1245 w = POP(); 1246 v = TOP(); 1247 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) { 1248 /* INLINE: list[int] */ 1249 Py_ssize_t i = PyInt_AsSsize_t(w); 1250 if (i < 0) 1251 i += PyList_GET_SIZE(v); 1252 if (i >= 0 && i < PyList_GET_SIZE(v)) { 1253 x = PyList_GET_ITEM(v, i); 1254 Py_INCREF(x); 1255 } 1256 else 1257 goto slow_get; 1258 } 1259 else 1260 slow_get: 1261 x = PyObject_GetItem(v, w); 1262 Py_DECREF(v); 1263 Py_DECREF(w); 1264 SET_TOP(x); 1265 if (x != NULL) continue; 1266 break; 1267 1268 case BINARY_LSHIFT: 1269 w = POP(); 1270 v = TOP(); 1271 x = PyNumber_Lshift(v, w); 1272 Py_DECREF(v); 1273 Py_DECREF(w); 1274 SET_TOP(x); 1275 if (x != NULL) continue; 1276 break; 1277 1278 case BINARY_RSHIFT: 1279 w = POP(); 1280 v = TOP(); 1281 x = PyNumber_Rshift(v, w); 1282 Py_DECREF(v); 1283 Py_DECREF(w); 1284 SET_TOP(x); 1285 if (x != NULL) continue; 1286 break; 1287 1288 case BINARY_AND: 1289 w = POP(); 1290 v = TOP(); 1291 x = PyNumber_And(v, w); 1292 Py_DECREF(v); 1293 Py_DECREF(w); 1294 SET_TOP(x); 1295 if (x != NULL) continue; 1296 break; 1297 1298 case BINARY_XOR: 1299 w = POP(); 1300 v = TOP(); 1301 x = PyNumber_Xor(v, w); 1302 Py_DECREF(v); 1303 Py_DECREF(w); 1304 SET_TOP(x); 1305 if (x != NULL) continue; 1306 break; 1307 1308 case BINARY_OR: 1309 w = POP(); 1310 v = TOP(); 1311 x = PyNumber_Or(v, w); 1312 Py_DECREF(v); 1313 Py_DECREF(w); 1314 SET_TOP(x); 1315 if (x != NULL) continue; 1316 break; 1317 1318 case LIST_APPEND: 1319 w = POP(); 1320 v = POP(); 1321 err = PyList_Append(v, w); 1322 Py_DECREF(v); 1323 Py_DECREF(w); 1324 if (err == 0) { 1325 PREDICT(JUMP_ABSOLUTE); 1326 continue; 1327 } 1328 break; 1329 1330 case INPLACE_POWER: 1331 w = POP(); 1332 v = TOP(); 1333 x = PyNumber_InPlacePower(v, w, Py_None); 1334 Py_DECREF(v); 1335 Py_DECREF(w); 1336 SET_TOP(x); 1337 if (x != NULL) continue; 1338 break; 1339 1340 case INPLACE_MULTIPLY: 1341 w = POP(); 1342 v = TOP(); 1343 x = PyNumber_InPlaceMultiply(v, w); 1344 Py_DECREF(v); 1345 Py_DECREF(w); 1346 SET_TOP(x); 1347 if (x != NULL) continue; 1348 break; 1349 1350 case INPLACE_DIVIDE: 1351 if (!_Py_QnewFlag) { 1352 w = POP(); 1353 v = TOP(); 1354 x = PyNumber_InPlaceDivide(v, w); 1355 Py_DECREF(v); 1356 Py_DECREF(w); 1357 SET_TOP(x); 1358 if (x != NULL) continue; 1359 break; 1360 } 1361 /* -Qnew is in effect: fall through to 1362 INPLACE_TRUE_DIVIDE */ 1363 case INPLACE_TRUE_DIVIDE: 1364 w = POP(); 1365 v = TOP(); 1366 x = PyNumber_InPlaceTrueDivide(v, w); 1367 Py_DECREF(v); 1368 Py_DECREF(w); 1369 SET_TOP(x); 1370 if (x != NULL) continue; 1371 break; 1372 1373 case INPLACE_FLOOR_DIVIDE: 1374 w = POP(); 1375 v = TOP(); 1376 x = PyNumber_InPlaceFloorDivide(v, w); 1377 Py_DECREF(v); 1378 Py_DECREF(w); 1379 SET_TOP(x); 1380 if (x != NULL) continue; 1381 break; 1382 1383 case INPLACE_MODULO: 1384 w = POP(); 1385 v = TOP(); 1386 x = PyNumber_InPlaceRemainder(v, w); 1387 Py_DECREF(v); 1388 Py_DECREF(w); 1389 SET_TOP(x); 1390 if (x != NULL) continue; 1391 break; 1392 1393 case INPLACE_ADD: 1394 w = POP(); 1395 v = TOP(); 1396 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { 1397 /* INLINE: int + int */ 1398 register long a, b, i; 1399 a = PyInt_AS_LONG(v); 1400 b = PyInt_AS_LONG(w); 1401 i = a + b; 1402 if ((i^a) < 0 && (i^b) < 0) 1403 goto slow_iadd; 1404 x = PyInt_FromLong(i); 1405 } 1406 else if (PyString_CheckExact(v) && 1407 PyString_CheckExact(w)) { 1408 x = string_concatenate(v, w, f, next_instr); 1409 /* string_concatenate consumed the ref to v */ 1410 goto skip_decref_v; 1411 } 1412 else { 1413 slow_iadd: 1414 x = PyNumber_InPlaceAdd(v, w); 1415 } 1416 Py_DECREF(v); 1417 skip_decref_v: 1418 Py_DECREF(w); 1419 SET_TOP(x); 1420 if (x != NULL) continue; 1421 break; 1422 1423 case INPLACE_SUBTRACT: 1424 w = POP(); 1425 v = TOP(); 1426 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { 1427 /* INLINE: int - int */ 1428 register long a, b, i; 1429 a = PyInt_AS_LONG(v); 1430 b = PyInt_AS_LONG(w); 1431 i = a - b; 1432 if ((i^a) < 0 && (i^~b) < 0) 1433 goto slow_isub; 1434 x = PyInt_FromLong(i); 1435 } 1436 else { 1437 slow_isub: 1438 x = PyNumber_InPlaceSubtract(v, w); 1439 } 1440 Py_DECREF(v); 1441 Py_DECREF(w); 1442 SET_TOP(x); 1443 if (x != NULL) continue; 1444 break; 1445 1446 case INPLACE_LSHIFT: 1447 w = POP(); 1448 v = TOP(); 1449 x = PyNumber_InPlaceLshift(v, w); 1450 Py_DECREF(v); 1451 Py_DECREF(w); 1452 SET_TOP(x); 1453 if (x != NULL) continue; 1454 break; 1455 1456 case INPLACE_RSHIFT: 1457 w = POP(); 1458 v = TOP(); 1459 x = PyNumber_InPlaceRshift(v, w); 1460 Py_DECREF(v); 1461 Py_DECREF(w); 1462 SET_TOP(x); 1463 if (x != NULL) continue; 1464 break; 1465 1466 case INPLACE_AND: 1467 w = POP(); 1468 v = TOP(); 1469 x = PyNumber_InPlaceAnd(v, w); 1470 Py_DECREF(v); 1471 Py_DECREF(w); 1472 SET_TOP(x); 1473 if (x != NULL) continue; 1474 break; 1475 1476 case INPLACE_XOR: 1477 w = POP(); 1478 v = TOP(); 1479 x = PyNumber_InPlaceXor(v, w); 1480 Py_DECREF(v); 1481 Py_DECREF(w); 1482 SET_TOP(x); 1483 if (x != NULL) continue; 1484 break; 1485 1486 case INPLACE_OR: 1487 w = POP(); 1488 v = TOP(); 1489 x = PyNumber_InPlaceOr(v, w); 1490 Py_DECREF(v); 1491 Py_DECREF(w); 1492 SET_TOP(x); 1493 if (x != NULL) continue; 1494 break; 1495 1496 case SLICE+0: 1497 case SLICE+1: 1498 case SLICE+2: 1499 case SLICE+3: 1500 if ((opcode-SLICE) & 2) 1501 w = POP(); 1502 else 1503 w = NULL; 1504 if ((opcode-SLICE) & 1) 1505 v = POP(); 1506 else 1507 v = NULL; 1508 u = TOP(); 1509 x = apply_slice(u, v, w); 1510 Py_DECREF(u); 1511 Py_XDECREF(v); 1512 Py_XDECREF(w); 1513 SET_TOP(x); 1514 if (x != NULL) continue; 1515 break; 1516 1517 case STORE_SLICE+0: 1518 case STORE_SLICE+1: 1519 case STORE_SLICE+2: 1520 case STORE_SLICE+3: 1521 if ((opcode-STORE_SLICE) & 2) 1522 w = POP(); 1523 else 1524 w = NULL; 1525 if ((opcode-STORE_SLICE) & 1) 1526 v = POP(); 1527 else 1528 v = NULL; 1529 u = POP(); 1530 t = POP(); 1531 err = assign_slice(u, v, w, t); /* u[v:w] = t */ 1532 Py_DECREF(t); 1533 Py_DECREF(u); 1534 Py_XDECREF(v); 1535 Py_XDECREF(w); 1536 if (err == 0) continue; 1537 break; 1538 1539 case DELETE_SLICE+0: 1540 case DELETE_SLICE+1: 1541 case DELETE_SLICE+2: 1542 case DELETE_SLICE+3: 1543 if ((opcode-DELETE_SLICE) & 2) 1544 w = POP(); 1545 else 1546 w = NULL; 1547 if ((opcode-DELETE_SLICE) & 1) 1548 v = POP(); 1549 else 1550 v = NULL; 1551 u = POP(); 1552 err = assign_slice(u, v, w, (PyObject *)NULL); 1553 /* del u[v:w] */ 1554 Py_DECREF(u); 1555 Py_XDECREF(v); 1556 Py_XDECREF(w); 1557 if (err == 0) continue; 1558 break; 1559 1560 case STORE_SUBSCR: 1561 w = TOP(); 1562 v = SECOND(); 1563 u = THIRD(); 1564 STACKADJ(-3); 1565 /* v[w] = u */ 1566 err = PyObject_SetItem(v, w, u); 1567 Py_DECREF(u); 1568 Py_DECREF(v); 1569 Py_DECREF(w); 1570 if (err == 0) continue; 1571 break; 1572 1573 case DELETE_SUBSCR: 1574 w = TOP(); 1575 v = SECOND(); 1576 STACKADJ(-2); 1577 /* del v[w] */ 1578 err = PyObject_DelItem(v, w); 1579 Py_DECREF(v); 1580 Py_DECREF(w); 1581 if (err == 0) continue; 1582 break; 1583 1584 case PRINT_EXPR: 1585 v = POP(); 1586 w = PySys_GetObject("displayhook"); 1587 if (w == NULL) { 1588 PyErr_SetString(PyExc_RuntimeError, 1589 "lost sys.displayhook"); 1590 err = -1; 1591 x = NULL; 1592 } 1593 if (err == 0) { 1594 x = PyTuple_Pack(1, v); 1595 if (x == NULL) 1596 err = -1; 1597 } 1598 if (err == 0) { 1599 w = PyEval_CallObject(w, x); 1600 Py_XDECREF(w); 1601 if (w == NULL) 1602 err = -1; 1603 } 1604 Py_DECREF(v); 1605 Py_XDECREF(x); 1606 break; 1607 1608 case PRINT_ITEM_TO: 1609 w = stream = POP(); 1610 /* fall through to PRINT_ITEM */ 1611 1612 case PRINT_ITEM: 1613 v = POP(); 1614 if (stream == NULL || stream == Py_None) { 1615 w = PySys_GetObject("stdout"); 1616 if (w == NULL) { 1617 PyErr_SetString(PyExc_RuntimeError, 1618 "lost sys.stdout"); 1619 err = -1; 1620 } 1621 } 1622 /* PyFile_SoftSpace() can exececute arbitrary code 1623 if sys.stdout is an instance with a __getattr__. 1624 If __getattr__ raises an exception, w will 1625 be freed, so we need to prevent that temporarily. */ 1626 Py_XINCREF(w); 1627 if (w != NULL && PyFile_SoftSpace(w, 0)) 1628 err = PyFile_WriteString(" ", w); 1629 if (err == 0) 1630 err = PyFile_WriteObject(v, w, Py_PRINT_RAW); 1631 if (err == 0) { 1632 /* XXX move into writeobject() ? */ 1633 if (PyString_Check(v)) { 1634 char *s = PyString_AS_STRING(v); 1635 Py_ssize_t len = PyString_GET_SIZE(v); 1636 if (len == 0 || 1637 !isspace(Py_CHARMASK(s[len-1])) || 1638 s[len-1] == ' ') 1639 PyFile_SoftSpace(w, 1); 1640 } 1096 /* Main switch on opcode */ 1097 READ_TIMESTAMP(inst0); 1098 1099 switch (opcode) { 1100 1101 /* BEWARE! 1102 It is essential that any operation that fails sets either 1103 x to NULL, err to nonzero, or why to anything but WHY_NOT, 1104 and that no operation that succeeds does this! */ 1105 1106 /* case STOP_CODE: this is an error! */ 1107 1108 case NOP: 1109 goto fast_next_opcode; 1110 1111 case LOAD_FAST: 1112 x = GETLOCAL(oparg); 1113 if (x != NULL) { 1114 Py_INCREF(x); 1115 PUSH(x); 1116 goto fast_next_opcode; 1117 } 1118 format_exc_check_arg(PyExc_UnboundLocalError, 1119 UNBOUNDLOCAL_ERROR_MSG, 1120 PyTuple_GetItem(co->co_varnames, oparg)); 1121 break; 1122 1123 case LOAD_CONST: 1124 x = GETITEM(consts, oparg); 1125 Py_INCREF(x); 1126 PUSH(x); 1127 goto fast_next_opcode; 1128 1129 PREDICTED_WITH_ARG(STORE_FAST); 1130 case STORE_FAST: 1131 v = POP(); 1132 SETLOCAL(oparg, v); 1133 goto fast_next_opcode; 1134 1135 case POP_TOP: 1136 v = POP(); 1137 Py_DECREF(v); 1138 goto fast_next_opcode; 1139 1140 case ROT_TWO: 1141 v = TOP(); 1142 w = SECOND(); 1143 SET_TOP(w); 1144 SET_SECOND(v); 1145 goto fast_next_opcode; 1146 1147 case ROT_THREE: 1148 v = TOP(); 1149 w = SECOND(); 1150 x = THIRD(); 1151 SET_TOP(w); 1152 SET_SECOND(x); 1153 SET_THIRD(v); 1154 goto fast_next_opcode; 1155 1156 case ROT_FOUR: 1157 u = TOP(); 1158 v = SECOND(); 1159 w = THIRD(); 1160 x = FOURTH(); 1161 SET_TOP(v); 1162 SET_SECOND(w); 1163 SET_THIRD(x); 1164 SET_FOURTH(u); 1165 goto fast_next_opcode; 1166 1167 case DUP_TOP: 1168 v = TOP(); 1169 Py_INCREF(v); 1170 PUSH(v); 1171 goto fast_next_opcode; 1172 1173 case DUP_TOPX: 1174 if (oparg == 2) { 1175 x = TOP(); 1176 Py_INCREF(x); 1177 w = SECOND(); 1178 Py_INCREF(w); 1179 STACKADJ(2); 1180 SET_TOP(x); 1181 SET_SECOND(w); 1182 goto fast_next_opcode; 1183 } else if (oparg == 3) { 1184 x = TOP(); 1185 Py_INCREF(x); 1186 w = SECOND(); 1187 Py_INCREF(w); 1188 v = THIRD(); 1189 Py_INCREF(v); 1190 STACKADJ(3); 1191 SET_TOP(x); 1192 SET_SECOND(w); 1193 SET_THIRD(v); 1194 goto fast_next_opcode; 1195 } 1196 Py_FatalError("invalid argument to DUP_TOPX" 1197 " (bytecode corruption?)"); 1198 /* Never returns, so don't bother to set why. */ 1199 break; 1200 1201 case UNARY_POSITIVE: 1202 v = TOP(); 1203 x = PyNumber_Positive(v); 1204 Py_DECREF(v); 1205 SET_TOP(x); 1206 if (x != NULL) continue; 1207 break; 1208 1209 case UNARY_NEGATIVE: 1210 v = TOP(); 1211 x = PyNumber_Negative(v); 1212 Py_DECREF(v); 1213 SET_TOP(x); 1214 if (x != NULL) continue; 1215 break; 1216 1217 case UNARY_NOT: 1218 v = TOP(); 1219 err = PyObject_IsTrue(v); 1220 Py_DECREF(v); 1221 if (err == 0) { 1222 Py_INCREF(Py_True); 1223 SET_TOP(Py_True); 1224 continue; 1225 } 1226 else if (err > 0) { 1227 Py_INCREF(Py_False); 1228 SET_TOP(Py_False); 1229 err = 0; 1230 continue; 1231 } 1232 STACKADJ(-1); 1233 break; 1234 1235 case UNARY_CONVERT: 1236 v = TOP(); 1237 x = PyObject_Repr(v); 1238 Py_DECREF(v); 1239 SET_TOP(x); 1240 if (x != NULL) continue; 1241 break; 1242 1243 case UNARY_INVERT: 1244 v = TOP(); 1245 x = PyNumber_Invert(v); 1246 Py_DECREF(v); 1247 SET_TOP(x); 1248 if (x != NULL) continue; 1249 break; 1250 1251 case BINARY_POWER: 1252 w = POP(); 1253 v = TOP(); 1254 x = PyNumber_Power(v, w, Py_None); 1255 Py_DECREF(v); 1256 Py_DECREF(w); 1257 SET_TOP(x); 1258 if (x != NULL) continue; 1259 break; 1260 1261 case BINARY_MULTIPLY: 1262 w = POP(); 1263 v = TOP(); 1264 x = PyNumber_Multiply(v, w); 1265 Py_DECREF(v); 1266 Py_DECREF(w); 1267 SET_TOP(x); 1268 if (x != NULL) continue; 1269 break; 1270 1271 case BINARY_DIVIDE: 1272 if (!_Py_QnewFlag) { 1273 w = POP(); 1274 v = TOP(); 1275 x = PyNumber_Divide(v, w); 1276 Py_DECREF(v); 1277 Py_DECREF(w); 1278 SET_TOP(x); 1279 if (x != NULL) continue; 1280 break; 1281 } 1282 /* -Qnew is in effect: fall through to 1283 BINARY_TRUE_DIVIDE */ 1284 case BINARY_TRUE_DIVIDE: 1285 w = POP(); 1286 v = TOP(); 1287 x = PyNumber_TrueDivide(v, w); 1288 Py_DECREF(v); 1289 Py_DECREF(w); 1290 SET_TOP(x); 1291 if (x != NULL) continue; 1292 break; 1293 1294 case BINARY_FLOOR_DIVIDE: 1295 w = POP(); 1296 v = TOP(); 1297 x = PyNumber_FloorDivide(v, w); 1298 Py_DECREF(v); 1299 Py_DECREF(w); 1300 SET_TOP(x); 1301 if (x != NULL) continue; 1302 break; 1303 1304 case BINARY_MODULO: 1305 w = POP(); 1306 v = TOP(); 1307 if (PyString_CheckExact(v)) 1308 x = PyString_Format(v, w); 1309 else 1310 x = PyNumber_Remainder(v, w); 1311 Py_DECREF(v); 1312 Py_DECREF(w); 1313 SET_TOP(x); 1314 if (x != NULL) continue; 1315 break; 1316 1317 case BINARY_ADD: 1318 w = POP(); 1319 v = TOP(); 1320 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { 1321 /* INLINE: int + int */ 1322 register long a, b, i; 1323 a = PyInt_AS_LONG(v); 1324 b = PyInt_AS_LONG(w); 1325 /* cast to avoid undefined behaviour 1326 on overflow */ 1327 i = (long)((unsigned long)a + b); 1328 if ((i^a) < 0 && (i^b) < 0) 1329 goto slow_add; 1330 x = PyInt_FromLong(i); 1331 } 1332 else if (PyString_CheckExact(v) && 1333 PyString_CheckExact(w)) { 1334 x = string_concatenate(v, w, f, next_instr); 1335 /* string_concatenate consumed the ref to v */ 1336 goto skip_decref_vx; 1337 } 1338 else { 1339 slow_add: 1340 x = PyNumber_Add(v, w); 1341 } 1342 Py_DECREF(v); 1343 skip_decref_vx: 1344 Py_DECREF(w); 1345 SET_TOP(x); 1346 if (x != NULL) continue; 1347 break; 1348 1349 case BINARY_SUBTRACT: 1350 w = POP(); 1351 v = TOP(); 1352 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { 1353 /* INLINE: int - int */ 1354 register long a, b, i; 1355 a = PyInt_AS_LONG(v); 1356 b = PyInt_AS_LONG(w); 1357 /* cast to avoid undefined behaviour 1358 on overflow */ 1359 i = (long)((unsigned long)a - b); 1360 if ((i^a) < 0 && (i^~b) < 0) 1361 goto slow_sub; 1362 x = PyInt_FromLong(i); 1363 } 1364 else { 1365 slow_sub: 1366 x = PyNumber_Subtract(v, w); 1367 } 1368 Py_DECREF(v); 1369 Py_DECREF(w); 1370 SET_TOP(x); 1371 if (x != NULL) continue; 1372 break; 1373 1374 case BINARY_SUBSCR: 1375 w = POP(); 1376 v = TOP(); 1377 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) { 1378 /* INLINE: list[int] */ 1379 Py_ssize_t i = PyInt_AsSsize_t(w); 1380 if (i < 0) 1381 i += PyList_GET_SIZE(v); 1382 if (i >= 0 && i < PyList_GET_SIZE(v)) { 1383 x = PyList_GET_ITEM(v, i); 1384 Py_INCREF(x); 1385 } 1386 else 1387 goto slow_get; 1388 } 1389 else 1390 slow_get: 1391 x = PyObject_GetItem(v, w); 1392 Py_DECREF(v); 1393 Py_DECREF(w); 1394 SET_TOP(x); 1395 if (x != NULL) continue; 1396 break; 1397 1398 case BINARY_LSHIFT: 1399 w = POP(); 1400 v = TOP(); 1401 x = PyNumber_Lshift(v, w); 1402 Py_DECREF(v); 1403 Py_DECREF(w); 1404 SET_TOP(x); 1405 if (x != NULL) continue; 1406 break; 1407 1408 case BINARY_RSHIFT: 1409 w = POP(); 1410 v = TOP(); 1411 x = PyNumber_Rshift(v, w); 1412 Py_DECREF(v); 1413 Py_DECREF(w); 1414 SET_TOP(x); 1415 if (x != NULL) continue; 1416 break; 1417 1418 case BINARY_AND: 1419 w = POP(); 1420 v = TOP(); 1421 x = PyNumber_And(v, w); 1422 Py_DECREF(v); 1423 Py_DECREF(w); 1424 SET_TOP(x); 1425 if (x != NULL) continue; 1426 break; 1427 1428 case BINARY_XOR: 1429 w = POP(); 1430 v = TOP(); 1431 x = PyNumber_Xor(v, w); 1432 Py_DECREF(v); 1433 Py_DECREF(w); 1434 SET_TOP(x); 1435 if (x != NULL) continue; 1436 break; 1437 1438 case BINARY_OR: 1439 w = POP(); 1440 v = TOP(); 1441 x = PyNumber_Or(v, w); 1442 Py_DECREF(v); 1443 Py_DECREF(w); 1444 SET_TOP(x); 1445 if (x != NULL) continue; 1446 break; 1447 1448 case LIST_APPEND: 1449 w = POP(); 1450 v = PEEK(oparg); 1451 err = PyList_Append(v, w); 1452 Py_DECREF(w); 1453 if (err == 0) { 1454 PREDICT(JUMP_ABSOLUTE); 1455 continue; 1456 } 1457 break; 1458 1459 case SET_ADD: 1460 w = POP(); 1461 v = stack_pointer[-oparg]; 1462 err = PySet_Add(v, w); 1463 Py_DECREF(w); 1464 if (err == 0) { 1465 PREDICT(JUMP_ABSOLUTE); 1466 continue; 1467 } 1468 break; 1469 1470 case INPLACE_POWER: 1471 w = POP(); 1472 v = TOP(); 1473 x = PyNumber_InPlacePower(v, w, Py_None); 1474 Py_DECREF(v); 1475 Py_DECREF(w); 1476 SET_TOP(x); 1477 if (x != NULL) continue; 1478 break; 1479 1480 case INPLACE_MULTIPLY: 1481 w = POP(); 1482 v = TOP(); 1483 x = PyNumber_InPlaceMultiply(v, w); 1484 Py_DECREF(v); 1485 Py_DECREF(w); 1486 SET_TOP(x); 1487 if (x != NULL) continue; 1488 break; 1489 1490 case INPLACE_DIVIDE: 1491 if (!_Py_QnewFlag) { 1492 w = POP(); 1493 v = TOP(); 1494 x = PyNumber_InPlaceDivide(v, w); 1495 Py_DECREF(v); 1496 Py_DECREF(w); 1497 SET_TOP(x); 1498 if (x != NULL) continue; 1499 break; 1500 } 1501 /* -Qnew is in effect: fall through to 1502 INPLACE_TRUE_DIVIDE */ 1503 case INPLACE_TRUE_DIVIDE: 1504 w = POP(); 1505 v = TOP(); 1506 x = PyNumber_InPlaceTrueDivide(v, w); 1507 Py_DECREF(v); 1508 Py_DECREF(w); 1509 SET_TOP(x); 1510 if (x != NULL) continue; 1511 break; 1512 1513 case INPLACE_FLOOR_DIVIDE: 1514 w = POP(); 1515 v = TOP(); 1516 x = PyNumber_InPlaceFloorDivide(v, w); 1517 Py_DECREF(v); 1518 Py_DECREF(w); 1519 SET_TOP(x); 1520 if (x != NULL) continue; 1521 break; 1522 1523 case INPLACE_MODULO: 1524 w = POP(); 1525 v = TOP(); 1526 x = PyNumber_InPlaceRemainder(v, w); 1527 Py_DECREF(v); 1528 Py_DECREF(w); 1529 SET_TOP(x); 1530 if (x != NULL) continue; 1531 break; 1532 1533 case INPLACE_ADD: 1534 w = POP(); 1535 v = TOP(); 1536 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { 1537 /* INLINE: int + int */ 1538 register long a, b, i; 1539 a = PyInt_AS_LONG(v); 1540 b = PyInt_AS_LONG(w); 1541 i = a + b; 1542 if ((i^a) < 0 && (i^b) < 0) 1543 goto slow_iadd; 1544 x = PyInt_FromLong(i); 1545 } 1546 else if (PyString_CheckExact(v) && 1547 PyString_CheckExact(w)) { 1548 x = string_concatenate(v, w, f, next_instr); 1549 /* string_concatenate consumed the ref to v */ 1550 goto skip_decref_v; 1551 } 1552 else { 1553 slow_iadd: 1554 x = PyNumber_InPlaceAdd(v, w); 1555 } 1556 Py_DECREF(v); 1557 skip_decref_v: 1558 Py_DECREF(w); 1559 SET_TOP(x); 1560 if (x != NULL) continue; 1561 break; 1562 1563 case INPLACE_SUBTRACT: 1564 w = POP(); 1565 v = TOP(); 1566 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { 1567 /* INLINE: int - int */ 1568 register long a, b, i; 1569 a = PyInt_AS_LONG(v); 1570 b = PyInt_AS_LONG(w); 1571 i = a - b; 1572 if ((i^a) < 0 && (i^~b) < 0) 1573 goto slow_isub; 1574 x = PyInt_FromLong(i); 1575 } 1576 else { 1577 slow_isub: 1578 x = PyNumber_InPlaceSubtract(v, w); 1579 } 1580 Py_DECREF(v); 1581 Py_DECREF(w); 1582 SET_TOP(x); 1583 if (x != NULL) continue; 1584 break; 1585 1586 case INPLACE_LSHIFT: 1587 w = POP(); 1588 v = TOP(); 1589 x = PyNumber_InPlaceLshift(v, w); 1590 Py_DECREF(v); 1591 Py_DECREF(w); 1592 SET_TOP(x); 1593 if (x != NULL) continue; 1594 break; 1595 1596 case INPLACE_RSHIFT: 1597 w = POP(); 1598 v = TOP(); 1599 x = PyNumber_InPlaceRshift(v, w); 1600 Py_DECREF(v); 1601 Py_DECREF(w); 1602 SET_TOP(x); 1603 if (x != NULL) continue; 1604 break; 1605 1606 case INPLACE_AND: 1607 w = POP(); 1608 v = TOP(); 1609 x = PyNumber_InPlaceAnd(v, w); 1610 Py_DECREF(v); 1611 Py_DECREF(w); 1612 SET_TOP(x); 1613 if (x != NULL) continue; 1614 break; 1615 1616 case INPLACE_XOR: 1617 w = POP(); 1618 v = TOP(); 1619 x = PyNumber_InPlaceXor(v, w); 1620 Py_DECREF(v); 1621 Py_DECREF(w); 1622 SET_TOP(x); 1623 if (x != NULL) continue; 1624 break; 1625 1626 case INPLACE_OR: 1627 w = POP(); 1628 v = TOP(); 1629 x = PyNumber_InPlaceOr(v, w); 1630 Py_DECREF(v); 1631 Py_DECREF(w); 1632 SET_TOP(x); 1633 if (x != NULL) continue; 1634 break; 1635 1636 case SLICE+0: 1637 case SLICE+1: 1638 case SLICE+2: 1639 case SLICE+3: 1640 if ((opcode-SLICE) & 2) 1641 w = POP(); 1642 else 1643 w = NULL; 1644 if ((opcode-SLICE) & 1) 1645 v = POP(); 1646 else 1647 v = NULL; 1648 u = TOP(); 1649 x = apply_slice(u, v, w); 1650 Py_DECREF(u); 1651 Py_XDECREF(v); 1652 Py_XDECREF(w); 1653 SET_TOP(x); 1654 if (x != NULL) continue; 1655 break; 1656 1657 case STORE_SLICE+0: 1658 case STORE_SLICE+1: 1659 case STORE_SLICE+2: 1660 case STORE_SLICE+3: 1661 if ((opcode-STORE_SLICE) & 2) 1662 w = POP(); 1663 else 1664 w = NULL; 1665 if ((opcode-STORE_SLICE) & 1) 1666 v = POP(); 1667 else 1668 v = NULL; 1669 u = POP(); 1670 t = POP(); 1671 err = assign_slice(u, v, w, t); /* u[v:w] = t */ 1672 Py_DECREF(t); 1673 Py_DECREF(u); 1674 Py_XDECREF(v); 1675 Py_XDECREF(w); 1676 if (err == 0) continue; 1677 break; 1678 1679 case DELETE_SLICE+0: 1680 case DELETE_SLICE+1: 1681 case DELETE_SLICE+2: 1682 case DELETE_SLICE+3: 1683 if ((opcode-DELETE_SLICE) & 2) 1684 w = POP(); 1685 else 1686 w = NULL; 1687 if ((opcode-DELETE_SLICE) & 1) 1688 v = POP(); 1689 else 1690 v = NULL; 1691 u = POP(); 1692 err = assign_slice(u, v, w, (PyObject *)NULL); 1693 /* del u[v:w] */ 1694 Py_DECREF(u); 1695 Py_XDECREF(v); 1696 Py_XDECREF(w); 1697 if (err == 0) continue; 1698 break; 1699 1700 case STORE_SUBSCR: 1701 w = TOP(); 1702 v = SECOND(); 1703 u = THIRD(); 1704 STACKADJ(-3); 1705 /* v[w] = u */ 1706 err = PyObject_SetItem(v, w, u); 1707 Py_DECREF(u); 1708 Py_DECREF(v); 1709 Py_DECREF(w); 1710 if (err == 0) continue; 1711 break; 1712 1713 case DELETE_SUBSCR: 1714 w = TOP(); 1715 v = SECOND(); 1716 STACKADJ(-2); 1717 /* del v[w] */ 1718 err = PyObject_DelItem(v, w); 1719 Py_DECREF(v); 1720 Py_DECREF(w); 1721 if (err == 0) continue; 1722 break; 1723 1724 case PRINT_EXPR: 1725 v = POP(); 1726 w = PySys_GetObject("displayhook"); 1727 if (w == NULL) { 1728 PyErr_SetString(PyExc_RuntimeError, 1729 "lost sys.displayhook"); 1730 err = -1; 1731 x = NULL; 1732 } 1733 if (err == 0) { 1734 x = PyTuple_Pack(1, v); 1735 if (x == NULL) 1736 err = -1; 1737 } 1738 if (err == 0) { 1739 w = PyEval_CallObject(w, x); 1740 Py_XDECREF(w); 1741 if (w == NULL) 1742 err = -1; 1743 } 1744 Py_DECREF(v); 1745 Py_XDECREF(x); 1746 break; 1747 1748 case PRINT_ITEM_TO: 1749 w = stream = POP(); 1750 /* fall through to PRINT_ITEM */ 1751 1752 case PRINT_ITEM: 1753 v = POP(); 1754 if (stream == NULL || stream == Py_None) { 1755 w = PySys_GetObject("stdout"); 1756 if (w == NULL) { 1757 PyErr_SetString(PyExc_RuntimeError, 1758 "lost sys.stdout"); 1759 err = -1; 1760 } 1761 } 1762 /* PyFile_SoftSpace() can exececute arbitrary code 1763 if sys.stdout is an instance with a __getattr__. 1764 If __getattr__ raises an exception, w will 1765 be freed, so we need to prevent that temporarily. */ 1766 Py_XINCREF(w); 1767 if (w != NULL && PyFile_SoftSpace(w, 0)) 1768 err = PyFile_WriteString(" ", w); 1769 if (err == 0) 1770 err = PyFile_WriteObject(v, w, Py_PRINT_RAW); 1771 if (err == 0) { 1772 /* XXX move into writeobject() ? */ 1773 if (PyString_Check(v)) { 1774 char *s = PyString_AS_STRING(v); 1775 Py_ssize_t len = PyString_GET_SIZE(v); 1776 if (len == 0 || 1777 !isspace(Py_CHARMASK(s[len-1])) || 1778 s[len-1] == ' ') 1779 PyFile_SoftSpace(w, 1); 1780 } 1641 1781 #ifdef Py_USING_UNICODE 1642 1643 1644 1645 1646 1647 1648 1649 1782 else if (PyUnicode_Check(v)) { 1783 Py_UNICODE *s = PyUnicode_AS_UNICODE(v); 1784 Py_ssize_t len = PyUnicode_GET_SIZE(v); 1785 if (len == 0 || 1786 !Py_UNICODE_ISSPACE(s[len-1]) || 1787 s[len-1] == ' ') 1788 PyFile_SoftSpace(w, 1); 1789 } 1650 1790 #endif 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1791 else 1792 PyFile_SoftSpace(w, 1); 1793 } 1794 Py_XDECREF(w); 1795 Py_DECREF(v); 1796 Py_XDECREF(stream); 1797 stream = NULL; 1798 if (err == 0) 1799 continue; 1800 break; 1801 1802 case PRINT_NEWLINE_TO: 1803 w = stream = POP(); 1804 /* fall through to PRINT_NEWLINE */ 1805 1806 case PRINT_NEWLINE: 1807 if (stream == NULL || stream == Py_None) { 1808 w = PySys_GetObject("stdout"); 1809 if (w == NULL) { 1810 PyErr_SetString(PyExc_RuntimeError, 1811 "lost sys.stdout"); 1812 why = WHY_EXCEPTION; 1813 } 1814 } 1815 if (w != NULL) { 1816 /* w.write() may replace sys.stdout, so we 1817 * have to keep our reference to it */ 1818 Py_INCREF(w); 1819 err = PyFile_WriteString("\n", w); 1820 if (err == 0) 1821 PyFile_SoftSpace(w, 0); 1822 Py_DECREF(w); 1823 } 1824 Py_XDECREF(stream); 1825 stream = NULL; 1826 break; 1687 1827 1688 1828 1689 1829 #ifdef CASE_TOO_BIG 1690 1830 default: switch (opcode) { 1691 1831 #endif 1692 case RAISE_VARARGS: 1693 u = v = w = NULL; 1694 switch (oparg) { 1695 case 3: 1696 u = POP(); /* traceback */ 1697 /* Fallthrough */ 1698 case 2: 1699 v = POP(); /* value */ 1700 /* Fallthrough */ 1701 case 1: 1702 w = POP(); /* exc */ 1703 case 0: /* Fallthrough */ 1704 why = do_raise(w, v, u); 1705 break; 1706 default: 1707 PyErr_SetString(PyExc_SystemError, 1708 "bad RAISE_VARARGS oparg"); 1709 why = WHY_EXCEPTION; 1710 break; 1711 } 1712 break; 1713 1714 case LOAD_LOCALS: 1715 if ((x = f->f_locals) != NULL) { 1716 Py_INCREF(x); 1717 PUSH(x); 1718 continue; 1719 } 1720 PyErr_SetString(PyExc_SystemError, "no locals"); 1721 break; 1722 1723 case RETURN_VALUE: 1724 retval = POP(); 1725 why = WHY_RETURN; 1726 goto fast_block_end; 1727 1728 case YIELD_VALUE: 1729 retval = POP(); 1730 f->f_stacktop = stack_pointer; 1731 why = WHY_YIELD; 1732 goto fast_yield; 1733 1734 case EXEC_STMT: 1735 w = TOP(); 1736 v = SECOND(); 1737 u = THIRD(); 1738 STACKADJ(-3); 1739 READ_TIMESTAMP(intr0); 1740 err = exec_statement(f, u, v, w); 1741 READ_TIMESTAMP(intr1); 1742 Py_DECREF(u); 1743 Py_DECREF(v); 1744 Py_DECREF(w); 1745 break; 1746 1747 case POP_BLOCK: 1748 { 1749 PyTryBlock *b = PyFrame_BlockPop(f); 1750 while (STACK_LEVEL() > b->b_level) { 1751 v = POP(); 1752 Py_DECREF(v); 1753 } 1754 } 1755 continue; 1756 1757 PREDICTED(END_FINALLY); 1758 case END_FINALLY: 1759 v = POP(); 1760 if (PyInt_Check(v)) { 1761 why = (enum why_code) PyInt_AS_LONG(v); 1762 assert(why != WHY_YIELD); 1763 if (why == WHY_RETURN || 1764 why == WHY_CONTINUE) 1765 retval = POP(); 1766 } 1767 else if (PyExceptionClass_Check(v) || 1768 PyString_Check(v)) { 1769 w = POP(); 1770 u = POP(); 1771 PyErr_Restore(v, w, u); 1772 why = WHY_RERAISE; 1773 break; 1774 } 1775 else if (v != Py_None) { 1776 PyErr_SetString(PyExc_SystemError, 1777 "'finally' pops bad exception"); 1778 why = WHY_EXCEPTION; 1779 } 1780 Py_DECREF(v); 1781 break; 1782 1783 case BUILD_CLASS: 1784 u = TOP(); 1785 v = SECOND(); 1786 w = THIRD(); 1787 STACKADJ(-2); 1788 x = build_class(u, v, w); 1789 SET_TOP(x); 1790 Py_DECREF(u); 1791 Py_DECREF(v); 1792 Py_DECREF(w); 1793 break; 1794 1795 case STORE_NAME: 1796 w = GETITEM(names, oparg); 1797 v = POP(); 1798 if ((x = f->f_locals) != NULL) { 1799 if (PyDict_CheckExact(x)) 1800 err = PyDict_SetItem(x, w, v); 1801 else 1802 err = PyObject_SetItem(x, w, v); 1803 Py_DECREF(v); 1804 if (err == 0) continue; 1805 break; 1806 } 1807 PyErr_Format(PyExc_SystemError, 1808 "no locals found when storing %s", 1809 PyObject_REPR(w)); 1810 break; 1811 1812 case DELETE_NAME: 1813 w = GETITEM(names, oparg); 1814 if ((x = f->f_locals) != NULL) { 1815 if ((err = PyObject_DelItem(x, w)) != 0) 1816 format_exc_check_arg(PyExc_NameError, 1817 NAME_ERROR_MSG, 1818 w); 1819 break; 1820 } 1821 PyErr_Format(PyExc_SystemError, 1822 "no locals when deleting %s", 1823 PyObject_REPR(w)); 1824 break; 1825 1826 PREDICTED_WITH_ARG(UNPACK_SEQUENCE); 1827 case UNPACK_SEQUENCE: 1828 v = POP(); 1829 if (PyTuple_CheckExact(v) && 1830 PyTuple_GET_SIZE(v) == oparg) { 1831 PyObject **items = \ 1832 ((PyTupleObject *)v)->ob_item; 1833 while (oparg--) { 1834 w = items[oparg]; 1835 Py_INCREF(w); 1836 PUSH(w); 1837 } 1838 Py_DECREF(v); 1839 continue; 1840 } else if (PyList_CheckExact(v) && 1841 PyList_GET_SIZE(v) == oparg) { 1842 PyObject **items = \ 1843 ((PyListObject *)v)->ob_item; 1844 while (oparg--) { 1845 w = items[oparg]; 1846 Py_INCREF(w); 1847 PUSH(w); 1848 } 1849 } else if (unpack_iterable(v, oparg, 1850 stack_pointer + oparg)) { 1851 stack_pointer += oparg; 1852 } else { 1853 /* unpack_iterable() raised an exception */ 1854 why = WHY_EXCEPTION; 1855 } 1856 Py_DECREF(v); 1857 break; 1858 1859 case STORE_ATTR: 1860 w = GETITEM(names, oparg); 1861 v = TOP(); 1862 u = SECOND(); 1863 STACKADJ(-2); 1864 err = PyObject_SetAttr(v, w, u); /* v.w = u */ 1865 Py_DECREF(v); 1866 Py_DECREF(u); 1867 if (err == 0) continue; 1868 break; 1869 1870 case DELETE_ATTR: 1871 w = GETITEM(names, oparg); 1872 v = POP(); 1873 err = PyObject_SetAttr(v, w, (PyObject *)NULL); 1874 /* del v.w */ 1875 Py_DECREF(v); 1876 break; 1877 1878 case STORE_GLOBAL: 1879 w = GETITEM(names, oparg); 1880 v = POP(); 1881 err = PyDict_SetItem(f->f_globals, w, v); 1882 Py_DECREF(v); 1883 if (err == 0) continue; 1884 break; 1885 1886 case DELETE_GLOBAL: 1887 w = GETITEM(names, oparg); 1888 if ((err = PyDict_DelItem(f->f_globals, w)) != 0) 1889 format_exc_check_arg( 1890 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w); 1891 break; 1892 1893 case LOAD_NAME: 1894 w = GETITEM(names, oparg); 1895 if ((v = f->f_locals) == NULL) { 1896 PyErr_Format(PyExc_SystemError, 1897 "no locals when loading %s", 1898 PyObject_REPR(w)); 1899 why = WHY_EXCEPTION; 1900 break; 1901 } 1902 if (PyDict_CheckExact(v)) { 1903 x = PyDict_GetItem(v, w); 1904 Py_XINCREF(x); 1905 } 1906 else { 1907 x = PyObject_GetItem(v, w); 1908 if (x == NULL && PyErr_Occurred()) { 1909 if (!PyErr_ExceptionMatches( 1910 PyExc_KeyError)) 1911 break; 1912 PyErr_Clear(); 1913 } 1914 } 1915 if (x == NULL) { 1916 x = PyDict_GetItem(f->f_globals, w); 1917 if (x == NULL) { 1918 x = PyDict_GetItem(f->f_builtins, w); 1919 if (x == NULL) { 1920 format_exc_check_arg( 1921 PyExc_NameError, 1922 NAME_ERROR_MSG, w); 1923 break; 1924 } 1925 } 1926 Py_INCREF(x); 1927 } 1928 PUSH(x); 1929 continue; 1930 1931 case LOAD_GLOBAL: 1932 w = GETITEM(names, oparg); 1933 if (PyString_CheckExact(w)) { 1934 /* Inline the PyDict_GetItem() calls. 1935 WARNING: this is an extreme speed hack. 1936 Do not try this at home. */ 1937 long hash = ((PyStringObject *)w)->ob_shash; 1938 if (hash != -1) { 1939 PyDictObject *d; 1940 PyDictEntry *e; 1941 d = (PyDictObject *)(f->f_globals); 1942 e = d->ma_lookup(d, w, hash); 1943 if (e == NULL) { 1944 x = NULL; 1945 break; 1946 } 1947 x = e->me_value; 1948 if (x != NULL) { 1949 Py_INCREF(x); 1950 PUSH(x); 1951 continue; 1952 } 1953 d = (PyDictObject *)(f->f_builtins); 1954 e = d->ma_lookup(d, w, hash); 1955 if (e == NULL) { 1956 x = NULL; 1957 break; 1958 } 1959 x = e->me_value; 1960 if (x != NULL) { 1961 Py_INCREF(x); 1962 PUSH(x); 1963 continue; 1964 } 1965 goto load_global_error; 1966 } 1967 } 1968 /* This is the un-inlined version of the code above */ 1969 x = PyDict_GetItem(f->f_globals, w); 1970 if (x == NULL) { 1971 x = PyDict_GetItem(f->f_builtins, w); 1972 if (x == NULL) { 1973 load_global_error: 1974 format_exc_check_arg( 1975 PyExc_NameError, 1976 GLOBAL_NAME_ERROR_MSG, w); 1977 break; 1978 } 1979 } 1980 Py_INCREF(x); 1981 PUSH(x); 1982 continue; 1983 1984 case DELETE_FAST: 1985 x = GETLOCAL(oparg); 1986 if (x != NULL) { 1987 SETLOCAL(oparg, NULL); 1988 continue; 1989 } 1990 format_exc_check_arg( 1991 PyExc_UnboundLocalError, 1992 UNBOUNDLOCAL_ERROR_MSG, 1993 PyTuple_GetItem(co->co_varnames, oparg) 1994 ); 1995 break; 1996 1997 case LOAD_CLOSURE: 1998 x = freevars[oparg]; 1999 Py_INCREF(x); 2000 PUSH(x); 2001 if (x != NULL) continue; 2002 break; 2003 2004 case LOAD_DEREF: 2005 x = freevars[oparg]; 2006 w = PyCell_Get(x); 2007 if (w != NULL) { 2008 PUSH(w); 2009 continue; 2010 } 2011 err = -1; 2012 /* Don't stomp existing exception */ 2013 if (PyErr_Occurred()) 2014 break; 2015 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { 2016 v = PyTuple_GET_ITEM(co->co_cellvars, 2017 oparg); 2018 format_exc_check_arg( 2019 PyExc_UnboundLocalError, 2020 UNBOUNDLOCAL_ERROR_MSG, 2021 v); 2022 } else { 2023 v = PyTuple_GET_ITEM(co->co_freevars, oparg - 2024 PyTuple_GET_SIZE(co->co_cellvars)); 2025 format_exc_check_arg(PyExc_NameError, 2026 UNBOUNDFREE_ERROR_MSG, v); 2027 } 2028 break; 2029 2030 case STORE_DEREF: 2031 w = POP(); 2032 x = freevars[oparg]; 2033 PyCell_Set(x, w); 2034 Py_DECREF(w); 2035 continue; 2036 2037 case BUILD_TUPLE: 2038 x = PyTuple_New(oparg); 2039 if (x != NULL) { 2040 for (; --oparg >= 0;) { 2041 w = POP(); 2042 PyTuple_SET_ITEM(x, oparg, w); 2043 } 2044 PUSH(x); 2045 continue; 2046 } 2047 break; 2048 2049 case BUILD_LIST: 2050 x = PyList_New(oparg); 2051 if (x != NULL) { 2052 for (; --oparg >= 0;) { 2053 w = POP(); 2054 PyList_SET_ITEM(x, oparg, w); 2055 } 2056 PUSH(x); 2057 continue; 2058 } 2059 break; 2060 2061 case BUILD_MAP: 2062 x = _PyDict_NewPresized((Py_ssize_t)oparg); 2063 PUSH(x); 2064 if (x != NULL) continue; 2065 break; 2066 2067 case STORE_MAP: 2068 w = TOP(); /* key */ 2069 u = SECOND(); /* value */ 2070 v = THIRD(); /* dict */ 2071 STACKADJ(-2); 2072 assert (PyDict_CheckExact(v)); 2073 err = PyDict_SetItem(v, w, u); /* v[w] = u */ 2074 Py_DECREF(u); 2075 Py_DECREF(w); 2076 if (err == 0) continue; 2077 break; 2078 2079 case LOAD_ATTR: 2080 w = GETITEM(names, oparg); 2081 v = TOP(); 2082 x = PyObject_GetAttr(v, w); 2083 Py_DECREF(v); 2084 SET_TOP(x); 2085 if (x != NULL) continue; 2086 break; 2087 2088 case COMPARE_OP: 2089 w = POP(); 2090 v = TOP(); 2091 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) { 2092 /* INLINE: cmp(int, int) */ 2093 register long a, b; 2094 register int res; 2095 a = PyInt_AS_LONG(v); 2096 b = PyInt_AS_LONG(w); 2097 switch (oparg) { 2098 case PyCmp_LT: res = a < b; break; 2099 case PyCmp_LE: res = a <= b; break; 2100 case PyCmp_EQ: res = a == b; break; 2101 case PyCmp_NE: res = a != b; break; 2102 case PyCmp_GT: res = a > b; break; 2103 case PyCmp_GE: res = a >= b; break; 2104 case PyCmp_IS: res = v == w; break; 2105 case PyCmp_IS_NOT: res = v != w; break; 2106 default: goto slow_compare; 2107 } 2108 x = res ? Py_True : Py_False; 2109 Py_INCREF(x); 2110 } 2111 else { 2112 slow_compare: 2113 x = cmp_outcome(oparg, v, w); 2114 } 2115 Py_DECREF(v); 2116 Py_DECREF(w); 2117 SET_TOP(x); 2118 if (x == NULL) break; 2119 PREDICT(JUMP_IF_FALSE); 2120 PREDICT(JUMP_IF_TRUE); 2121 continue; 2122 2123 case IMPORT_NAME: 2124 w = GETITEM(names, oparg); 2125 x = PyDict_GetItemString(f->f_builtins, "__import__"); 2126 if (x == NULL) { 2127 PyErr_SetString(PyExc_ImportError, 2128 "__import__ not found"); 2129 break; 2130 } 2131 Py_INCREF(x); 2132 v = POP(); 2133 u = TOP(); 2134 if (PyInt_AsLong(u) != -1 || PyErr_Occurred()) 2135 w = PyTuple_Pack(5, 2136 w, 2137 f->f_globals, 2138 f->f_locals == NULL ? 2139 Py_None : f->f_locals, 2140 v, 2141 u); 2142 else 2143 w = PyTuple_Pack(4, 2144 w, 2145 f->f_globals, 2146 f->f_locals == NULL ? 2147 Py_None : f->f_locals, 2148 v); 2149 Py_DECREF(v); 2150 Py_DECREF(u); 2151 if (w == NULL) { 2152 u = POP(); 2153 Py_DECREF(x); 2154 x = NULL; 2155 break; 2156 } 2157 READ_TIMESTAMP(intr0); 2158 v = x; 2159 x = PyEval_CallObject(v, w); 2160 Py_DECREF(v); 2161 READ_TIMESTAMP(intr1); 2162 Py_DECREF(w); 2163 SET_TOP(x); 2164 if (x != NULL) continue; 2165 break; 2166 2167 case IMPORT_STAR: 2168 v = POP(); 2169 PyFrame_FastToLocals(f); 2170 if ((x = f->f_locals) == NULL) { 2171 PyErr_SetString(PyExc_SystemError, 2172 "no locals found during 'import *'"); 2173 break; 2174 } 2175 READ_TIMESTAMP(intr0); 2176 err = import_all_from(x, v); 2177 READ_TIMESTAMP(intr1); 2178 PyFrame_LocalsToFast(f, 0); 2179 Py_DECREF(v); 2180 if (err == 0) continue; 2181 break; 2182 2183 case IMPORT_FROM: 2184 w = GETITEM(names, oparg); 2185 v = TOP(); 2186 READ_TIMESTAMP(intr0); 2187 x = import_from(v, w); 2188 READ_TIMESTAMP(intr1); 2189 PUSH(x); 2190 if (x != NULL) continue; 2191 break; 2192 2193 case JUMP_FORWARD: 2194 JUMPBY(oparg); 2195 goto fast_next_opcode; 2196 2197 PREDICTED_WITH_ARG(JUMP_IF_FALSE); 2198 case JUMP_IF_FALSE: 2199 w = TOP(); 2200 if (w == Py_True) { 2201 PREDICT(POP_TOP); 2202 goto fast_next_opcode; 2203 } 2204 if (w == Py_False) { 2205 JUMPBY(oparg); 2206 goto fast_next_opcode; 2207 } 2208 err = PyObject_IsTrue(w); 2209 if (err > 0) 2210 err = 0; 2211 else if (err == 0) 2212 JUMPBY(oparg); 2213 else 2214 break; 2215 continue; 2216 2217 PREDICTED_WITH_ARG(JUMP_IF_TRUE); 2218 case JUMP_IF_TRUE: 2219 w = TOP(); 2220 if (w == Py_False) { 2221 PREDICT(POP_TOP); 2222 goto fast_next_opcode; 2223 } 2224 if (w == Py_True) { 2225 JUMPBY(oparg); 2226 goto fast_next_opcode; 2227 } 2228 err = PyObject_IsTrue(w); 2229 if (err > 0) { 2230 err = 0; 2231 JUMPBY(oparg); 2232 } 2233 else if (err == 0) 2234 ; 2235 else 2236 break; 2237 continue; 2238 2239 PREDICTED_WITH_ARG(JUMP_ABSOLUTE); 2240 case JUMP_ABSOLUTE: 2241 JUMPTO(oparg); 1832 case RAISE_VARARGS: 1833 u = v = w = NULL; 1834 switch (oparg) { 1835 case 3: 1836 u = POP(); /* traceback */ 1837 /* Fallthrough */ 1838 case 2: 1839 v = POP(); /* value */ 1840 /* Fallthrough */ 1841 case 1: 1842 w = POP(); /* exc */ 1843 case 0: /* Fallthrough */ 1844 why = do_raise(w, v, u); 1845 break; 1846 default: 1847 PyErr_SetString(PyExc_SystemError, 1848 "bad RAISE_VARARGS oparg"); 1849 why = WHY_EXCEPTION; 1850 break; 1851 } 1852 break; 1853 1854 case LOAD_LOCALS: 1855 if ((x = f->f_locals) != NULL) { 1856 Py_INCREF(x); 1857 PUSH(x); 1858 continue; 1859 } 1860 PyErr_SetString(PyExc_SystemError, "no locals"); 1861 break; 1862 1863 case RETURN_VALUE: 1864 retval = POP(); 1865 why = WHY_RETURN; 1866 goto fast_block_end; 1867 1868 case YIELD_VALUE: 1869 retval = POP(); 1870 f->f_stacktop = stack_pointer; 1871 why = WHY_YIELD; 1872 goto fast_yield; 1873 1874 case EXEC_STMT: 1875 w = TOP(); 1876 v = SECOND(); 1877 u = THIRD(); 1878 STACKADJ(-3); 1879 READ_TIMESTAMP(intr0); 1880 err = exec_statement(f, u, v, w); 1881 READ_TIMESTAMP(intr1); 1882 Py_DECREF(u); 1883 Py_DECREF(v); 1884 Py_DECREF(w); 1885 break; 1886 1887 case POP_BLOCK: 1888 { 1889 PyTryBlock *b = PyFrame_BlockPop(f); 1890 while (STACK_LEVEL() > b->b_level) { 1891 v = POP(); 1892 Py_DECREF(v); 1893 } 1894 } 1895 continue; 1896 1897 PREDICTED(END_FINALLY); 1898 case END_FINALLY: 1899 v = POP(); 1900 if (PyInt_Check(v)) { 1901 why = (enum why_code) PyInt_AS_LONG(v); 1902 assert(why != WHY_YIELD); 1903 if (why == WHY_RETURN || 1904 why == WHY_CONTINUE) 1905 retval = POP(); 1906 } 1907 else if (PyExceptionClass_Check(v) || 1908 PyString_Check(v)) { 1909 w = POP(); 1910 u = POP(); 1911 PyErr_Restore(v, w, u); 1912 why = WHY_RERAISE; 1913 break; 1914 } 1915 else if (v != Py_None) { 1916 PyErr_SetString(PyExc_SystemError, 1917 "'finally' pops bad exception"); 1918 why = WHY_EXCEPTION; 1919 } 1920 Py_DECREF(v); 1921 break; 1922 1923 case BUILD_CLASS: 1924 u = TOP(); 1925 v = SECOND(); 1926 w = THIRD(); 1927 STACKADJ(-2); 1928 x = build_class(u, v, w); 1929 SET_TOP(x); 1930 Py_DECREF(u); 1931 Py_DECREF(v); 1932 Py_DECREF(w); 1933 break; 1934 1935 case STORE_NAME: 1936 w = GETITEM(names, oparg); 1937 v = POP(); 1938 if ((x = f->f_locals) != NULL) { 1939 if (PyDict_CheckExact(x)) 1940 err = PyDict_SetItem(x, w, v); 1941 else 1942 err = PyObject_SetItem(x, w, v); 1943 Py_DECREF(v); 1944 if (err == 0) continue; 1945 break; 1946 } 1947 PyErr_Format(PyExc_SystemError, 1948 "no locals found when storing %s", 1949 PyObject_REPR(w)); 1950 break; 1951 1952 case DELETE_NAME: 1953 w = GETITEM(names, oparg); 1954 if ((x = f->f_locals) != NULL) { 1955 if ((err = PyObject_DelItem(x, w)) != 0) 1956 format_exc_check_arg(PyExc_NameError, 1957 NAME_ERROR_MSG, 1958 w); 1959 break; 1960 } 1961 PyErr_Format(PyExc_SystemError, 1962 "no locals when deleting %s", 1963 PyObject_REPR(w)); 1964 break; 1965 1966 PREDICTED_WITH_ARG(UNPACK_SEQUENCE); 1967 case UNPACK_SEQUENCE: 1968 v = POP(); 1969 if (PyTuple_CheckExact(v) && 1970 PyTuple_GET_SIZE(v) == oparg) { 1971 PyObject **items = \ 1972 ((PyTupleObject *)v)->ob_item; 1973 while (oparg--) { 1974 w = items[oparg]; 1975 Py_INCREF(w); 1976 PUSH(w); 1977 } 1978 Py_DECREF(v); 1979 continue; 1980 } else if (PyList_CheckExact(v) && 1981 PyList_GET_SIZE(v) == oparg) { 1982 PyObject **items = \ 1983 ((PyListObject *)v)->ob_item; 1984 while (oparg--) { 1985 w = items[oparg]; 1986 Py_INCREF(w); 1987 PUSH(w); 1988 } 1989 } else if (unpack_iterable(v, oparg, 1990 stack_pointer + oparg)) { 1991 STACKADJ(oparg); 1992 } else { 1993 /* unpack_iterable() raised an exception */ 1994 why = WHY_EXCEPTION; 1995 } 1996 Py_DECREF(v); 1997 break; 1998 1999 case STORE_ATTR: 2000 w = GETITEM(names, oparg); 2001 v = TOP(); 2002 u = SECOND(); 2003 STACKADJ(-2); 2004 err = PyObject_SetAttr(v, w, u); /* v.w = u */ 2005 Py_DECREF(v); 2006 Py_DECREF(u); 2007 if (err == 0) continue; 2008 break; 2009 2010 case DELETE_ATTR: 2011 w = GETITEM(names, oparg); 2012 v = POP(); 2013 err = PyObject_SetAttr(v, w, (PyObject *)NULL); 2014 /* del v.w */ 2015 Py_DECREF(v); 2016 break; 2017 2018 case STORE_GLOBAL: 2019 w = GETITEM(names, oparg); 2020 v = POP(); 2021 err = PyDict_SetItem(f->f_globals, w, v); 2022 Py_DECREF(v); 2023 if (err == 0) continue; 2024 break; 2025 2026 case DELETE_GLOBAL: 2027 w = GETITEM(names, oparg); 2028 if ((err = PyDict_DelItem(f->f_globals, w)) != 0) 2029 format_exc_check_arg( 2030 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w); 2031 break; 2032 2033 case LOAD_NAME: 2034 w = GETITEM(names, oparg); 2035 if ((v = f->f_locals) == NULL) { 2036 PyErr_Format(PyExc_SystemError, 2037 "no locals when loading %s", 2038 PyObject_REPR(w)); 2039 why = WHY_EXCEPTION; 2040 break; 2041 } 2042 if (PyDict_CheckExact(v)) { 2043 x = PyDict_GetItem(v, w); 2044 Py_XINCREF(x); 2045 } 2046 else { 2047 x = PyObject_GetItem(v, w); 2048 if (x == NULL && PyErr_Occurred()) { 2049 if (!PyErr_ExceptionMatches( 2050 PyExc_KeyError)) 2051 break; 2052 PyErr_Clear(); 2053 } 2054 } 2055 if (x == NULL) { 2056 x = PyDict_GetItem(f->f_globals, w); 2057 if (x == NULL) { 2058 x = PyDict_GetItem(f->f_builtins, w); 2059 if (x == NULL) { 2060 format_exc_check_arg( 2061 PyExc_NameError, 2062 NAME_ERROR_MSG, w); 2063 break; 2064 } 2065 } 2066 Py_INCREF(x); 2067 } 2068 PUSH(x); 2069 continue; 2070 2071 case LOAD_GLOBAL: 2072 w = GETITEM(names, oparg); 2073 if (PyString_CheckExact(w)) { 2074 /* Inline the PyDict_GetItem() calls. 2075 WARNING: this is an extreme speed hack. 2076 Do not try this at home. */ 2077 long hash = ((PyStringObject *)w)->ob_shash; 2078 if (hash != -1) { 2079 PyDictObject *d; 2080 PyDictEntry *e; 2081 d = (PyDictObject *)(f->f_globals); 2082 e = d->ma_lookup(d, w, hash); 2083 if (e == NULL) { 2084 x = NULL; 2085 break; 2086 } 2087 x = e->me_value; 2088 if (x != NULL) { 2089 Py_INCREF(x); 2090 PUSH(x); 2091 continue; 2092 } 2093 d = (PyDictObject *)(f->f_builtins); 2094 e = d->ma_lookup(d, w, hash); 2095 if (e == NULL) { 2096 x = NULL; 2097 break; 2098 } 2099 x = e->me_value; 2100 if (x != NULL) { 2101 Py_INCREF(x); 2102 PUSH(x); 2103 continue; 2104 } 2105 goto load_global_error; 2106 } 2107 } 2108 /* This is the un-inlined version of the code above */ 2109 x = PyDict_GetItem(f->f_globals, w); 2110 if (x == NULL) { 2111 x = PyDict_GetItem(f->f_builtins, w); 2112 if (x == NULL) { 2113 load_global_error: 2114 format_exc_check_arg( 2115 PyExc_NameError, 2116 GLOBAL_NAME_ERROR_MSG, w); 2117 break; 2118 } 2119 } 2120 Py_INCREF(x); 2121 PUSH(x); 2122 continue; 2123 2124 case DELETE_FAST: 2125 x = GETLOCAL(oparg); 2126 if (x != NULL) { 2127 SETLOCAL(oparg, NULL); 2128 continue; 2129 } 2130 format_exc_check_arg( 2131 PyExc_UnboundLocalError, 2132 UNBOUNDLOCAL_ERROR_MSG, 2133 PyTuple_GetItem(co->co_varnames, oparg) 2134 ); 2135 break; 2136 2137 case LOAD_CLOSURE: 2138 x = freevars[oparg]; 2139 Py_INCREF(x); 2140 PUSH(x); 2141 if (x != NULL) continue; 2142 break; 2143 2144 case LOAD_DEREF: 2145 x = freevars[oparg]; 2146 w = PyCell_Get(x); 2147 if (w != NULL) { 2148 PUSH(w); 2149 continue; 2150 } 2151 err = -1; 2152 /* Don't stomp existing exception */ 2153 if (PyErr_Occurred()) 2154 break; 2155 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { 2156 v = PyTuple_GET_ITEM(co->co_cellvars, 2157 oparg); 2158 format_exc_check_arg( 2159 PyExc_UnboundLocalError, 2160 UNBOUNDLOCAL_ERROR_MSG, 2161 v); 2162 } else { 2163 v = PyTuple_GET_ITEM(co->co_freevars, oparg - 2164 PyTuple_GET_SIZE(co->co_cellvars)); 2165 format_exc_check_arg(PyExc_NameError, 2166 UNBOUNDFREE_ERROR_MSG, v); 2167 } 2168 break; 2169 2170 case STORE_DEREF: 2171 w = POP(); 2172 x = freevars[oparg]; 2173 PyCell_Set(x, w); 2174 Py_DECREF(w); 2175 continue; 2176 2177 case BUILD_TUPLE: 2178 x = PyTuple_New(oparg); 2179 if (x != NULL) { 2180 for (; --oparg >= 0;) { 2181 w = POP(); 2182 PyTuple_SET_ITEM(x, oparg, w); 2183 } 2184 PUSH(x); 2185 continue; 2186 } 2187 break; 2188 2189 case BUILD_LIST: 2190 x = PyList_New(oparg); 2191 if (x != NULL) { 2192 for (; --oparg >= 0;) { 2193 w = POP(); 2194 PyList_SET_ITEM(x, oparg, w); 2195 } 2196 PUSH(x); 2197 continue; 2198 } 2199 break; 2200 2201 case BUILD_SET: 2202 x = PySet_New(NULL); 2203 if (x != NULL) { 2204 for (; --oparg >= 0;) { 2205 w = POP(); 2206 if (err == 0) 2207 err = PySet_Add(x, w); 2208 Py_DECREF(w); 2209 } 2210 if (err != 0) { 2211 Py_DECREF(x); 2212 break; 2213 } 2214 PUSH(x); 2215 continue; 2216 } 2217 break; 2218 2219 2220 case BUILD_MAP: 2221 x = _PyDict_NewPresized((Py_ssize_t)oparg); 2222 PUSH(x); 2223 if (x != NULL) continue; 2224 break; 2225 2226 case STORE_MAP: 2227 w = TOP(); /* key */ 2228 u = SECOND(); /* value */ 2229 v = THIRD(); /* dict */ 2230 STACKADJ(-2); 2231 assert (PyDict_CheckExact(v)); 2232 err = PyDict_SetItem(v, w, u); /* v[w] = u */ 2233 Py_DECREF(u); 2234 Py_DECREF(w); 2235 if (err == 0) continue; 2236 break; 2237 2238 case MAP_ADD: 2239 w = TOP(); /* key */ 2240 u = SECOND(); /* value */ 2241 STACKADJ(-2); 2242 v = stack_pointer[-oparg]; /* dict */ 2243 assert (PyDict_CheckExact(v)); 2244 err = PyDict_SetItem(v, w, u); /* v[w] = u */ 2245 Py_DECREF(u); 2246 Py_DECREF(w); 2247 if (err == 0) { 2248 PREDICT(JUMP_ABSOLUTE); 2249 continue; 2250 } 2251 break; 2252 2253 case LOAD_ATTR: 2254 w = GETITEM(names, oparg); 2255 v = TOP(); 2256 x = PyObject_GetAttr(v, w); 2257 Py_DECREF(v); 2258 SET_TOP(x); 2259 if (x != NULL) continue; 2260 break; 2261 2262 case COMPARE_OP: 2263 w = POP(); 2264 v = TOP(); 2265 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) { 2266 /* INLINE: cmp(int, int) */ 2267 register long a, b; 2268 register int res; 2269 a = PyInt_AS_LONG(v); 2270 b = PyInt_AS_LONG(w); 2271 switch (oparg) { 2272 case PyCmp_LT: res = a < b; break; 2273 case PyCmp_LE: res = a <= b; break; 2274 case PyCmp_EQ: res = a == b; break; 2275 case PyCmp_NE: res = a != b; break; 2276 case PyCmp_GT: res = a > b; break; 2277 case PyCmp_GE: res = a >= b; break; 2278 case PyCmp_IS: res = v == w; break; 2279 case PyCmp_IS_NOT: res = v != w; break; 2280 default: goto slow_compare; 2281 } 2282 x = res ? Py_True : Py_False; 2283 Py_INCREF(x); 2284 } 2285 else { 2286 slow_compare: 2287 x = cmp_outcome(oparg, v, w); 2288 } 2289 Py_DECREF(v); 2290 Py_DECREF(w); 2291 SET_TOP(x); 2292 if (x == NULL) break; 2293 PREDICT(POP_JUMP_IF_FALSE); 2294 PREDICT(POP_JUMP_IF_TRUE); 2295 continue; 2296 2297 case IMPORT_NAME: 2298 w = GETITEM(names, oparg); 2299 x = PyDict_GetItemString(f->f_builtins, "__import__"); 2300 if (x == NULL) { 2301 PyErr_SetString(PyExc_ImportError, 2302 "__import__ not found"); 2303 break; 2304 } 2305 Py_INCREF(x); 2306 v = POP(); 2307 u = TOP(); 2308 if (PyInt_AsLong(u) != -1 || PyErr_Occurred()) 2309 w = PyTuple_Pack(5, 2310 w, 2311 f->f_globals, 2312 f->f_locals == NULL ? 2313 Py_None : f->f_locals, 2314 v, 2315 u); 2316 else 2317 w = PyTuple_Pack(4, 2318 w, 2319 f->f_globals, 2320 f->f_locals == NULL ? 2321 Py_None : f->f_locals, 2322 v); 2323 Py_DECREF(v); 2324 Py_DECREF(u); 2325 if (w == NULL) { 2326 u = POP(); 2327 Py_DECREF(x); 2328 x = NULL; 2329 break; 2330 } 2331 READ_TIMESTAMP(intr0); 2332 v = x; 2333 x = PyEval_CallObject(v, w); 2334 Py_DECREF(v); 2335 READ_TIMESTAMP(intr1); 2336 Py_DECREF(w); 2337 SET_TOP(x); 2338 if (x != NULL) continue; 2339 break; 2340 2341 case IMPORT_STAR: 2342 v = POP(); 2343 PyFrame_FastToLocals(f); 2344 if ((x = f->f_locals) == NULL) { 2345 PyErr_SetString(PyExc_SystemError, 2346 "no locals found during 'import *'"); 2347 break; 2348 } 2349 READ_TIMESTAMP(intr0); 2350 err = import_all_from(x, v); 2351 READ_TIMESTAMP(intr1); 2352 PyFrame_LocalsToFast(f, 0); 2353 Py_DECREF(v); 2354 if (err == 0) continue; 2355 break; 2356 2357 case IMPORT_FROM: 2358 w = GETITEM(names, oparg); 2359 v = TOP(); 2360 READ_TIMESTAMP(intr0); 2361 x = import_from(v, w); 2362 READ_TIMESTAMP(intr1); 2363 PUSH(x); 2364 if (x != NULL) continue; 2365 break; 2366 2367 case JUMP_FORWARD: 2368 JUMPBY(oparg); 2369 goto fast_next_opcode; 2370 2371 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE); 2372 case POP_JUMP_IF_FALSE: 2373 w = POP(); 2374 if (w == Py_True) { 2375 Py_DECREF(w); 2376 goto fast_next_opcode; 2377 } 2378 if (w == Py_False) { 2379 Py_DECREF(w); 2380 JUMPTO(oparg); 2381 goto fast_next_opcode; 2382 } 2383 err = PyObject_IsTrue(w); 2384 Py_DECREF(w); 2385 if (err > 0) 2386 err = 0; 2387 else if (err == 0) 2388 JUMPTO(oparg); 2389 else 2390 break; 2391 continue; 2392 2393 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE); 2394 case POP_JUMP_IF_TRUE: 2395 w = POP(); 2396 if (w == Py_False) { 2397 Py_DECREF(w); 2398 goto fast_next_opcode; 2399 } 2400 if (w == Py_True) { 2401 Py_DECREF(w); 2402 JUMPTO(oparg); 2403 goto fast_next_opcode; 2404 } 2405 err = PyObject_IsTrue(w); 2406 Py_DECREF(w); 2407 if (err > 0) { 2408 err = 0; 2409 JUMPTO(oparg); 2410 } 2411 else if (err == 0) 2412 ; 2413 else 2414 break; 2415 continue; 2416 2417 case JUMP_IF_FALSE_OR_POP: 2418 w = TOP(); 2419 if (w == Py_True) { 2420 STACKADJ(-1); 2421 Py_DECREF(w); 2422 goto fast_next_opcode; 2423 } 2424 if (w == Py_False) { 2425 JUMPTO(oparg); 2426 goto fast_next_opcode; 2427 } 2428 err = PyObject_IsTrue(w); 2429 if (err > 0) { 2430 STACKADJ(-1); 2431 Py_DECREF(w); 2432 err = 0; 2433 } 2434 else if (err == 0) 2435 JUMPTO(oparg); 2436 else 2437 break; 2438 continue; 2439 2440 case JUMP_IF_TRUE_OR_POP: 2441 w = TOP(); 2442 if (w == Py_False) { 2443 STACKADJ(-1); 2444 Py_DECREF(w); 2445 goto fast_next_opcode; 2446 } 2447 if (w == Py_True) { 2448 JUMPTO(oparg); 2449 goto fast_next_opcode; 2450 } 2451 err = PyObject_IsTrue(w); 2452 if (err > 0) { 2453 err = 0; 2454 JUMPTO(oparg); 2455 } 2456 else if (err == 0) { 2457 STACKADJ(-1); 2458 Py_DECREF(w); 2459 } 2460 else 2461 break; 2462 continue; 2463 2464 PREDICTED_WITH_ARG(JUMP_ABSOLUTE); 2465 case JUMP_ABSOLUTE: 2466 JUMPTO(oparg); 2242 2467 #if FAST_LOOPS 2243 2244 2245 2246 2247 2248 2249 2250 2468 /* Enabling this path speeds-up all while and for-loops by bypassing 2469 the per-loop checks for signals. By default, this should be turned-off 2470 because it prevents detection of a control-break in tight loops like 2471 "while 1: pass". Compile with this option turned-on when you need 2472 the speed-up and do not need break checking inside tight loops (ones 2473 that contain only instructions ending with goto fast_next_opcode). 2474 */ 2475 goto fast_next_opcode; 2251 2476 #else 2252 2477 continue; 2253 2478 #endif 2254 2479 2255 case GET_ITER: 2256 /* before: [obj]; after [getiter(obj)] */ 2257 v = TOP(); 2258 x = PyObject_GetIter(v); 2259 Py_DECREF(v); 2260 if (x != NULL) { 2261 SET_TOP(x); 2262 PREDICT(FOR_ITER); 2263 continue; 2264 } 2265 STACKADJ(-1); 2266 break; 2267 2268 PREDICTED_WITH_ARG(FOR_ITER); 2269 case FOR_ITER: 2270 /* before: [iter]; after: [iter, iter()] *or* [] */ 2271 v = TOP(); 2272 x = (*v->ob_type->tp_iternext)(v); 2273 if (x != NULL) { 2274 PUSH(x); 2275 PREDICT(STORE_FAST); 2276 PREDICT(UNPACK_SEQUENCE); 2277 continue; 2278 } 2279 if (PyErr_Occurred()) { 2280 if (!PyErr_ExceptionMatches( 2281 PyExc_StopIteration)) 2282 break; 2283 PyErr_Clear(); 2284 } 2285 /* iterator ended normally */ 2286 x = v = POP(); 2287 Py_DECREF(v); 2288 JUMPBY(oparg); 2289 continue; 2290 2291 case BREAK_LOOP: 2292 why = WHY_BREAK; 2293 goto fast_block_end; 2294 2295 case CONTINUE_LOOP: 2296 retval = PyInt_FromLong(oparg); 2297 if (!retval) { 2298 x = NULL; 2299 break; 2300 } 2301 why = WHY_CONTINUE; 2302 goto fast_block_end; 2303 2304 case SETUP_LOOP: 2305 case SETUP_EXCEPT: 2306 case SETUP_FINALLY: 2307 /* NOTE: If you add any new block-setup opcodes that 2308 are not try/except/finally handlers, you may need 2309 to update the PyGen_NeedsFinalizing() function. 2310 */ 2311 2312 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, 2313 STACK_LEVEL()); 2314 continue; 2315 2316 case WITH_CLEANUP: 2317 { 2318 /* At the top of the stack are 1-3 values indicating 2319 how/why we entered the finally clause: 2320 - TOP = None 2321 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval 2322 - TOP = WHY_*; no retval below it 2323 - (TOP, SECOND, THIRD) = exc_info() 2324 Below them is EXIT, the context.__exit__ bound method. 2325 In the last case, we must call 2326 EXIT(TOP, SECOND, THIRD) 2327 otherwise we must call 2328 EXIT(None, None, None) 2329 2330 In all cases, we remove EXIT from the stack, leaving 2331 the rest in the same order. 2332 2333 In addition, if the stack represents an exception, 2334 *and* the function call returns a 'true' value, we 2335 "zap" this information, to prevent END_FINALLY from 2336 re-raising the exception. (But non-local gotos 2337 should still be resumed.) 2338 */ 2339 2340 PyObject *exit_func; 2341 2342 u = POP(); 2343 if (u == Py_None) { 2344 exit_func = TOP(); 2345 SET_TOP(u); 2346 v = w = Py_None; 2347 } 2348 else if (PyInt_Check(u)) { 2349 switch(PyInt_AS_LONG(u)) { 2350 case WHY_RETURN: 2351 case WHY_CONTINUE: 2352 /* Retval in TOP. */ 2353 exit_func = SECOND(); 2354 SET_SECOND(TOP()); 2355 SET_TOP(u); 2356 break; 2357 default: 2358 exit_func = TOP(); 2359 SET_TOP(u); 2360 break; 2361 } 2362 u = v = w = Py_None; 2363 } 2364 else { 2365 v = TOP(); 2366 w = SECOND(); 2367 exit_func = THIRD(); 2368 SET_TOP(u); 2369 SET_SECOND(v); 2370 SET_THIRD(w); 2371 } 2372 /* XXX Not the fastest way to call it... */ 2373 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w, 2374 NULL); 2375 Py_DECREF(exit_func); 2376 if (x == NULL) 2377 break; /* Go to error exit */ 2378 2379 if (u != Py_None) 2380 err = PyObject_IsTrue(x); 2381 else 2382 err = 0; 2383 Py_DECREF(x); 2384 2385 if (err < 0) 2386 break; /* Go to error exit */ 2387 else if (err > 0) { 2388 err = 0; 2389 /* There was an exception and a true return */ 2390 STACKADJ(-2); 2391 Py_INCREF(Py_None); 2392 SET_TOP(Py_None); 2393 Py_DECREF(u); 2394 Py_DECREF(v); 2395 Py_DECREF(w); 2396 } else { 2397 /* The stack was rearranged to remove EXIT 2398 above. Let END_FINALLY do its thing */ 2399 } 2400 PREDICT(END_FINALLY); 2401 break; 2402 } 2403 2404 case CALL_FUNCTION: 2405 { 2406 PyObject **sp; 2407 PCALL(PCALL_ALL); 2408 sp = stack_pointer; 2480 case GET_ITER: 2481 /* before: [obj]; after [getiter(obj)] */ 2482 v = TOP(); 2483 x = PyObject_GetIter(v); 2484 Py_DECREF(v); 2485 if (x != NULL) { 2486 SET_TOP(x); 2487 PREDICT(FOR_ITER); 2488 continue; 2489 } 2490 STACKADJ(-1); 2491 break; 2492 2493 PREDICTED_WITH_ARG(FOR_ITER); 2494 case FOR_ITER: 2495 /* before: [iter]; after: [iter, iter()] *or* [] */ 2496 v = TOP(); 2497 x = (*v->ob_type->tp_iternext)(v); 2498 if (x != NULL) { 2499 PUSH(x); 2500 PREDICT(STORE_FAST); 2501 PREDICT(UNPACK_SEQUENCE); 2502 continue; 2503 } 2504 if (PyErr_Occurred()) { 2505 if (!PyErr_ExceptionMatches( 2506 PyExc_StopIteration)) 2507 break; 2508 PyErr_Clear(); 2509 } 2510 /* iterator ended normally */ 2511 x = v = POP(); 2512 Py_DECREF(v); 2513 JUMPBY(oparg); 2514 continue; 2515 2516 case BREAK_LOOP: 2517 why = WHY_BREAK; 2518 goto fast_block_end; 2519 2520 case CONTINUE_LOOP: 2521 retval = PyInt_FromLong(oparg); 2522 if (!retval) { 2523 x = NULL; 2524 break; 2525 } 2526 why = WHY_CONTINUE; 2527 goto fast_block_end; 2528 2529 case SETUP_LOOP: 2530 case SETUP_EXCEPT: 2531 case SETUP_FINALLY: 2532 /* NOTE: If you add any new block-setup opcodes that 2533 are not try/except/finally handlers, you may need 2534 to update the PyGen_NeedsFinalizing() function. 2535 */ 2536 2537 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, 2538 STACK_LEVEL()); 2539 continue; 2540 2541 case SETUP_WITH: 2542 { 2543 static PyObject *exit, *enter; 2544 w = TOP(); 2545 x = special_lookup(w, "__exit__", &exit); 2546 if (!x) 2547 break; 2548 SET_TOP(x); 2549 u = special_lookup(w, "__enter__", &enter); 2550 Py_DECREF(w); 2551 if (!u) { 2552 x = NULL; 2553 break; 2554 } 2555 x = PyObject_CallFunctionObjArgs(u, NULL); 2556 Py_DECREF(u); 2557 if (!x) 2558 break; 2559 /* Setup a finally block (SETUP_WITH as a block is 2560 equivalent to SETUP_FINALLY except it normalizes 2561 the exception) before pushing the result of 2562 __enter__ on the stack. */ 2563 PyFrame_BlockSetup(f, SETUP_WITH, INSTR_OFFSET() + oparg, 2564 STACK_LEVEL()); 2565 2566 PUSH(x); 2567 continue; 2568 } 2569 2570 case WITH_CLEANUP: 2571 { 2572 /* At the top of the stack are 1-3 values indicating 2573 how/why we entered the finally clause: 2574 - TOP = None 2575 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval 2576 - TOP = WHY_*; no retval below it 2577 - (TOP, SECOND, THIRD) = exc_info() 2578 Below them is EXIT, the context.__exit__ bound method. 2579 In the last case, we must call 2580 EXIT(TOP, SECOND, THIRD) 2581 otherwise we must call 2582 EXIT(None, None, None) 2583 2584 In all cases, we remove EXIT from the stack, leaving 2585 the rest in the same order. 2586 2587 In addition, if the stack represents an exception, 2588 *and* the function call returns a 'true' value, we 2589 "zap" this information, to prevent END_FINALLY from 2590 re-raising the exception. (But non-local gotos 2591 should still be resumed.) 2592 */ 2593 2594 PyObject *exit_func; 2595 2596 u = POP(); 2597 if (u == Py_None) { 2598 exit_func = TOP(); 2599 SET_TOP(u); 2600 v = w = Py_None; 2601 } 2602 else if (PyInt_Check(u)) { 2603 switch(PyInt_AS_LONG(u)) { 2604 case WHY_RETURN: 2605 case WHY_CONTINUE: 2606 /* Retval in TOP. */ 2607 exit_func = SECOND(); 2608 SET_SECOND(TOP()); 2609 SET_TOP(u); 2610 break; 2611 default: 2612 exit_func = TOP(); 2613 SET_TOP(u); 2614 break; 2615 } 2616 u = v = w = Py_None; 2617 } 2618 else { 2619 v = TOP(); 2620 w = SECOND(); 2621 exit_func = THIRD(); 2622 SET_TOP(u); 2623 SET_SECOND(v); 2624 SET_THIRD(w); 2625 } 2626 /* XXX Not the fastest way to call it... */ 2627 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w, 2628 NULL); 2629 Py_DECREF(exit_func); 2630 if (x == NULL) 2631 break; /* Go to error exit */ 2632 2633 if (u != Py_None) 2634 err = PyObject_IsTrue(x); 2635 else 2636 err = 0; 2637 Py_DECREF(x); 2638 2639 if (err < 0) 2640 break; /* Go to error exit */ 2641 else if (err > 0) { 2642 err = 0; 2643 /* There was an exception and a true return */ 2644 STACKADJ(-2); 2645 Py_INCREF(Py_None); 2646 SET_TOP(Py_None); 2647 Py_DECREF(u); 2648 Py_DECREF(v); 2649 Py_DECREF(w); 2650 } else { 2651 /* The stack was rearranged to remove EXIT 2652 above. Let END_FINALLY do its thing */ 2653 } 2654 PREDICT(END_FINALLY); 2655 break; 2656 } 2657 2658 case CALL_FUNCTION: 2659 { 2660 PyObject **sp; 2661 PCALL(PCALL_ALL); 2662 sp = stack_pointer; 2409 2663 #ifdef WITH_TSC 2410 2664 x = call_function(&sp, oparg, &intr0, &intr1); 2411 2665 #else 2412 2666 x = call_function(&sp, oparg); 2413 2667 #endif 2414 stack_pointer = sp; 2415 PUSH(x); 2416 if (x != NULL) 2417 continue; 2418 break; 2419 } 2420 2421 case CALL_FUNCTION_VAR: 2422 case CALL_FUNCTION_KW: 2423 case CALL_FUNCTION_VAR_KW: 2424 { 2425 int na = oparg & 0xff; 2426 int nk = (oparg>>8) & 0xff; 2427 int flags = (opcode - CALL_FUNCTION) & 3; 2428 int n = na + 2 * nk; 2429 PyObject **pfunc, *func, **sp; 2430 PCALL(PCALL_ALL); 2431 if (flags & CALL_FLAG_VAR) 2432 n++; 2433 if (flags & CALL_FLAG_KW) 2434 n++; 2435 pfunc = stack_pointer - n - 1; 2436 func = *pfunc; 2437 2438 if (PyMethod_Check(func) 2439 && PyMethod_GET_SELF(func) != NULL) { 2440 PyObject *self = PyMethod_GET_SELF(func); 2441 Py_INCREF(self); 2442 func = PyMethod_GET_FUNCTION(func); 2443 Py_INCREF(func); 2444 Py_DECREF(*pfunc); 2445 *pfunc = self; 2446 na++; 2447 n++; 2448 } else 2449 Py_INCREF(func); 2450 sp = stack_pointer; 2451 READ_TIMESTAMP(intr0); 2452 x = ext_do_call(func, &sp, flags, na, nk); 2453 READ_TIMESTAMP(intr1); 2454 stack_pointer = sp; 2455 Py_DECREF(func); 2456 2457 while (stack_pointer > pfunc) { 2458 w = POP(); 2459 Py_DECREF(w); 2460 } 2461 PUSH(x); 2462 if (x != NULL) 2463 continue; 2464 break; 2465 } 2466 2467 case MAKE_FUNCTION: 2468 v = POP(); /* code object */ 2469 x = PyFunction_New(v, f->f_globals); 2470 Py_DECREF(v); 2471 /* XXX Maybe this should be a separate opcode? */ 2472 if (x != NULL && oparg > 0) { 2473 v = PyTuple_New(oparg); 2474 if (v == NULL) { 2475 Py_DECREF(x); 2476 x = NULL; 2477 break; 2478 } 2479 while (--oparg >= 0) { 2480 w = POP(); 2481 PyTuple_SET_ITEM(v, oparg, w); 2482 } 2483 err = PyFunction_SetDefaults(x, v); 2484 Py_DECREF(v); 2485 } 2486 PUSH(x); 2487 break; 2488 2489 case MAKE_CLOSURE: 2490 { 2491 v = POP(); /* code object */ 2492 x = PyFunction_New(v, f->f_globals); 2493 Py_DECREF(v); 2494 if (x != NULL) { 2495 v = POP(); 2496 if (PyFunction_SetClosure(x, v) != 0) { 2497 /* Can't happen unless bytecode is corrupt. */ 2498 why = WHY_EXCEPTION; 2499 } 2500 Py_DECREF(v); 2501 } 2502 if (x != NULL && oparg > 0) { 2503 v = PyTuple_New(oparg); 2504 if (v == NULL) { 2505 Py_DECREF(x); 2506 x = NULL; 2507 break; 2508 } 2509 while (--oparg >= 0) { 2510 w = POP(); 2511 PyTuple_SET_ITEM(v, oparg, w); 2512 } 2513 if (PyFunction_SetDefaults(x, v) != 0) { 2514 /* Can't happen unless 2515 PyFunction_SetDefaults changes. */ 2516 why = WHY_EXCEPTION; 2517 } 2518 Py_DECREF(v); 2519 } 2520 PUSH(x); 2521 break; 2522 } 2523 2524 case BUILD_SLICE: 2525 if (oparg == 3) 2526 w = POP(); 2527 else 2528 w = NULL; 2529 v = POP(); 2530 u = TOP(); 2531 x = PySlice_New(u, v, w); 2532 Py_DECREF(u); 2533 Py_DECREF(v); 2534 Py_XDECREF(w); 2535 SET_TOP(x); 2536 if (x != NULL) continue; 2537 break; 2538 2539 case EXTENDED_ARG: 2540 opcode = NEXTOP(); 2541 oparg = oparg<<16 | NEXTARG(); 2542 goto dispatch_opcode; 2543 2544 default: 2545 fprintf(stderr, 2546 "XXX lineno: %d, opcode: %d\n", 2547 PyCode_Addr2Line(f->f_code, f->f_lasti), 2548 opcode); 2549 PyErr_SetString(PyExc_SystemError, "unknown opcode"); 2550 why = WHY_EXCEPTION; 2551 break; 2668 stack_pointer = sp; 2669 PUSH(x); 2670 if (x != NULL) 2671 continue; 2672 break; 2673 } 2674 2675 case CALL_FUNCTION_VAR: 2676 case CALL_FUNCTION_KW: 2677 case CALL_FUNCTION_VAR_KW: 2678 { 2679 int na = oparg & 0xff; 2680 int nk = (oparg>>8) & 0xff; 2681 int flags = (opcode - CALL_FUNCTION) & 3; 2682 int n = na + 2 * nk; 2683 PyObject **pfunc, *func, **sp; 2684 PCALL(PCALL_ALL); 2685 if (flags & CALL_FLAG_VAR) 2686 n++; 2687 if (flags & CALL_FLAG_KW) 2688 n++; 2689 pfunc = stack_pointer - n - 1; 2690 func = *pfunc; 2691 2692 if (PyMethod_Check(func) 2693 && PyMethod_GET_SELF(func) != NULL) { 2694 PyObject *self = PyMethod_GET_SELF(func); 2695 Py_INCREF(self); 2696 func = PyMethod_GET_FUNCTION(func); 2697 Py_INCREF(func); 2698 Py_DECREF(*pfunc); 2699 *pfunc = self; 2700 na++; 2701 } else 2702 Py_INCREF(func); 2703 sp = stack_pointer; 2704 READ_TIMESTAMP(intr0); 2705 x = ext_do_call(func, &sp, flags, na, nk); 2706 READ_TIMESTAMP(intr1); 2707 stack_pointer = sp; 2708 Py_DECREF(func); 2709 2710 while (stack_pointer > pfunc) { 2711 w = POP(); 2712 Py_DECREF(w); 2713 } 2714 PUSH(x); 2715 if (x != NULL) 2716 continue; 2717 break; 2718 } 2719 2720 case MAKE_FUNCTION: 2721 v = POP(); /* code object */ 2722 x = PyFunction_New(v, f->f_globals); 2723 Py_DECREF(v); 2724 /* XXX Maybe this should be a separate opcode? */ 2725 if (x != NULL && oparg > 0) { 2726 v = PyTuple_New(oparg); 2727 if (v == NULL) { 2728 Py_DECREF(x); 2729 x = NULL; 2730 break; 2731 } 2732 while (--oparg >= 0) { 2733 w = POP(); 2734 PyTuple_SET_ITEM(v, oparg, w); 2735 } 2736 err = PyFunction_SetDefaults(x, v); 2737 Py_DECREF(v); 2738 } 2739 PUSH(x); 2740 break; 2741 2742 case MAKE_CLOSURE: 2743 { 2744 v = POP(); /* code object */ 2745 x = PyFunction_New(v, f->f_globals); 2746 Py_DECREF(v); 2747 if (x != NULL) { 2748 v = POP(); 2749 if (PyFunction_SetClosure(x, v) != 0) { 2750 /* Can't happen unless bytecode is corrupt. */ 2751 why = WHY_EXCEPTION; 2752 } 2753 Py_DECREF(v); 2754 } 2755 if (x != NULL && oparg > 0) { 2756 v = PyTuple_New(oparg); 2757 if (v == NULL) { 2758 Py_DECREF(x); 2759 x = NULL; 2760 break; 2761 } 2762 while (--oparg >= 0) { 2763 w = POP(); 2764 PyTuple_SET_ITEM(v, oparg, w); 2765 } 2766 if (PyFunction_SetDefaults(x, v) != 0) { 2767 /* Can't happen unless 2768 PyFunction_SetDefaults changes. */ 2769 why = WHY_EXCEPTION; 2770 } 2771 Py_DECREF(v); 2772 } 2773 PUSH(x); 2774 break; 2775 } 2776 2777 case BUILD_SLICE: 2778 if (oparg == 3) 2779 w = POP(); 2780 else 2781 w = NULL; 2782 v = POP(); 2783 u = TOP(); 2784 x = PySlice_New(u, v, w); 2785 Py_DECREF(u); 2786 Py_DECREF(v); 2787 Py_XDECREF(w); 2788 SET_TOP(x); 2789 if (x != NULL) continue; 2790 break; 2791 2792 case EXTENDED_ARG: 2793 opcode = NEXTOP(); 2794 oparg = oparg<<16 | NEXTARG(); 2795 goto dispatch_opcode; 2796 2797 default: 2798 fprintf(stderr, 2799 "XXX lineno: %d, opcode: %d\n", 2800 PyFrame_GetLineNumber(f), 2801 opcode); 2802 PyErr_SetString(PyExc_SystemError, "unknown opcode"); 2803 why = WHY_EXCEPTION; 2804 break; 2552 2805 2553 2806 #ifdef CASE_TOO_BIG 2554 2807 } 2555 2808 #endif 2556 2809 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2810 } /* switch */ 2811 2812 on_error: 2813 2814 READ_TIMESTAMP(inst1); 2815 2816 /* Quickly continue if no error occurred */ 2817 2818 if (why == WHY_NOT) { 2819 if (err == 0 && x != NULL) { 2567 2820 #ifdef CHECKEXC 2568 2569 2570 2571 2572 2821 /* This check is expensive! */ 2822 if (PyErr_Occurred()) 2823 fprintf(stderr, 2824 "XXX undetected error\n"); 2825 else { 2573 2826 #endif 2574 2575 2827 READ_TIMESTAMP(loop1); 2828 continue; /* Normal, fast path */ 2576 2829 #ifdef CHECKEXC 2577 2830 } 2578 2831 #endif 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2832 } 2833 why = WHY_EXCEPTION; 2834 x = Py_None; 2835 err = 0; 2836 } 2837 2838 /* Double-check exception status */ 2839 2840 if (why == WHY_EXCEPTION || why == WHY_RERAISE) { 2841 if (!PyErr_Occurred()) { 2842 PyErr_SetString(PyExc_SystemError, 2843 "error return without exception set"); 2844 why = WHY_EXCEPTION; 2845 } 2846 } 2594 2847 #ifdef CHECKEXC 2595 2596 2597 2598 2599 2600 2601 2602 2603 2848 else { 2849 /* This check is expensive! */ 2850 if (PyErr_Occurred()) { 2851 char buf[128]; 2852 sprintf(buf, "Stack unwind with exception " 2853 "set and why=%d", why); 2854 Py_FatalError(buf); 2855 } 2856 } 2604 2857 #endif 2605 2858 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2859 /* Log traceback info if this is a real exception */ 2860 2861 if (why == WHY_EXCEPTION) { 2862 PyTraceBack_Here(f); 2863 2864 if (tstate->c_tracefunc != NULL) 2865 call_exc_trace(tstate->c_tracefunc, 2866 tstate->c_traceobj, f); 2867 } 2868 2869 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */ 2870 2871 if (why == WHY_RERAISE) 2872 why = WHY_EXCEPTION; 2873 2874 /* Unwind stacks if a (pseudo) exception occurred */ 2622 2875 2623 2876 fast_block_end: 2624 while (why != WHY_NOT && f->f_iblock > 0) { 2625 PyTryBlock *b = PyFrame_BlockPop(f); 2626 2627 assert(why != WHY_YIELD); 2628 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) { 2629 /* For a continue inside a try block, 2630 don't pop the block for the loop. */ 2631 PyFrame_BlockSetup(f, b->b_type, b->b_handler, 2632 b->b_level); 2633 why = WHY_NOT; 2634 JUMPTO(PyInt_AS_LONG(retval)); 2635 Py_DECREF(retval); 2636 break; 2637 } 2638 2639 while (STACK_LEVEL() > b->b_level) { 2640 v = POP(); 2641 Py_XDECREF(v); 2642 } 2643 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { 2644 why = WHY_NOT; 2645 JUMPTO(b->b_handler); 2646 break; 2647 } 2648 if (b->b_type == SETUP_FINALLY || 2649 (b->b_type == SETUP_EXCEPT && 2650 why == WHY_EXCEPTION)) { 2651 if (why == WHY_EXCEPTION) { 2652 PyObject *exc, *val, *tb; 2653 PyErr_Fetch(&exc, &val, &tb); 2654 if (val == NULL) { 2655 val = Py_None; 2656 Py_INCREF(val); 2657 } 2658 /* Make the raw exception data 2659 available to the handler, 2660 so a program can emulate the 2661 Python main loop. Don't do 2662 this for 'finally'. */ 2663 if (b->b_type == SETUP_EXCEPT) { 2664 PyErr_NormalizeException( 2665 &exc, &val, &tb); 2666 set_exc_info(tstate, 2667 exc, val, tb); 2668 } 2669 if (tb == NULL) { 2670 Py_INCREF(Py_None); 2671 PUSH(Py_None); 2672 } else 2673 PUSH(tb); 2674 PUSH(val); 2675 PUSH(exc); 2676 } 2677 else { 2678 if (why & (WHY_RETURN | WHY_CONTINUE)) 2679 PUSH(retval); 2680 v = PyInt_FromLong((long)why); 2681 PUSH(v); 2682 } 2683 why = WHY_NOT; 2684 JUMPTO(b->b_handler); 2685 break; 2686 } 2687 } /* unwind stack */ 2688 2689 /* End the loop if we still have an error (or return) */ 2690 2691 if (why != WHY_NOT) 2692 break; 2693 READ_TIMESTAMP(loop1); 2694 2695 } /* main loop */ 2696 2697 assert(why != WHY_YIELD); 2698 /* Pop remaining stack entries. */ 2699 while (!EMPTY()) { 2700 v = POP(); 2701 Py_XDECREF(v); 2702 } 2703 2704 if (why != WHY_RETURN) 2705 retval = NULL; 2877 while (why != WHY_NOT && f->f_iblock > 0) { 2878 /* Peek at the current block. */ 2879 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1]; 2880 2881 assert(why != WHY_YIELD); 2882 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) { 2883 why = WHY_NOT; 2884 JUMPTO(PyInt_AS_LONG(retval)); 2885 Py_DECREF(retval); 2886 break; 2887 } 2888 2889 /* Now we have to pop the block. */ 2890 f->f_iblock--; 2891 2892 while (STACK_LEVEL() > b->b_level) { 2893 v = POP(); 2894 Py_XDECREF(v); 2895 } 2896 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { 2897 why = WHY_NOT; 2898 JUMPTO(b->b_handler); 2899 break; 2900 } 2901 if (b->b_type == SETUP_FINALLY || 2902 (b->b_type == SETUP_EXCEPT && 2903 why == WHY_EXCEPTION) || 2904 b->b_type == SETUP_WITH) { 2905 if (why == WHY_EXCEPTION) { 2906 PyObject *exc, *val, *tb; 2907 PyErr_Fetch(&exc, &val, &tb); 2908 if (val == NULL) { 2909 val = Py_None; 2910 Py_INCREF(val); 2911 } 2912 /* Make the raw exception data 2913 available to the handler, 2914 so a program can emulate the 2915 Python main loop. Don't do 2916 this for 'finally'. */ 2917 if (b->b_type == SETUP_EXCEPT || 2918 b->b_type == SETUP_WITH) { 2919 PyErr_NormalizeException( 2920 &exc, &val, &tb); 2921 set_exc_info(tstate, 2922 exc, val, tb); 2923 } 2924 if (tb == NULL) { 2925 Py_INCREF(Py_None); 2926 PUSH(Py_None); 2927 } else 2928 PUSH(tb); 2929 PUSH(val); 2930 PUSH(exc); 2931 } 2932 else { 2933 if (why & (WHY_RETURN | WHY_CONTINUE)) 2934 PUSH(retval); 2935 v = PyInt_FromLong((long)why); 2936 PUSH(v); 2937 } 2938 why = WHY_NOT; 2939 JUMPTO(b->b_handler); 2940 break; 2941 } 2942 } /* unwind stack */ 2943 2944 /* End the loop if we still have an error (or return) */ 2945 2946 if (why != WHY_NOT) 2947 break; 2948 READ_TIMESTAMP(loop1); 2949 2950 } /* main loop */ 2951 2952 assert(why != WHY_YIELD); 2953 /* Pop remaining stack entries. */ 2954 while (!EMPTY()) { 2955 v = POP(); 2956 Py_XDECREF(v); 2957 } 2958 2959 if (why != WHY_RETURN) 2960 retval = NULL; 2706 2961 2707 2962 fast_yield: 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2963 if (tstate->use_tracing) { 2964 if (tstate->c_tracefunc) { 2965 if (why == WHY_RETURN || why == WHY_YIELD) { 2966 if (call_trace(tstate->c_tracefunc, 2967 tstate->c_traceobj, f, 2968 PyTrace_RETURN, retval)) { 2969 Py_XDECREF(retval); 2970 retval = NULL; 2971 why = WHY_EXCEPTION; 2972 } 2973 } 2974 else if (why == WHY_EXCEPTION) { 2975 call_trace_protected(tstate->c_tracefunc, 2976 tstate->c_traceobj, f, 2977 PyTrace_RETURN, NULL); 2978 } 2979 } 2980 if (tstate->c_profilefunc) { 2981 if (why == WHY_EXCEPTION) 2982 call_trace_protected(tstate->c_profilefunc, 2983 tstate->c_profileobj, f, 2984 PyTrace_RETURN, NULL); 2985 else if (call_trace(tstate->c_profilefunc, 2986 tstate->c_profileobj, f, 2987 PyTrace_RETURN, retval)) { 2988 Py_XDECREF(retval); 2989 retval = NULL; 2990 why = WHY_EXCEPTION; 2991 } 2992 } 2993 } 2994 2995 if (tstate->frame->f_exc_type != NULL) 2996 reset_exc_info(tstate); 2997 else { 2998 assert(tstate->frame->f_exc_value == NULL); 2999 assert(tstate->frame->f_exc_traceback == NULL); 3000 } 3001 3002 /* pop frame */ 2748 3003 exit_eval_frame: 2749 2750 2751 2752 3004 Py_LeaveRecursiveCall(); 3005 tstate->frame = f->f_back; 3006 3007 return retval; 2753 3008 } 2754 3009 … … 2759 3014 PyObject * 2760 3015 PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals, 2761 PyObject **args, int argcount, PyObject **kws, int kwcount, 2762 PyObject **defs, int defcount, PyObject *closure) 2763 { 2764 register PyFrameObject *f; 2765 register PyObject *retval = NULL; 2766 register PyObject **fastlocals, **freevars; 2767 PyThreadState *tstate = PyThreadState_GET(); 2768 PyObject *x, *u; 2769 2770 if (globals == NULL) { 2771 PyErr_SetString(PyExc_SystemError, 2772 "PyEval_EvalCodeEx: NULL globals"); 2773 return NULL; 2774 } 2775 2776 assert(tstate != NULL); 2777 assert(globals != NULL); 2778 f = PyFrame_New(tstate, co, globals, locals); 2779 if (f == NULL) 2780 return NULL; 2781 2782 fastlocals = f->f_localsplus; 2783 freevars = f->f_localsplus + co->co_nlocals; 2784 2785 if (co->co_argcount > 0 || 2786 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) { 2787 int i; 2788 int n = argcount; 2789 PyObject *kwdict = NULL; 2790 if (co->co_flags & CO_VARKEYWORDS) { 2791 kwdict = PyDict_New(); 2792 if (kwdict == NULL) 2793 goto fail; 2794 i = co->co_argcount; 2795 if (co->co_flags & CO_VARARGS) 2796 i++; 2797 SETLOCAL(i, kwdict); 2798 } 2799 if (argcount > co->co_argcount) { 2800 if (!(co->co_flags & CO_VARARGS)) { 2801 PyErr_Format(PyExc_TypeError, 2802 "%.200s() takes %s %d " 2803 "%sargument%s (%d given)", 2804 PyString_AsString(co->co_name), 2805 defcount ? "at most" : "exactly", 2806 co->co_argcount, 2807 kwcount ? "non-keyword " : "", 2808 co->co_argcount == 1 ? "" : "s", 2809 argcount); 2810 goto fail; 2811 } 2812 n = co->co_argcount; 2813 } 2814 for (i = 0; i < n; i++) { 2815 x = args[i]; 2816 Py_INCREF(x); 2817 SETLOCAL(i, x); 2818 } 2819 if (co->co_flags & CO_VARARGS) { 2820 u = PyTuple_New(argcount - n); 2821 if (u == NULL) 2822 goto fail; 2823 SETLOCAL(co->co_argcount, u); 2824 for (i = n; i < argcount; i++) { 2825 x = args[i]; 2826 Py_INCREF(x); 2827 PyTuple_SET_ITEM(u, i-n, x); 2828 } 2829 } 2830 for (i = 0; i < kwcount; i++) { 2831 PyObject **co_varnames; 2832 PyObject *keyword = kws[2*i]; 2833 PyObject *value = kws[2*i + 1]; 2834 int j; 2835 if (keyword == NULL || !(PyString_Check(keyword) || 2836 PyUnicode_Check(keyword))) { 2837 PyErr_Format(PyExc_TypeError, 2838 "%.200s() keywords must be strings", 2839 PyString_AsString(co->co_name)); 2840 goto fail; 2841 } 2842 /* Speed hack: do raw pointer compares. As names are 2843 normally interned this should almost always hit. */ 2844 co_varnames = PySequence_Fast_ITEMS(co->co_varnames); 2845 for (j = 0; j < co->co_argcount; j++) { 2846 PyObject *nm = co_varnames[j]; 2847 if (nm == keyword) 2848 goto kw_found; 2849 } 2850 /* Slow fallback, just in case */ 2851 for (j = 0; j < co->co_argcount; j++) { 2852 PyObject *nm = co_varnames[j]; 2853 int cmp = PyObject_RichCompareBool( 2854 keyword, nm, Py_EQ); 2855 if (cmp > 0) 2856 goto kw_found; 2857 else if (cmp < 0) 2858 goto fail; 2859 } 2860 /* Check errors from Compare */ 2861 if (PyErr_Occurred()) 2862 goto fail; 2863 if (j >= co->co_argcount) { 2864 if (kwdict == NULL) { 2865 PyObject *kwd_str = kwd_as_string(keyword); 2866 if (kwd_str) { 2867 PyErr_Format(PyExc_TypeError, 2868 "%.200s() got an unexpected " 2869 "keyword argument '%.400s'", 2870 PyString_AsString(co->co_name), 2871 PyString_AsString(kwd_str)); 2872 Py_DECREF(kwd_str); 2873 } 2874 goto fail; 2875 } 2876 PyDict_SetItem(kwdict, keyword, value); 2877 continue; 2878 } 2879 kw_found: 2880 if (GETLOCAL(j) != NULL) { 2881 PyObject *kwd_str = kwd_as_string(keyword); 2882 if (kwd_str) { 2883 PyErr_Format(PyExc_TypeError, 2884 "%.200s() got multiple " 2885 "values for keyword " 2886 "argument '%.400s'", 2887 PyString_AsString(co->co_name), 2888 PyString_AsString(kwd_str)); 2889 Py_DECREF(kwd_str); 2890 } 2891 goto fail; 2892 } 2893 Py_INCREF(value); 2894 SETLOCAL(j, value); 2895 } 2896 if (argcount < co->co_argcount) { 2897 int m = co->co_argcount - defcount; 2898 for (i = argcount; i < m; i++) { 2899 if (GETLOCAL(i) == NULL) { 2900 PyErr_Format(PyExc_TypeError, 2901 "%.200s() takes %s %d " 2902 "%sargument%s (%d given)", 2903 PyString_AsString(co->co_name), 2904 ((co->co_flags & CO_VARARGS) || 2905 defcount) ? "at least" 2906 : "exactly", 2907 m, kwcount ? "non-keyword " : "", 2908 m == 1 ? "" : "s", i); 2909 goto fail; 2910 } 2911 } 2912 if (n > m) 2913 i = n - m; 2914 else 2915 i = 0; 2916 for (; i < defcount; i++) { 2917 if (GETLOCAL(m+i) == NULL) { 2918 PyObject *def = defs[i]; 2919 Py_INCREF(def); 2920 SETLOCAL(m+i, def); 2921 } 2922 } 2923 } 2924 } 2925 else { 2926 if (argcount > 0 || kwcount > 0) { 2927 PyErr_Format(PyExc_TypeError, 2928 "%.200s() takes no arguments (%d given)", 2929 PyString_AsString(co->co_name), 2930 argcount + kwcount); 2931 goto fail; 2932 } 2933 } 2934 /* Allocate and initialize storage for cell vars, and copy free 2935 vars into frame. This isn't too efficient right now. */ 2936 if (PyTuple_GET_SIZE(co->co_cellvars)) { 2937 int i, j, nargs, found; 2938 char *cellname, *argname; 2939 PyObject *c; 2940 2941 nargs = co->co_argcount; 2942 if (co->co_flags & CO_VARARGS) 2943 nargs++; 2944 if (co->co_flags & CO_VARKEYWORDS) 2945 nargs++; 2946 2947 /* Initialize each cell var, taking into account 2948 cell vars that are initialized from arguments. 2949 2950 Should arrange for the compiler to put cellvars 2951 that are arguments at the beginning of the cellvars 2952 list so that we can march over it more efficiently? 2953 */ 2954 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { 2955 cellname = PyString_AS_STRING( 2956 PyTuple_GET_ITEM(co->co_cellvars, i)); 2957 found = 0; 2958 for (j = 0; j < nargs; j++) { 2959 argname = PyString_AS_STRING( 2960 PyTuple_GET_ITEM(co->co_varnames, j)); 2961 if (strcmp(cellname, argname) == 0) { 2962 c = PyCell_New(GETLOCAL(j)); 2963 if (c == NULL) 2964 goto fail; 2965 GETLOCAL(co->co_nlocals + i) = c; 2966 found = 1; 2967 break; 2968 } 2969 } 2970 if (found == 0) { 2971 c = PyCell_New(NULL); 2972 if (c == NULL) 2973 goto fail; 2974 SETLOCAL(co->co_nlocals + i, c); 2975 } 2976 } 2977 } 2978 if (PyTuple_GET_SIZE(co->co_freevars)) { 2979 int i; 2980 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { 2981 PyObject *o = PyTuple_GET_ITEM(closure, i); 2982 Py_INCREF(o); 2983 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; 2984 } 2985 } 2986 2987 if (co->co_flags & CO_GENERATOR) { 2988 /* Don't need to keep the reference to f_back, it will be set 2989 * when the generator is resumed. */ 2990 Py_XDECREF(f->f_back); 2991 f->f_back = NULL; 2992 2993 PCALL(PCALL_GENERATOR); 2994 2995 /* Create a new generator that owns the ready to run frame 2996 * and return that as the value. */ 2997 return PyGen_New(f); 2998 } 2999 3000 retval = PyEval_EvalFrameEx(f,0); 3016 PyObject **args, int argcount, PyObject **kws, int kwcount, 3017 PyObject **defs, int defcount, PyObject *closure) 3018 { 3019 register PyFrameObject *f; 3020 register PyObject *retval = NULL; 3021 register PyObject **fastlocals, **freevars; 3022 PyThreadState *tstate = PyThreadState_GET(); 3023 PyObject *x, *u; 3024 3025 if (globals == NULL) { 3026 PyErr_SetString(PyExc_SystemError, 3027 "PyEval_EvalCodeEx: NULL globals"); 3028 return NULL; 3029 } 3030 3031 assert(tstate != NULL); 3032 assert(globals != NULL); 3033 f = PyFrame_New(tstate, co, globals, locals); 3034 if (f == NULL) 3035 return NULL; 3036 3037 fastlocals = f->f_localsplus; 3038 freevars = f->f_localsplus + co->co_nlocals; 3039 3040 if (co->co_argcount > 0 || 3041 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) { 3042 int i; 3043 int n = argcount; 3044 PyObject *kwdict = NULL; 3045 if (co->co_flags & CO_VARKEYWORDS) { 3046 kwdict = PyDict_New(); 3047 if (kwdict == NULL) 3048 goto fail; 3049 i = co->co_argcount; 3050 if (co->co_flags & CO_VARARGS) 3051 i++; 3052 SETLOCAL(i, kwdict); 3053 } 3054 if (argcount > co->co_argcount) { 3055 if (!(co->co_flags & CO_VARARGS)) { 3056 PyErr_Format(PyExc_TypeError, 3057 "%.200s() takes %s %d " 3058 "argument%s (%d given)", 3059 PyString_AsString(co->co_name), 3060 defcount ? "at most" : "exactly", 3061 co->co_argcount, 3062 co->co_argcount == 1 ? "" : "s", 3063 argcount + kwcount); 3064 goto fail; 3065 } 3066 n = co->co_argcount; 3067 } 3068 for (i = 0; i < n; i++) { 3069 x = args[i]; 3070 Py_INCREF(x); 3071 SETLOCAL(i, x); 3072 } 3073 if (co->co_flags & CO_VARARGS) { 3074 u = PyTuple_New(argcount - n); 3075 if (u == NULL) 3076 goto fail; 3077 SETLOCAL(co->co_argcount, u); 3078 for (i = n; i < argcount; i++) { 3079 x = args[i]; 3080 Py_INCREF(x); 3081 PyTuple_SET_ITEM(u, i-n, x); 3082 } 3083 } 3084 for (i = 0; i < kwcount; i++) { 3085 PyObject **co_varnames; 3086 PyObject *keyword = kws[2*i]; 3087 PyObject *value = kws[2*i + 1]; 3088 int j; 3089 if (keyword == NULL || !(PyString_Check(keyword) 3090 #ifdef Py_USING_UNICODE 3091 || PyUnicode_Check(keyword) 3092 #endif 3093 )) { 3094 PyErr_Format(PyExc_TypeError, 3095 "%.200s() keywords must be strings", 3096 PyString_AsString(co->co_name)); 3097 goto fail; 3098 } 3099 /* Speed hack: do raw pointer compares. As names are 3100 normally interned this should almost always hit. */ 3101 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; 3102 for (j = 0; j < co->co_argcount; j++) { 3103 PyObject *nm = co_varnames[j]; 3104 if (nm == keyword) 3105 goto kw_found; 3106 } 3107 /* Slow fallback, just in case */ 3108 for (j = 0; j < co->co_argcount; j++) { 3109 PyObject *nm = co_varnames[j]; 3110 int cmp = PyObject_RichCompareBool( 3111 keyword, nm, Py_EQ); 3112 if (cmp > 0) 3113 goto kw_found; 3114 else if (cmp < 0) 3115 goto fail; 3116 } 3117 if (kwdict == NULL) { 3118 PyObject *kwd_str = kwd_as_string(keyword); 3119 if (kwd_str) { 3120 PyErr_Format(PyExc_TypeError, 3121 "%.200s() got an unexpected " 3122 "keyword argument '%.400s'", 3123 PyString_AsString(co->co_name), 3124 PyString_AsString(kwd_str)); 3125 Py_DECREF(kwd_str); 3126 } 3127 goto fail; 3128 } 3129 PyDict_SetItem(kwdict, keyword, value); 3130 continue; 3131 kw_found: 3132 if (GETLOCAL(j) != NULL) { 3133 PyObject *kwd_str = kwd_as_string(keyword); 3134 if (kwd_str) { 3135 PyErr_Format(PyExc_TypeError, 3136 "%.200s() got multiple " 3137 "values for keyword " 3138 "argument '%.400s'", 3139 PyString_AsString(co->co_name), 3140 PyString_AsString(kwd_str)); 3141 Py_DECREF(kwd_str); 3142 } 3143 goto fail; 3144 } 3145 Py_INCREF(value); 3146 SETLOCAL(j, value); 3147 } 3148 if (argcount < co->co_argcount) { 3149 int m = co->co_argcount - defcount; 3150 for (i = argcount; i < m; i++) { 3151 if (GETLOCAL(i) == NULL) { 3152 int j, given = 0; 3153 for (j = 0; j < co->co_argcount; j++) 3154 if (GETLOCAL(j)) 3155 given++; 3156 PyErr_Format(PyExc_TypeError, 3157 "%.200s() takes %s %d " 3158 "argument%s (%d given)", 3159 PyString_AsString(co->co_name), 3160 ((co->co_flags & CO_VARARGS) || 3161 defcount) ? "at least" 3162 : "exactly", 3163 m, m == 1 ? "" : "s", given); 3164 goto fail; 3165 } 3166 } 3167 if (n > m) 3168 i = n - m; 3169 else 3170 i = 0; 3171 for (; i < defcount; i++) { 3172 if (GETLOCAL(m+i) == NULL) { 3173 PyObject *def = defs[i]; 3174 Py_INCREF(def); 3175 SETLOCAL(m+i, def); 3176 } 3177 } 3178 } 3179 } 3180 else if (argcount > 0 || kwcount > 0) { 3181 PyErr_Format(PyExc_TypeError, 3182 "%.200s() takes no arguments (%d given)", 3183 PyString_AsString(co->co_name), 3184 argcount + kwcount); 3185 goto fail; 3186 } 3187 /* Allocate and initialize storage for cell vars, and copy free 3188 vars into frame. This isn't too efficient right now. */ 3189 if (PyTuple_GET_SIZE(co->co_cellvars)) { 3190 int i, j, nargs, found; 3191 char *cellname, *argname; 3192 PyObject *c; 3193 3194 nargs = co->co_argcount; 3195 if (co->co_flags & CO_VARARGS) 3196 nargs++; 3197 if (co->co_flags & CO_VARKEYWORDS) 3198 nargs++; 3199 3200 /* Initialize each cell var, taking into account 3201 cell vars that are initialized from arguments. 3202 3203 Should arrange for the compiler to put cellvars 3204 that are arguments at the beginning of the cellvars 3205 list so that we can march over it more efficiently? 3206 */ 3207 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { 3208 cellname = PyString_AS_STRING( 3209 PyTuple_GET_ITEM(co->co_cellvars, i)); 3210 found = 0; 3211 for (j = 0; j < nargs; j++) { 3212 argname = PyString_AS_STRING( 3213 PyTuple_GET_ITEM(co->co_varnames, j)); 3214 if (strcmp(cellname, argname) == 0) { 3215 c = PyCell_New(GETLOCAL(j)); 3216 if (c == NULL) 3217 goto fail; 3218 GETLOCAL(co->co_nlocals + i) = c; 3219 found = 1; 3220 break; 3221 } 3222 } 3223 if (found == 0) { 3224 c = PyCell_New(NULL); 3225 if (c == NULL) 3226 goto fail; 3227 SETLOCAL(co->co_nlocals + i, c); 3228 } 3229 } 3230 } 3231 if (PyTuple_GET_SIZE(co->co_freevars)) { 3232 int i; 3233 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { 3234 PyObject *o = PyTuple_GET_ITEM(closure, i); 3235 Py_INCREF(o); 3236 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; 3237 } 3238 } 3239 3240 if (co->co_flags & CO_GENERATOR) { 3241 /* Don't need to keep the reference to f_back, it will be set 3242 * when the generator is resumed. */ 3243 Py_XDECREF(f->f_back); 3244 f->f_back = NULL; 3245 3246 PCALL(PCALL_GENERATOR); 3247 3248 /* Create a new generator that owns the ready to run frame 3249 * and return that as the value. */ 3250 return PyGen_New(f); 3251 } 3252 3253 retval = PyEval_EvalFrameEx(f,0); 3001 3254 3002 3255 fail: /* Jump here from prelude on failure */ 3003 3256 3004 /* decref'ing the frame can cause __del__ methods to get invoked, 3005 which can call back into Python. While we're done with the 3006 current Python frame (f), the associated C stack is still in use, 3007 so recursion_depth must be boosted for the duration. 3008 */ 3009 assert(tstate != NULL); 3010 ++tstate->recursion_depth; 3011 Py_DECREF(f); 3012 --tstate->recursion_depth; 3013 return retval; 3257 /* decref'ing the frame can cause __del__ methods to get invoked, 3258 which can call back into Python. While we're done with the 3259 current Python frame (f), the associated C stack is still in use, 3260 so recursion_depth must be boosted for the duration. 3261 */ 3262 assert(tstate != NULL); 3263 ++tstate->recursion_depth; 3264 Py_DECREF(f); 3265 --tstate->recursion_depth; 3266 return retval; 3267 } 3268 3269 3270 static PyObject * 3271 special_lookup(PyObject *o, char *meth, PyObject **cache) 3272 { 3273 PyObject *res; 3274 if (PyInstance_Check(o)) { 3275 if (!*cache) 3276 return PyObject_GetAttrString(o, meth); 3277 else 3278 return PyObject_GetAttr(o, *cache); 3279 } 3280 res = _PyObject_LookupSpecial(o, meth, cache); 3281 if (res == NULL && !PyErr_Occurred()) { 3282 PyErr_SetObject(PyExc_AttributeError, *cache); 3283 return NULL; 3284 } 3285 return res; 3014 3286 } 3015 3287 … … 3017 3289 static PyObject * 3018 3290 kwd_as_string(PyObject *kwd) { 3019 if (PyString_Check(kwd)) { 3020 Py_INCREF(kwd); 3021 return kwd; 3022 } 3023 else 3024 return _PyUnicode_AsDefaultEncodedString(kwd, "replace"); 3291 #ifdef Py_USING_UNICODE 3292 if (PyString_Check(kwd)) { 3293 #else 3294 assert(PyString_Check(kwd)); 3295 #endif 3296 Py_INCREF(kwd); 3297 return kwd; 3298 #ifdef Py_USING_UNICODE 3299 } 3300 return _PyUnicode_AsDefaultEncodedString(kwd, "replace"); 3301 #endif 3025 3302 } 3026 3303 … … 3049 3326 3050 3327 try: 3051 3328 "something that may fail" 3052 3329 except "some exception": 3053 3054 3330 "do something else first" 3331 "print the exception from sys.exc_ZZZ." 3055 3332 3056 3333 if "do something else first" invoked something that raised and caught … … 3090 3367 static void 3091 3368 set_exc_info(PyThreadState *tstate, 3092 3093 { 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3369 PyObject *type, PyObject *value, PyObject *tb) 3370 { 3371 PyFrameObject *frame = tstate->frame; 3372 PyObject *tmp_type, *tmp_value, *tmp_tb; 3373 3374 assert(type != NULL); 3375 assert(frame != NULL); 3376 if (frame->f_exc_type == NULL) { 3377 assert(frame->f_exc_value == NULL); 3378 assert(frame->f_exc_traceback == NULL); 3379 /* This frame didn't catch an exception before. */ 3380 /* Save previous exception of this thread in this frame. */ 3381 if (tstate->exc_type == NULL) { 3382 /* XXX Why is this set to Py_None? */ 3383 Py_INCREF(Py_None); 3384 tstate->exc_type = Py_None; 3385 } 3386 Py_INCREF(tstate->exc_type); 3387 Py_XINCREF(tstate->exc_value); 3388 Py_XINCREF(tstate->exc_traceback); 3389 frame->f_exc_type = tstate->exc_type; 3390 frame->f_exc_value = tstate->exc_value; 3391 frame->f_exc_traceback = tstate->exc_traceback; 3392 } 3393 /* Set new exception for this thread. */ 3394 tmp_type = tstate->exc_type; 3395 tmp_value = tstate->exc_value; 3396 tmp_tb = tstate->exc_traceback; 3397 Py_INCREF(type); 3398 Py_XINCREF(value); 3399 Py_XINCREF(tb); 3400 tstate->exc_type = type; 3401 tstate->exc_value = value; 3402 tstate->exc_traceback = tb; 3403 Py_XDECREF(tmp_type); 3404 Py_XDECREF(tmp_value); 3405 Py_XDECREF(tmp_tb); 3406 /* For b/w compatibility */ 3407 PySys_SetObject("exc_type", type); 3408 PySys_SetObject("exc_value", value); 3409 PySys_SetObject("exc_traceback", tb); 3133 3410 } 3134 3411 … … 3136 3413 reset_exc_info(PyThreadState *tstate) 3137 3414 { 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3415 PyFrameObject *frame; 3416 PyObject *tmp_type, *tmp_value, *tmp_tb; 3417 3418 /* It's a precondition that the thread state's frame caught an 3419 * exception -- verify in a debug build. 3420 */ 3421 assert(tstate != NULL); 3422 frame = tstate->frame; 3423 assert(frame != NULL); 3424 assert(frame->f_exc_type != NULL); 3425 3426 /* Copy the frame's exception info back to the thread state. */ 3427 tmp_type = tstate->exc_type; 3428 tmp_value = tstate->exc_value; 3429 tmp_tb = tstate->exc_traceback; 3430 Py_INCREF(frame->f_exc_type); 3431 Py_XINCREF(frame->f_exc_value); 3432 Py_XINCREF(frame->f_exc_traceback); 3433 tstate->exc_type = frame->f_exc_type; 3434 tstate->exc_value = frame->f_exc_value; 3435 tstate->exc_traceback = frame->f_exc_traceback; 3436 Py_XDECREF(tmp_type); 3437 Py_XDECREF(tmp_value); 3438 Py_XDECREF(tmp_tb); 3439 3440 /* For b/w compatibility */ 3441 PySys_SetObject("exc_type", frame->f_exc_type); 3442 PySys_SetObject("exc_value", frame->f_exc_value); 3443 PySys_SetObject("exc_traceback", frame->f_exc_traceback); 3444 3445 /* Clear the frame's exception info. */ 3446 tmp_type = frame->f_exc_type; 3447 tmp_value = frame->f_exc_value; 3448 tmp_tb = frame->f_exc_traceback; 3449 frame->f_exc_type = NULL; 3450 frame->f_exc_value = NULL; 3451 frame->f_exc_traceback = NULL; 3452 Py_DECREF(tmp_type); 3453 Py_XDECREF(tmp_value); 3454 Py_XDECREF(tmp_tb); 3178 3455 } 3179 3456 … … 3183 3460 do_raise(PyObject *type, PyObject *value, PyObject *tb) 3184 3461 { 3185 if (type == NULL) { 3186 /* Reraise */ 3187 PyThreadState *tstate = PyThreadState_GET(); 3188 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type; 3189 value = tstate->exc_value; 3190 tb = tstate->exc_traceback; 3191 Py_XINCREF(type); 3192 Py_XINCREF(value); 3193 Py_XINCREF(tb); 3194 } 3195 3196 /* We support the following forms of raise: 3197 raise <class>, <classinstance> 3198 raise <class>, <argument tuple> 3199 raise <class>, None 3200 raise <class>, <argument> 3201 raise <classinstance>, None 3202 raise <string>, <object> 3203 raise <string>, None 3204 3205 An omitted second argument is the same as None. 3206 3207 In addition, raise <tuple>, <anything> is the same as 3208 raising the tuple's first item (and it better have one!); 3209 this rule is applied recursively. 3210 3211 Finally, an optional third argument can be supplied, which 3212 gives the traceback to be substituted (useful when 3213 re-raising an exception after examining it). */ 3214 3215 /* First, check the traceback argument, replacing None with 3216 NULL. */ 3217 if (tb == Py_None) { 3218 Py_DECREF(tb); 3219 tb = NULL; 3220 } 3221 else if (tb != NULL && !PyTraceBack_Check(tb)) { 3222 PyErr_SetString(PyExc_TypeError, 3223 "raise: arg 3 must be a traceback or None"); 3224 goto raise_error; 3225 } 3226 3227 /* Next, replace a missing value with None */ 3228 if (value == NULL) { 3229 value = Py_None; 3230 Py_INCREF(value); 3231 } 3232 3233 /* Next, repeatedly, replace a tuple exception with its first item */ 3234 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) { 3235 PyObject *tmp = type; 3236 type = PyTuple_GET_ITEM(type, 0); 3237 Py_INCREF(type); 3238 Py_DECREF(tmp); 3239 } 3240 3241 if (PyExceptionClass_Check(type)) 3242 PyErr_NormalizeException(&type, &value, &tb); 3243 3244 else if (PyExceptionInstance_Check(type)) { 3245 /* Raising an instance. The value should be a dummy. */ 3246 if (value != Py_None) { 3247 PyErr_SetString(PyExc_TypeError, 3248 "instance exception may not have a separate value"); 3249 goto raise_error; 3250 } 3251 else { 3252 /* Normalize to raise <class>, <instance> */ 3253 Py_DECREF(value); 3254 value = type; 3255 type = PyExceptionInstance_Class(type); 3256 Py_INCREF(type); 3257 } 3258 } 3259 else { 3260 /* Not something you can raise. You get an exception 3261 anyway, just not what you specified :-) */ 3262 PyErr_Format(PyExc_TypeError, 3263 "exceptions must be old-style classes or " 3264 "derived from BaseException, not %s", 3265 type->ob_type->tp_name); 3266 goto raise_error; 3267 } 3268 3269 assert(PyExceptionClass_Check(type)); 3270 if (Py_Py3kWarningFlag && PyClass_Check(type)) { 3271 if (PyErr_WarnEx(PyExc_DeprecationWarning, 3272 "exceptions must derive from BaseException " 3273 "in 3.x", 1) < 0) 3274 goto raise_error; 3275 } 3276 3277 PyErr_Restore(type, value, tb); 3278 if (tb == NULL) 3279 return WHY_EXCEPTION; 3280 else 3281 return WHY_RERAISE; 3462 if (type == NULL) { 3463 /* Reraise */ 3464 PyThreadState *tstate = PyThreadState_GET(); 3465 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type; 3466 value = tstate->exc_value; 3467 tb = tstate->exc_traceback; 3468 Py_XINCREF(type); 3469 Py_XINCREF(value); 3470 Py_XINCREF(tb); 3471 } 3472 3473 /* We support the following forms of raise: 3474 raise <class>, <classinstance> 3475 raise <class>, <argument tuple> 3476 raise <class>, None 3477 raise <class>, <argument> 3478 raise <classinstance>, None 3479 raise <string>, <object> 3480 raise <string>, None 3481 3482 An omitted second argument is the same as None. 3483 3484 In addition, raise <tuple>, <anything> is the same as 3485 raising the tuple's first item (and it better have one!); 3486 this rule is applied recursively. 3487 3488 Finally, an optional third argument can be supplied, which 3489 gives the traceback to be substituted (useful when 3490 re-raising an exception after examining it). */ 3491 3492 /* First, check the traceback argument, replacing None with 3493 NULL. */ 3494 if (tb == Py_None) { 3495 Py_DECREF(tb); 3496 tb = NULL; 3497 } 3498 else if (tb != NULL && !PyTraceBack_Check(tb)) { 3499 PyErr_SetString(PyExc_TypeError, 3500 "raise: arg 3 must be a traceback or None"); 3501 goto raise_error; 3502 } 3503 3504 /* Next, replace a missing value with None */ 3505 if (value == NULL) { 3506 value = Py_None; 3507 Py_INCREF(value); 3508 } 3509 3510 /* Next, repeatedly, replace a tuple exception with its first item */ 3511 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) { 3512 PyObject *tmp = type; 3513 type = PyTuple_GET_ITEM(type, 0); 3514 Py_INCREF(type); 3515 Py_DECREF(tmp); 3516 } 3517 3518 if (PyExceptionClass_Check(type)) { 3519 PyErr_NormalizeException(&type, &value, &tb); 3520 if (!PyExceptionInstance_Check(value)) { 3521 PyErr_Format(PyExc_TypeError, 3522 "calling %s() should have returned an instance of " 3523 "BaseException, not '%s'", 3524 ((PyTypeObject *)type)->tp_name, 3525 Py_TYPE(value)->tp_name); 3526 goto raise_error; 3527 } 3528 } 3529 else if (PyExceptionInstance_Check(type)) { 3530 /* Raising an instance. The value should be a dummy. */ 3531 if (value != Py_None) { 3532 PyErr_SetString(PyExc_TypeError, 3533 "instance exception may not have a separate value"); 3534 goto raise_error; 3535 } 3536 else { 3537 /* Normalize to raise <class>, <instance> */ 3538 Py_DECREF(value); 3539 value = type; 3540 type = PyExceptionInstance_Class(type); 3541 Py_INCREF(type); 3542 } 3543 } 3544 else { 3545 /* Not something you can raise. You get an exception 3546 anyway, just not what you specified :-) */ 3547 PyErr_Format(PyExc_TypeError, 3548 "exceptions must be old-style classes or " 3549 "derived from BaseException, not %s", 3550 type->ob_type->tp_name); 3551 goto raise_error; 3552 } 3553 3554 assert(PyExceptionClass_Check(type)); 3555 if (Py_Py3kWarningFlag && PyClass_Check(type)) { 3556 if (PyErr_WarnEx(PyExc_DeprecationWarning, 3557 "exceptions must derive from BaseException " 3558 "in 3.x", 1) < 0) 3559 goto raise_error; 3560 } 3561 3562 PyErr_Restore(type, value, tb); 3563 if (tb == NULL) 3564 return WHY_EXCEPTION; 3565 else 3566 return WHY_RERAISE; 3282 3567 raise_error: 3283 3284 3285 3286 3568 Py_XDECREF(value); 3569 Py_XDECREF(type); 3570 Py_XDECREF(tb); 3571 return WHY_EXCEPTION; 3287 3572 } 3288 3573 … … 3293 3578 unpack_iterable(PyObject *v, int argcnt, PyObject **sp) 3294 3579 { 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3580 int i = 0; 3581 PyObject *it; /* iter(v) */ 3582 PyObject *w; 3583 3584 assert(v != NULL); 3585 3586 it = PyObject_GetIter(v); 3587 if (it == NULL) 3588 goto Error; 3589 3590 for (; i < argcnt; i++) { 3591 w = PyIter_Next(it); 3592 if (w == NULL) { 3593 /* Iterator done, via error or exhaustion. */ 3594 if (!PyErr_Occurred()) { 3595 PyErr_Format(PyExc_ValueError, 3596 "need more than %d value%s to unpack", 3597 i, i == 1 ? "" : "s"); 3598 } 3599 goto Error; 3600 } 3601 *--sp = w; 3602 } 3603 3604 /* We better have exhausted the iterator now. */ 3605 w = PyIter_Next(it); 3606 if (w == NULL) { 3607 if (PyErr_Occurred()) 3608 goto Error; 3609 Py_DECREF(it); 3610 return 1; 3611 } 3612 Py_DECREF(w); 3613 PyErr_SetString(PyExc_ValueError, "too many values to unpack"); 3614 /* fall through */ 3330 3615 Error: 3331 3332 3333 3334 3616 for (; i > 0; i--, sp++) 3617 Py_DECREF(*sp); 3618 Py_XDECREF(it); 3619 return 0; 3335 3620 } 3336 3621 … … 3340 3625 prtrace(PyObject *v, char *str) 3341 3626 { 3342 3343 3344 3345 3346 3627 printf("%s ", str); 3628 if (PyObject_Print(v, stdout, 0) != 0) 3629 PyErr_Clear(); /* Don't know what else to do */ 3630 printf("\n"); 3631 return 1; 3347 3632 } 3348 3633 #endif … … 3351 3636 call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f) 3352 3637 { 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3638 PyObject *type, *value, *traceback, *arg; 3639 int err; 3640 PyErr_Fetch(&type, &value, &traceback); 3641 if (value == NULL) { 3642 value = Py_None; 3643 Py_INCREF(value); 3644 } 3645 arg = PyTuple_Pack(3, type, value, traceback); 3646 if (arg == NULL) { 3647 PyErr_Restore(type, value, traceback); 3648 return; 3649 } 3650 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg); 3651 Py_DECREF(arg); 3652 if (err == 0) 3653 PyErr_Restore(type, value, traceback); 3654 else { 3655 Py_XDECREF(type); 3656 Py_XDECREF(value); 3657 Py_XDECREF(traceback); 3658 } 3374 3659 } 3375 3660 3376 3661 static int 3377 3662 call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, 3378 3379 { 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3663 int what, PyObject *arg) 3664 { 3665 PyObject *type, *value, *traceback; 3666 int err; 3667 PyErr_Fetch(&type, &value, &traceback); 3668 err = call_trace(func, obj, frame, what, arg); 3669 if (err == 0) 3670 { 3671 PyErr_Restore(type, value, traceback); 3672 return 0; 3673 } 3674 else { 3675 Py_XDECREF(type); 3676 Py_XDECREF(value); 3677 Py_XDECREF(traceback); 3678 return -1; 3679 } 3395 3680 } 3396 3681 3397 3682 static int 3398 3683 call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, 3399 3400 { 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3684 int what, PyObject *arg) 3685 { 3686 register PyThreadState *tstate = frame->f_tstate; 3687 int result; 3688 if (tstate->tracing) 3689 return 0; 3690 tstate->tracing++; 3691 tstate->use_tracing = 0; 3692 result = func(obj, frame, what, arg); 3693 tstate->use_tracing = ((tstate->c_tracefunc != NULL) 3694 || (tstate->c_profilefunc != NULL)); 3695 tstate->tracing--; 3696 return result; 3412 3697 } 3413 3698 … … 3415 3700 _PyEval_CallTracing(PyObject *func, PyObject *args) 3416 3701 { 3417 PyFrameObject *frame = PyEval_GetFrame(); 3418 PyThreadState *tstate = frame->f_tstate; 3419 int save_tracing = tstate->tracing; 3420 int save_use_tracing = tstate->use_tracing; 3421 PyObject *result; 3422 3423 tstate->tracing = 0; 3424 tstate->use_tracing = ((tstate->c_tracefunc != NULL) 3425 || (tstate->c_profilefunc != NULL)); 3426 result = PyObject_Call(func, args, NULL); 3427 tstate->tracing = save_tracing; 3428 tstate->use_tracing = save_use_tracing; 3429 return result; 3430 } 3431 3702 PyFrameObject *frame = PyEval_GetFrame(); 3703 PyThreadState *tstate = frame->f_tstate; 3704 int save_tracing = tstate->tracing; 3705 int save_use_tracing = tstate->use_tracing; 3706 PyObject *result; 3707 3708 tstate->tracing = 0; 3709 tstate->use_tracing = ((tstate->c_tracefunc != NULL) 3710 || (tstate->c_profilefunc != NULL)); 3711 result = PyObject_Call(func, args, NULL); 3712 tstate->tracing = save_tracing; 3713 tstate->use_tracing = save_use_tracing; 3714 return result; 3715 } 3716 3717 /* See Objects/lnotab_notes.txt for a description of how tracing works. */ 3432 3718 static int 3433 3719 maybe_call_line_trace(Py_tracefunc func, PyObject *obj, 3434 PyFrameObject *frame, int *instr_lb, int *instr_ub, 3435 int *instr_prev) 3436 { 3437 int result = 0; 3438 3439 /* If the last instruction executed isn't in the current 3440 instruction window, reset the window. If the last 3441 instruction happens to fall at the start of a line or if it 3442 represents a jump backwards, call the trace function. 3443 */ 3444 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) { 3445 int line; 3446 PyAddrPair bounds; 3447 3448 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, 3449 &bounds); 3450 if (line >= 0) { 3451 frame->f_lineno = line; 3452 result = call_trace(func, obj, frame, 3453 PyTrace_LINE, Py_None); 3454 } 3455 *instr_lb = bounds.ap_lower; 3456 *instr_ub = bounds.ap_upper; 3457 } 3458 else if (frame->f_lasti <= *instr_prev) { 3459 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None); 3460 } 3461 *instr_prev = frame->f_lasti; 3462 return result; 3720 PyFrameObject *frame, int *instr_lb, int *instr_ub, 3721 int *instr_prev) 3722 { 3723 int result = 0; 3724 int line = frame->f_lineno; 3725 3726 /* If the last instruction executed isn't in the current 3727 instruction window, reset the window. 3728 */ 3729 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { 3730 PyAddrPair bounds; 3731 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, 3732 &bounds); 3733 *instr_lb = bounds.ap_lower; 3734 *instr_ub = bounds.ap_upper; 3735 } 3736 /* If the last instruction falls at the start of a line or if 3737 it represents a jump backwards, update the frame's line 3738 number and call the trace function. */ 3739 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { 3740 frame->f_lineno = line; 3741 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None); 3742 } 3743 *instr_prev = frame->f_lasti; 3744 return result; 3463 3745 } 3464 3746 … … 3466 3748 PyEval_SetProfile(Py_tracefunc func, PyObject *arg) 3467 3749 { 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3750 PyThreadState *tstate = PyThreadState_GET(); 3751 PyObject *temp = tstate->c_profileobj; 3752 Py_XINCREF(arg); 3753 tstate->c_profilefunc = NULL; 3754 tstate->c_profileobj = NULL; 3755 /* Must make sure that tracing is not ignored if 'temp' is freed */ 3756 tstate->use_tracing = tstate->c_tracefunc != NULL; 3757 Py_XDECREF(temp); 3758 tstate->c_profilefunc = func; 3759 tstate->c_profileobj = arg; 3760 /* Flag that tracing or profiling is turned on */ 3761 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); 3480 3762 } 3481 3763 … … 3483 3765 PyEval_SetTrace(Py_tracefunc func, PyObject *arg) 3484 3766 { 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3767 PyThreadState *tstate = PyThreadState_GET(); 3768 PyObject *temp = tstate->c_traceobj; 3769 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL); 3770 Py_XINCREF(arg); 3771 tstate->c_tracefunc = NULL; 3772 tstate->c_traceobj = NULL; 3773 /* Must make sure that profiling is not ignored if 'temp' is freed */ 3774 tstate->use_tracing = tstate->c_profilefunc != NULL; 3775 Py_XDECREF(temp); 3776 tstate->c_tracefunc = func; 3777 tstate->c_traceobj = arg; 3778 /* Flag that tracing or profiling is turned on */ 3779 tstate->use_tracing = ((func != NULL) 3780 || (tstate->c_profilefunc != NULL)); 3499 3781 } 3500 3782 … … 3502 3784 PyEval_GetBuiltins(void) 3503 3785 { 3504 3505 3506 3507 3508 3786 PyFrameObject *current_frame = PyEval_GetFrame(); 3787 if (current_frame == NULL) 3788 return PyThreadState_GET()->interp->builtins; 3789 else 3790 return current_frame->f_builtins; 3509 3791 } 3510 3792 … … 3512 3794 PyEval_GetLocals(void) 3513 3795 { 3514 3515 3516 3517 3518 3796 PyFrameObject *current_frame = PyEval_GetFrame(); 3797 if (current_frame == NULL) 3798 return NULL; 3799 PyFrame_FastToLocals(current_frame); 3800 return current_frame->f_locals; 3519 3801 } 3520 3802 … … 3522 3804 PyEval_GetGlobals(void) 3523 3805 { 3524 3525 3526 3527 3528 3806 PyFrameObject *current_frame = PyEval_GetFrame(); 3807 if (current_frame == NULL) 3808 return NULL; 3809 else 3810 return current_frame->f_globals; 3529 3811 } 3530 3812 … … 3532 3814 PyEval_GetFrame(void) 3533 3815 { 3534 3535 3816 PyThreadState *tstate = PyThreadState_GET(); 3817 return _PyThreadState_GetFrame(tstate); 3536 3818 } 3537 3819 … … 3539 3821 PyEval_GetRestricted(void) 3540 3822 { 3541 3542 3823 PyFrameObject *current_frame = PyEval_GetFrame(); 3824 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame); 3543 3825 } 3544 3826 … … 3546 3828 PyEval_MergeCompilerFlags(PyCompilerFlags *cf) 3547 3829 { 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3830 PyFrameObject *current_frame = PyEval_GetFrame(); 3831 int result = cf->cf_flags != 0; 3832 3833 if (current_frame != NULL) { 3834 const int codeflags = current_frame->f_code->co_flags; 3835 const int compilerflags = codeflags & PyCF_MASK; 3836 if (compilerflags) { 3837 result = 1; 3838 cf->cf_flags |= compilerflags; 3839 } 3558 3840 #if 0 /* future keyword */ 3559 3560 3561 3562 3841 if (codeflags & CO_GENERATOR_ALLOWED) { 3842 result = 1; 3843 cf->cf_flags |= CO_GENERATOR_ALLOWED; 3844 } 3563 3845 #endif 3564 3565 3846 } 3847 return result; 3566 3848 } 3567 3849 … … 3569 3851 Py_FlushLine(void) 3570 3852 { 3571 3572 3573 3574 3575 3576 3853 PyObject *f = PySys_GetObject("stdout"); 3854 if (f == NULL) 3855 return 0; 3856 if (!PyFile_SoftSpace(f, 0)) 3857 return 0; 3858 return PyFile_WriteString("\n", f); 3577 3859 } 3578 3860 3579 3861 3580 3862 /* External interface to call any callable object. 3581 The arg must be a tuple or NULL. */ 3582 3583 #undef PyEval_CallObject 3584 /* for backward compatibility: export this interface */ 3585 3586 PyObject * 3587 PyEval_CallObject(PyObject *func, PyObject *arg) 3588 { 3589 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL); 3590 } 3591 #define PyEval_CallObject(func,arg) \ 3592 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL) 3863 The arg must be a tuple or NULL. The kw must be a dict or NULL. */ 3593 3864 3594 3865 PyObject * 3595 3866 PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw) 3596 3867 { 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3868 PyObject *result; 3869 3870 if (arg == NULL) { 3871 arg = PyTuple_New(0); 3872 if (arg == NULL) 3873 return NULL; 3874 } 3875 else if (!PyTuple_Check(arg)) { 3876 PyErr_SetString(PyExc_TypeError, 3877 "argument list must be a tuple"); 3878 return NULL; 3879 } 3880 else 3881 Py_INCREF(arg); 3882 3883 if (kw != NULL && !PyDict_Check(kw)) { 3884 PyErr_SetString(PyExc_TypeError, 3885 "keyword list must be a dictionary"); 3886 Py_DECREF(arg); 3887 return NULL; 3888 } 3889 3890 result = PyObject_Call(func, arg, kw); 3891 Py_DECREF(arg); 3892 return result; 3622 3893 } 3623 3894 … … 3625 3896 PyEval_GetFuncName(PyObject *func) 3626 3897 { 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3898 if (PyMethod_Check(func)) 3899 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); 3900 else if (PyFunction_Check(func)) 3901 return PyString_AsString(((PyFunctionObject*)func)->func_name); 3902 else if (PyCFunction_Check(func)) 3903 return ((PyCFunctionObject*)func)->m_ml->ml_name; 3904 else if (PyClass_Check(func)) 3905 return PyString_AsString(((PyClassObject*)func)->cl_name); 3906 else if (PyInstance_Check(func)) { 3907 return PyString_AsString( 3908 ((PyInstanceObject*)func)->in_class->cl_name); 3909 } else { 3910 return func->ob_type->tp_name; 3911 } 3641 3912 } 3642 3913 … … 3644 3915 PyEval_GetFuncDesc(PyObject *func) 3645 3916 { 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3917 if (PyMethod_Check(func)) 3918 return "()"; 3919 else if (PyFunction_Check(func)) 3920 return "()"; 3921 else if (PyCFunction_Check(func)) 3922 return "()"; 3923 else if (PyClass_Check(func)) 3924 return " constructor"; 3925 else if (PyInstance_Check(func)) { 3926 return " instance"; 3927 } else { 3928 return " object"; 3929 } 3659 3930 } 3660 3931 … … 3662 3933 err_args(PyObject *func, int flags, int nargs) 3663 3934 { 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3935 if (flags & METH_NOARGS) 3936 PyErr_Format(PyExc_TypeError, 3937 "%.200s() takes no arguments (%d given)", 3938 ((PyCFunctionObject *)func)->m_ml->ml_name, 3939 nargs); 3940 else 3941 PyErr_Format(PyExc_TypeError, 3942 "%.200s() takes exactly one argument (%d given)", 3943 ((PyCFunctionObject *)func)->m_ml->ml_name, 3944 nargs); 3674 3945 } 3675 3946 3676 3947 #define C_TRACE(x, call) \ 3677 3948 if (tstate->use_tracing && tstate->c_profilefunc) { \ 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3949 if (call_trace(tstate->c_profilefunc, \ 3950 tstate->c_profileobj, \ 3951 tstate->frame, PyTrace_C_CALL, \ 3952 func)) { \ 3953 x = NULL; \ 3954 } \ 3955 else { \ 3956 x = call; \ 3957 if (tstate->c_profilefunc != NULL) { \ 3958 if (x == NULL) { \ 3959 call_trace_protected(tstate->c_profilefunc, \ 3960 tstate->c_profileobj, \ 3961 tstate->frame, PyTrace_C_EXCEPTION, \ 3962 func); \ 3963 /* XXX should pass (type, value, tb) */ \ 3964 } else { \ 3965 if (call_trace(tstate->c_profilefunc, \ 3966 tstate->c_profileobj, \ 3967 tstate->frame, PyTrace_C_RETURN, \ 3968 func)) { \ 3969 Py_DECREF(x); \ 3970 x = NULL; \ 3971 } \ 3972 } \ 3973 } \ 3974 } \ 3704 3975 } else { \ 3705 3706 3976 x = call; \ 3977 } 3707 3978 3708 3979 static PyObject * 3709 3980 call_function(PyObject ***pp_stack, int oparg 3710 3981 #ifdef WITH_TSC 3711 3982 , uint64* pintr0, uint64* pintr1 3712 3983 #endif 3713 3714 { 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3984 ) 3985 { 3986 int na = oparg & 0xff; 3987 int nk = (oparg>>8) & 0xff; 3988 int n = na + 2 * nk; 3989 PyObject **pfunc = (*pp_stack) - n - 1; 3990 PyObject *func = *pfunc; 3991 PyObject *x, *w; 3992 3993 /* Always dispatch PyCFunction first, because these are 3994 presumed to be the most frequent callable object. 3995 */ 3996 if (PyCFunction_Check(func) && nk == 0) { 3997 int flags = PyCFunction_GET_FLAGS(func); 3998 PyThreadState *tstate = PyThreadState_GET(); 3999 4000 PCALL(PCALL_CFUNCTION); 4001 if (flags & (METH_NOARGS | METH_O)) { 4002 PyCFunction meth = PyCFunction_GET_FUNCTION(func); 4003 PyObject *self = PyCFunction_GET_SELF(func); 4004 if (flags & METH_NOARGS && na == 0) { 4005 C_TRACE(x, (*meth)(self,NULL)); 4006 } 4007 else if (flags & METH_O && na == 1) { 4008 PyObject *arg = EXT_POP(*pp_stack); 4009 C_TRACE(x, (*meth)(self,arg)); 4010 Py_DECREF(arg); 4011 } 4012 else { 4013 err_args(func, flags, na); 4014 x = NULL; 4015 } 4016 } 4017 else { 4018 PyObject *callargs; 4019 callargs = load_args(pp_stack, na); 4020 READ_TIMESTAMP(*pintr0); 4021 C_TRACE(x, PyCFunction_Call(func,callargs,NULL)); 4022 READ_TIMESTAMP(*pintr1); 4023 Py_XDECREF(callargs); 4024 } 4025 } else { 4026 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) { 4027 /* optimize access to bound methods */ 4028 PyObject *self = PyMethod_GET_SELF(func); 4029 PCALL(PCALL_METHOD); 4030 PCALL(PCALL_BOUND_METHOD); 4031 Py_INCREF(self); 4032 func = PyMethod_GET_FUNCTION(func); 4033 Py_INCREF(func); 4034 Py_DECREF(*pfunc); 4035 *pfunc = self; 4036 na++; 4037 n++; 4038 } else 4039 Py_INCREF(func); 4040 READ_TIMESTAMP(*pintr0); 4041 if (PyFunction_Check(func)) 4042 x = fast_function(func, pp_stack, n, na, nk); 4043 else 4044 x = do_call(func, pp_stack, na, nk); 4045 READ_TIMESTAMP(*pintr1); 4046 Py_DECREF(func); 4047 } 4048 4049 /* Clear the stack of the function object. Also removes 4050 the arguments in case they weren't consumed already 4051 (fast_function() and err_args() leave them on the stack). 4052 */ 4053 while ((*pp_stack) > pfunc) { 4054 w = EXT_POP(*pp_stack); 4055 Py_DECREF(w); 4056 PCALL(PCALL_POP); 4057 } 4058 return x; 3788 4059 } 3789 4060 … … 3800 4071 fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk) 3801 4072 { 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 4073 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); 4074 PyObject *globals = PyFunction_GET_GLOBALS(func); 4075 PyObject *argdefs = PyFunction_GET_DEFAULTS(func); 4076 PyObject **d = NULL; 4077 int nd = 0; 4078 4079 PCALL(PCALL_FUNCTION); 4080 PCALL(PCALL_FAST_FUNCTION); 4081 if (argdefs == NULL && co->co_argcount == n && nk==0 && 4082 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) { 4083 PyFrameObject *f; 4084 PyObject *retval = NULL; 4085 PyThreadState *tstate = PyThreadState_GET(); 4086 PyObject **fastlocals, **stack; 4087 int i; 4088 4089 PCALL(PCALL_FASTER_FUNCTION); 4090 assert(globals != NULL); 4091 /* XXX Perhaps we should create a specialized 4092 PyFrame_New() that doesn't take locals, but does 4093 take builtins without sanity checking them. 4094 */ 4095 assert(tstate != NULL); 4096 f = PyFrame_New(tstate, co, globals, NULL); 4097 if (f == NULL) 4098 return NULL; 4099 4100 fastlocals = f->f_localsplus; 4101 stack = (*pp_stack) - n; 4102 4103 for (i = 0; i < n; i++) { 4104 Py_INCREF(*stack); 4105 fastlocals[i] = *stack++; 4106 } 4107 retval = PyEval_EvalFrameEx(f,0); 4108 ++tstate->recursion_depth; 4109 Py_DECREF(f); 4110 --tstate->recursion_depth; 4111 return retval; 4112 } 4113 if (argdefs != NULL) { 4114 d = &PyTuple_GET_ITEM(argdefs, 0); 4115 nd = Py_SIZE(argdefs); 4116 } 4117 return PyEval_EvalCodeEx(co, globals, 4118 (PyObject *)NULL, (*pp_stack)-n, na, 4119 (*pp_stack)-2*nk, nk, d, nd, 4120 PyFunction_GET_CLOSURE(func)); 3850 4121 } 3851 4122 … … 3854 4125 PyObject *func) 3855 4126 { 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 4127 PyObject *kwdict = NULL; 4128 if (orig_kwdict == NULL) 4129 kwdict = PyDict_New(); 4130 else { 4131 kwdict = PyDict_Copy(orig_kwdict); 4132 Py_DECREF(orig_kwdict); 4133 } 4134 if (kwdict == NULL) 4135 return NULL; 4136 while (--nk >= 0) { 4137 int err; 4138 PyObject *value = EXT_POP(*pp_stack); 4139 PyObject *key = EXT_POP(*pp_stack); 4140 if (PyDict_GetItem(kwdict, key) != NULL) { 4141 PyErr_Format(PyExc_TypeError, 4142 "%.200s%s got multiple values " 4143 "for keyword argument '%.200s'", 4144 PyEval_GetFuncName(func), 4145 PyEval_GetFuncDesc(func), 4146 PyString_AsString(key)); 4147 Py_DECREF(key); 4148 Py_DECREF(value); 4149 Py_DECREF(kwdict); 4150 return NULL; 4151 } 4152 err = PyDict_SetItem(kwdict, key, value); 4153 Py_DECREF(key); 4154 Py_DECREF(value); 4155 if (err) { 4156 Py_DECREF(kwdict); 4157 return NULL; 4158 } 4159 } 4160 return kwdict; 3890 4161 } 3891 4162 3892 4163 static PyObject * 3893 4164 update_star_args(int nstack, int nstar, PyObject *stararg, 3894 3895 { 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 4165 PyObject ***pp_stack) 4166 { 4167 PyObject *callargs, *w; 4168 4169 callargs = PyTuple_New(nstack + nstar); 4170 if (callargs == NULL) { 4171 return NULL; 4172 } 4173 if (nstar) { 4174 int i; 4175 for (i = 0; i < nstar; i++) { 4176 PyObject *a = PyTuple_GET_ITEM(stararg, i); 4177 Py_INCREF(a); 4178 PyTuple_SET_ITEM(callargs, nstack + i, a); 4179 } 4180 } 4181 while (--nstack >= 0) { 4182 w = EXT_POP(*pp_stack); 4183 PyTuple_SET_ITEM(callargs, nstack, w); 4184 } 4185 return callargs; 3915 4186 } 3916 4187 … … 3918 4189 load_args(PyObject ***pp_stack, int na) 3919 4190 { 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 4191 PyObject *args = PyTuple_New(na); 4192 PyObject *w; 4193 4194 if (args == NULL) 4195 return NULL; 4196 while (--na >= 0) { 4197 w = EXT_POP(*pp_stack); 4198 PyTuple_SET_ITEM(args, na, w); 4199 } 4200 return args; 3930 4201 } 3931 4202 … … 3933 4204 do_call(PyObject *func, PyObject ***pp_stack, int na, int nk) 3934 4205 { 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 4206 PyObject *callargs = NULL; 4207 PyObject *kwdict = NULL; 4208 PyObject *result = NULL; 4209 4210 if (nk > 0) { 4211 kwdict = update_keyword_args(NULL, nk, pp_stack, func); 4212 if (kwdict == NULL) 4213 goto call_fail; 4214 } 4215 callargs = load_args(pp_stack, na); 4216 if (callargs == NULL) 4217 goto call_fail; 3947 4218 #ifdef CALL_PROFILE 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 4219 /* At this point, we have to look at the type of func to 4220 update the call stats properly. Do it here so as to avoid 4221 exposing the call stats machinery outside ceval.c 4222 */ 4223 if (PyFunction_Check(func)) 4224 PCALL(PCALL_FUNCTION); 4225 else if (PyMethod_Check(func)) 4226 PCALL(PCALL_METHOD); 4227 else if (PyType_Check(func)) 4228 PCALL(PCALL_TYPE); 4229 else if (PyCFunction_Check(func)) 4230 PCALL(PCALL_CFUNCTION); 4231 else 4232 PCALL(PCALL_OTHER); 3962 4233 #endif 3963 3964 3965 3966 3967 3968 4234 if (PyCFunction_Check(func)) { 4235 PyThreadState *tstate = PyThreadState_GET(); 4236 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); 4237 } 4238 else 4239 result = PyObject_Call(func, callargs, kwdict); 3969 4240 call_fail: 3970 3971 3972 4241 Py_XDECREF(callargs); 4242 Py_XDECREF(kwdict); 4243 return result; 3973 4244 } 3974 4245 … … 3976 4247 ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk) 3977 4248 { 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4249 int nstar = 0; 4250 PyObject *callargs = NULL; 4251 PyObject *stararg = NULL; 4252 PyObject *kwdict = NULL; 4253 PyObject *result = NULL; 4254 4255 if (flags & CALL_FLAG_KW) { 4256 kwdict = EXT_POP(*pp_stack); 4257 if (!PyDict_Check(kwdict)) { 4258 PyObject *d; 4259 d = PyDict_New(); 4260 if (d == NULL) 4261 goto ext_call_fail; 4262 if (PyDict_Update(d, kwdict) != 0) { 4263 Py_DECREF(d); 4264 /* PyDict_Update raises attribute 4265 * error (percolated from an attempt 4266 * to get 'keys' attribute) instead of 4267 * a type error if its second argument 4268 * is not a mapping. 4269 */ 4270 if (PyErr_ExceptionMatches(PyExc_AttributeError)) { 4271 PyErr_Format(PyExc_TypeError, 4272 "%.200s%.200s argument after ** " 4273 "must be a mapping, not %.200s", 4274 PyEval_GetFuncName(func), 4275 PyEval_GetFuncDesc(func), 4276 kwdict->ob_type->tp_name); 4277 } 4278 goto ext_call_fail; 4279 } 4280 Py_DECREF(kwdict); 4281 kwdict = d; 4282 } 4283 } 4284 if (flags & CALL_FLAG_VAR) { 4285 stararg = EXT_POP(*pp_stack); 4286 if (!PyTuple_Check(stararg)) { 4287 PyObject *t = NULL; 4288 t = PySequence_Tuple(stararg); 4289 if (t == NULL) { 4290 if (PyErr_ExceptionMatches(PyExc_TypeError)) { 4291 PyErr_Format(PyExc_TypeError, 4292 "%.200s%.200s argument after * " 4293 "must be a sequence, not %200s", 4294 PyEval_GetFuncName(func), 4295 PyEval_GetFuncDesc(func), 4296 stararg->ob_type->tp_name); 4297 } 4298 goto ext_call_fail; 4299 } 4300 Py_DECREF(stararg); 4301 stararg = t; 4302 } 4303 nstar = PyTuple_GET_SIZE(stararg); 4304 } 4305 if (nk > 0) { 4306 kwdict = update_keyword_args(kwdict, nk, pp_stack, func); 4307 if (kwdict == NULL) 4308 goto ext_call_fail; 4309 } 4310 callargs = update_star_args(na, nstar, stararg, pp_stack); 4311 if (callargs == NULL) 4312 goto ext_call_fail; 4042 4313 #ifdef CALL_PROFILE 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4314 /* At this point, we have to look at the type of func to 4315 update the call stats properly. Do it here so as to avoid 4316 exposing the call stats machinery outside ceval.c 4317 */ 4318 if (PyFunction_Check(func)) 4319 PCALL(PCALL_FUNCTION); 4320 else if (PyMethod_Check(func)) 4321 PCALL(PCALL_METHOD); 4322 else if (PyType_Check(func)) 4323 PCALL(PCALL_TYPE); 4324 else if (PyCFunction_Check(func)) 4325 PCALL(PCALL_CFUNCTION); 4326 else 4327 PCALL(PCALL_OTHER); 4057 4328 #endif 4058 4059 4060 4061 4062 4063 4329 if (PyCFunction_Check(func)) { 4330 PyThreadState *tstate = PyThreadState_GET(); 4331 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); 4332 } 4333 else 4334 result = PyObject_Call(func, callargs, kwdict); 4064 4335 ext_call_fail: 4065 4066 4067 4068 4336 Py_XDECREF(callargs); 4337 Py_XDECREF(kwdict); 4338 Py_XDECREF(stararg); 4339 return result; 4069 4340 } 4070 4341 … … 4082 4353 _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) 4083 4354 { 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4355 if (v != NULL) { 4356 Py_ssize_t x; 4357 if (PyInt_Check(v)) { 4358 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct, 4359 however, it looks like it should be AsSsize_t. 4360 There should be a comment here explaining why. 4361 */ 4362 x = PyInt_AS_LONG(v); 4363 } 4364 else if (PyIndex_Check(v)) { 4365 x = PyNumber_AsSsize_t(v, NULL); 4366 if (x == -1 && PyErr_Occurred()) 4367 return 0; 4368 } 4369 else { 4370 PyErr_SetString(PyExc_TypeError, 4371 "slice indices must be integers or " 4372 "None or have an __index__ method"); 4373 return 0; 4374 } 4375 *pi = x; 4376 } 4377 return 1; 4107 4378 } 4108 4379 4109 4380 #undef ISINDEX 4110 4381 #define ISINDEX(x) ((x) == NULL || \ 4111 4382 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x)) 4112 4383 4113 4384 static PyObject * 4114 4385 apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */ 4115 4386 { 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4387 PyTypeObject *tp = u->ob_type; 4388 PySequenceMethods *sq = tp->tp_as_sequence; 4389 4390 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) { 4391 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX; 4392 if (!_PyEval_SliceIndex(v, &ilow)) 4393 return NULL; 4394 if (!_PyEval_SliceIndex(w, &ihigh)) 4395 return NULL; 4396 return PySequence_GetSlice(u, ilow, ihigh); 4397 } 4398 else { 4399 PyObject *slice = PySlice_New(v, w, NULL); 4400 if (slice != NULL) { 4401 PyObject *res = PyObject_GetItem(u, slice); 4402 Py_DECREF(slice); 4403 return res; 4404 } 4405 else 4406 return NULL; 4407 } 4137 4408 } 4138 4409 4139 4410 static int 4140 4411 assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x) 4141 4142 { 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4412 /* u[v:w] = x */ 4413 { 4414 PyTypeObject *tp = u->ob_type; 4415 PySequenceMethods *sq = tp->tp_as_sequence; 4416 4417 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) { 4418 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX; 4419 if (!_PyEval_SliceIndex(v, &ilow)) 4420 return -1; 4421 if (!_PyEval_SliceIndex(w, &ihigh)) 4422 return -1; 4423 if (x == NULL) 4424 return PySequence_DelSlice(u, ilow, ihigh); 4425 else 4426 return PySequence_SetSlice(u, ilow, ihigh, x); 4427 } 4428 else { 4429 PyObject *slice = PySlice_New(v, w, NULL); 4430 if (slice != NULL) { 4431 int res; 4432 if (x != NULL) 4433 res = PyObject_SetItem(u, slice, x); 4434 else 4435 res = PyObject_DelItem(u, slice); 4436 Py_DECREF(slice); 4437 return res; 4438 } 4439 else 4440 return -1; 4441 } 4171 4442 } 4172 4443 … … 4176 4447 4177 4448 #define CANNOT_CATCH_MSG "catching classes that don't inherit from " \ 4178 4449 "BaseException is not allowed in 3.x" 4179 4450 4180 4451 static PyObject * 4181 4452 cmp_outcome(int op, register PyObject *v, register PyObject *w) 4182 4453 { 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4454 int res = 0; 4455 switch (op) { 4456 case PyCmp_IS: 4457 res = (v == w); 4458 break; 4459 case PyCmp_IS_NOT: 4460 res = (v != w); 4461 break; 4462 case PyCmp_IN: 4463 res = PySequence_Contains(w, v); 4464 if (res < 0) 4465 return NULL; 4466 break; 4467 case PyCmp_NOT_IN: 4468 res = PySequence_Contains(w, v); 4469 if (res < 0) 4470 return NULL; 4471 res = !res; 4472 break; 4473 case PyCmp_EXC_MATCH: 4474 if (PyTuple_Check(w)) { 4475 Py_ssize_t i, length; 4476 length = PyTuple_Size(w); 4477 for (i = 0; i < length; i += 1) { 4478 PyObject *exc = PyTuple_GET_ITEM(w, i); 4479 if (PyString_Check(exc)) { 4480 int ret_val; 4481 ret_val = PyErr_WarnEx( 4482 PyExc_DeprecationWarning, 4483 "catching of string " 4484 "exceptions is deprecated", 1); 4485 if (ret_val < 0) 4486 return NULL; 4487 } 4488 else if (Py_Py3kWarningFlag && 4489 !PyTuple_Check(exc) && 4490 !Py3kExceptionClass_Check(exc)) 4491 { 4492 int ret_val; 4493 ret_val = PyErr_WarnEx( 4494 PyExc_DeprecationWarning, 4495 CANNOT_CATCH_MSG, 1); 4496 if (ret_val < 0) 4497 return NULL; 4498 } 4499 } 4500 } 4501 else { 4502 if (PyString_Check(w)) { 4503 int ret_val; 4504 ret_val = PyErr_WarnEx( 4505 PyExc_DeprecationWarning, 4506 "catching of string " 4507 "exceptions is deprecated", 1); 4508 if (ret_val < 0) 4509 return NULL; 4510 } 4511 else if (Py_Py3kWarningFlag && 4512 !PyTuple_Check(w) && 4513 !Py3kExceptionClass_Check(w)) 4514 { 4515 int ret_val; 4516 ret_val = PyErr_WarnEx( 4517 PyExc_DeprecationWarning, 4518 CANNOT_CATCH_MSG, 1); 4519 if (ret_val < 0) 4520 return NULL; 4521 } 4522 } 4523 res = PyErr_GivenExceptionMatches(v, w); 4524 break; 4525 default: 4526 return PyObject_RichCompare(v, w, op); 4527 } 4528 v = res ? Py_True : Py_False; 4529 Py_INCREF(v); 4530 return v; 4260 4531 } 4261 4532 … … 4263 4534 import_from(PyObject *v, PyObject *name) 4264 4535 { 4265 4266 4267 4268 4269 4270 4271 4272 4273 4536 PyObject *x; 4537 4538 x = PyObject_GetAttr(v, name); 4539 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { 4540 PyErr_Format(PyExc_ImportError, 4541 "cannot import name %.230s", 4542 PyString_AsString(name)); 4543 } 4544 return x; 4274 4545 } 4275 4546 … … 4277 4548 import_all_from(PyObject *locals, PyObject *v) 4278 4549 { 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4550 PyObject *all = PyObject_GetAttrString(v, "__all__"); 4551 PyObject *dict, *name, *value; 4552 int skip_leading_underscores = 0; 4553 int pos, err; 4554 4555 if (all == NULL) { 4556 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) 4557 return -1; /* Unexpected error */ 4558 PyErr_Clear(); 4559 dict = PyObject_GetAttrString(v, "__dict__"); 4560 if (dict == NULL) { 4561 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) 4562 return -1; 4563 PyErr_SetString(PyExc_ImportError, 4564 "from-import-* object has no __dict__ and no __all__"); 4565 return -1; 4566 } 4567 all = PyMapping_Keys(dict); 4568 Py_DECREF(dict); 4569 if (all == NULL) 4570 return -1; 4571 skip_leading_underscores = 1; 4572 } 4573 4574 for (pos = 0, err = 0; ; pos++) { 4575 name = PySequence_GetItem(all, pos); 4576 if (name == NULL) { 4577 if (!PyErr_ExceptionMatches(PyExc_IndexError)) 4578 err = -1; 4579 else 4580 PyErr_Clear(); 4581 break; 4582 } 4583 if (skip_leading_underscores && 4584 PyString_Check(name) && 4585 PyString_AS_STRING(name)[0] == '_') 4586 { 4587 Py_DECREF(name); 4588 continue; 4589 } 4590 value = PyObject_GetAttr(v, name); 4591 if (value == NULL) 4592 err = -1; 4593 else if (PyDict_CheckExact(locals)) 4594 err = PyDict_SetItem(locals, name, value); 4595 else 4596 err = PyObject_SetItem(locals, name, value); 4597 Py_DECREF(name); 4598 Py_XDECREF(value); 4599 if (err != 0) 4600 break; 4601 } 4602 Py_DECREF(all); 4603 return err; 4333 4604 } 4334 4605 … … 4336 4607 build_class(PyObject *methods, PyObject *bases, PyObject *name) 4337 4608 { 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4609 PyObject *metaclass = NULL, *result, *base; 4610 4611 if (PyDict_Check(methods)) 4612 metaclass = PyDict_GetItemString(methods, "__metaclass__"); 4613 if (metaclass != NULL) 4614 Py_INCREF(metaclass); 4615 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) { 4616 base = PyTuple_GET_ITEM(bases, 0); 4617 metaclass = PyObject_GetAttrString(base, "__class__"); 4618 if (metaclass == NULL) { 4619 PyErr_Clear(); 4620 metaclass = (PyObject *)base->ob_type; 4621 Py_INCREF(metaclass); 4622 } 4623 } 4624 else { 4625 PyObject *g = PyEval_GetGlobals(); 4626 if (g != NULL && PyDict_Check(g)) 4627 metaclass = PyDict_GetItemString(g, "__metaclass__"); 4628 if (metaclass == NULL) 4629 metaclass = (PyObject *) &PyClass_Type; 4630 Py_INCREF(metaclass); 4631 } 4632 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods, 4633 NULL); 4634 Py_DECREF(metaclass); 4635 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) { 4636 /* A type error here likely means that the user passed 4637 in a base that was not a class (such the random module 4638 instead of the random.random type). Help them out with 4639 by augmenting the error message with more information.*/ 4640 4641 PyObject *ptype, *pvalue, *ptraceback; 4642 4643 PyErr_Fetch(&ptype, &pvalue, &ptraceback); 4644 if (PyString_Check(pvalue)) { 4645 PyObject *newmsg; 4646 newmsg = PyString_FromFormat( 4647 "Error when calling the metaclass bases\n" 4648 " %s", 4649 PyString_AS_STRING(pvalue)); 4650 if (newmsg != NULL) { 4651 Py_DECREF(pvalue); 4652 pvalue = newmsg; 4653 } 4654 } 4655 PyErr_Restore(ptype, pvalue, ptraceback); 4656 } 4657 return result; 4387 4658 } 4388 4659 4389 4660 static int 4390 4661 exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals, 4391 PyObject *locals) 4392 { 4393 int n; 4394 PyObject *v; 4395 int plain = 0; 4396 4397 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None && 4398 ((n = PyTuple_Size(prog)) == 2 || n == 3)) { 4399 /* Backward compatibility hack */ 4400 globals = PyTuple_GetItem(prog, 1); 4401 if (n == 3) 4402 locals = PyTuple_GetItem(prog, 2); 4403 prog = PyTuple_GetItem(prog, 0); 4404 } 4405 if (globals == Py_None) { 4406 globals = PyEval_GetGlobals(); 4407 if (locals == Py_None) { 4408 locals = PyEval_GetLocals(); 4409 plain = 1; 4410 } 4411 if (!globals || !locals) { 4412 PyErr_SetString(PyExc_SystemError, 4413 "globals and locals cannot be NULL"); 4414 return -1; 4415 } 4416 } 4417 else if (locals == Py_None) 4418 locals = globals; 4419 if (!PyString_Check(prog) && 4420 !PyUnicode_Check(prog) && 4421 !PyCode_Check(prog) && 4422 !PyFile_Check(prog)) { 4423 PyErr_SetString(PyExc_TypeError, 4424 "exec: arg 1 must be a string, file, or code object"); 4425 return -1; 4426 } 4427 if (!PyDict_Check(globals)) { 4428 PyErr_SetString(PyExc_TypeError, 4429 "exec: arg 2 must be a dictionary or None"); 4430 return -1; 4431 } 4432 if (!PyMapping_Check(locals)) { 4433 PyErr_SetString(PyExc_TypeError, 4434 "exec: arg 3 must be a mapping or None"); 4435 return -1; 4436 } 4437 if (PyDict_GetItemString(globals, "__builtins__") == NULL) 4438 PyDict_SetItemString(globals, "__builtins__", f->f_builtins); 4439 if (PyCode_Check(prog)) { 4440 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) { 4441 PyErr_SetString(PyExc_TypeError, 4442 "code object passed to exec may not contain free variables"); 4443 return -1; 4444 } 4445 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals); 4446 } 4447 else if (PyFile_Check(prog)) { 4448 FILE *fp = PyFile_AsFile(prog); 4449 char *name = PyString_AsString(PyFile_Name(prog)); 4450 PyCompilerFlags cf; 4451 if (name == NULL) 4452 return -1; 4453 cf.cf_flags = 0; 4454 if (PyEval_MergeCompilerFlags(&cf)) 4455 v = PyRun_FileFlags(fp, name, Py_file_input, globals, 4456 locals, &cf); 4457 else 4458 v = PyRun_File(fp, name, Py_file_input, globals, 4459 locals); 4460 } 4461 else { 4462 PyObject *tmp = NULL; 4463 char *str; 4464 PyCompilerFlags cf; 4465 cf.cf_flags = 0; 4662 PyObject *locals) 4663 { 4664 int n; 4665 PyObject *v; 4666 int plain = 0; 4667 4668 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None && 4669 ((n = PyTuple_Size(prog)) == 2 || n == 3)) { 4670 /* Backward compatibility hack */ 4671 globals = PyTuple_GetItem(prog, 1); 4672 if (n == 3) 4673 locals = PyTuple_GetItem(prog, 2); 4674 prog = PyTuple_GetItem(prog, 0); 4675 } 4676 if (globals == Py_None) { 4677 globals = PyEval_GetGlobals(); 4678 if (locals == Py_None) { 4679 locals = PyEval_GetLocals(); 4680 plain = 1; 4681 } 4682 if (!globals || !locals) { 4683 PyErr_SetString(PyExc_SystemError, 4684 "globals and locals cannot be NULL"); 4685 return -1; 4686 } 4687 } 4688 else if (locals == Py_None) 4689 locals = globals; 4690 if (!PyString_Check(prog) && 4466 4691 #ifdef Py_USING_UNICODE 4467 if (PyUnicode_Check(prog)) { 4468 tmp = PyUnicode_AsUTF8String(prog); 4469 if (tmp == NULL) 4470 return -1; 4471 prog = tmp; 4472 cf.cf_flags |= PyCF_SOURCE_IS_UTF8; 4473 } 4692 !PyUnicode_Check(prog) && 4474 4693 #endif 4475 if (PyString_AsStringAndSize(prog, &str, NULL)) 4476 return -1; 4477 if (PyEval_MergeCompilerFlags(&cf)) 4478 v = PyRun_StringFlags(str, Py_file_input, globals, 4479 locals, &cf); 4480 else 4481 v = PyRun_String(str, Py_file_input, globals, locals); 4482 Py_XDECREF(tmp); 4483 } 4484 if (plain) 4485 PyFrame_LocalsToFast(f, 0); 4486 if (v == NULL) 4487 return -1; 4488 Py_DECREF(v); 4489 return 0; 4694 !PyCode_Check(prog) && 4695 !PyFile_Check(prog)) { 4696 PyErr_SetString(PyExc_TypeError, 4697 "exec: arg 1 must be a string, file, or code object"); 4698 return -1; 4699 } 4700 if (!PyDict_Check(globals)) { 4701 PyErr_SetString(PyExc_TypeError, 4702 "exec: arg 2 must be a dictionary or None"); 4703 return -1; 4704 } 4705 if (!PyMapping_Check(locals)) { 4706 PyErr_SetString(PyExc_TypeError, 4707 "exec: arg 3 must be a mapping or None"); 4708 return -1; 4709 } 4710 if (PyDict_GetItemString(globals, "__builtins__") == NULL) 4711 PyDict_SetItemString(globals, "__builtins__", f->f_builtins); 4712 if (PyCode_Check(prog)) { 4713 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) { 4714 PyErr_SetString(PyExc_TypeError, 4715 "code object passed to exec may not contain free variables"); 4716 return -1; 4717 } 4718 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals); 4719 } 4720 else if (PyFile_Check(prog)) { 4721 FILE *fp = PyFile_AsFile(prog); 4722 char *name = PyString_AsString(PyFile_Name(prog)); 4723 PyCompilerFlags cf; 4724 if (name == NULL) 4725 return -1; 4726 cf.cf_flags = 0; 4727 if (PyEval_MergeCompilerFlags(&cf)) 4728 v = PyRun_FileFlags(fp, name, Py_file_input, globals, 4729 locals, &cf); 4730 else 4731 v = PyRun_File(fp, name, Py_file_input, globals, 4732 locals); 4733 } 4734 else { 4735 PyObject *tmp = NULL; 4736 char *str; 4737 PyCompilerFlags cf; 4738 cf.cf_flags = 0; 4739 #ifdef Py_USING_UNICODE 4740 if (PyUnicode_Check(prog)) { 4741 tmp = PyUnicode_AsUTF8String(prog); 4742 if (tmp == NULL) 4743 return -1; 4744 prog = tmp; 4745 cf.cf_flags |= PyCF_SOURCE_IS_UTF8; 4746 } 4747 #endif 4748 if (PyString_AsStringAndSize(prog, &str, NULL)) 4749 return -1; 4750 if (PyEval_MergeCompilerFlags(&cf)) 4751 v = PyRun_StringFlags(str, Py_file_input, globals, 4752 locals, &cf); 4753 else 4754 v = PyRun_String(str, Py_file_input, globals, locals); 4755 Py_XDECREF(tmp); 4756 } 4757 if (plain) 4758 PyFrame_LocalsToFast(f, 0); 4759 if (v == NULL) 4760 return -1; 4761 Py_DECREF(v); 4762 return 0; 4490 4763 } 4491 4764 … … 4493 4766 format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj) 4494 4767 { 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4768 char *obj_str; 4769 4770 if (!obj) 4771 return; 4772 4773 obj_str = PyString_AsString(obj); 4774 if (!obj_str) 4775 return; 4776 4777 PyErr_Format(exc, format_str, obj_str); 4505 4778 } 4506 4779 4507 4780 static PyObject * 4508 4781 string_concatenate(PyObject *v, PyObject *w, 4509 4510 { 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4782 PyFrameObject *f, unsigned char *next_instr) 4783 { 4784 /* This function implements 'variable += expr' when both arguments 4785 are strings. */ 4786 Py_ssize_t v_len = PyString_GET_SIZE(v); 4787 Py_ssize_t w_len = PyString_GET_SIZE(w); 4788 Py_ssize_t new_len = v_len + w_len; 4789 if (new_len < 0) { 4790 PyErr_SetString(PyExc_OverflowError, 4791 "strings are too large to concat"); 4792 return NULL; 4793 } 4794 4795 if (v->ob_refcnt == 2) { 4796 /* In the common case, there are 2 references to the value 4797 * stored in 'variable' when the += is performed: one on the 4798 * value stack (in 'v') and one still stored in the 4799 * 'variable'. We try to delete the variable now to reduce 4800 * the refcnt to 1. 4801 */ 4802 switch (*next_instr) { 4803 case STORE_FAST: 4804 { 4805 int oparg = PEEKARG(); 4806 PyObject **fastlocals = f->f_localsplus; 4807 if (GETLOCAL(oparg) == v) 4808 SETLOCAL(oparg, NULL); 4809 break; 4810 } 4811 case STORE_DEREF: 4812 { 4813 PyObject **freevars = (f->f_localsplus + 4814 f->f_code->co_nlocals); 4815 PyObject *c = freevars[PEEKARG()]; 4816 if (PyCell_GET(c) == v) 4817 PyCell_Set(c, NULL); 4818 break; 4819 } 4820 case STORE_NAME: 4821 { 4822 PyObject *names = f->f_code->co_names; 4823 PyObject *name = GETITEM(names, PEEKARG()); 4824 PyObject *locals = f->f_locals; 4825 if (PyDict_CheckExact(locals) && 4826 PyDict_GetItem(locals, name) == v) { 4827 if (PyDict_DelItem(locals, name) != 0) { 4828 PyErr_Clear(); 4829 } 4830 } 4831 break; 4832 } 4833 } 4834 } 4835 4836 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) { 4837 /* Now we own the last reference to 'v', so we can resize it 4838 * in-place. 4839 */ 4840 if (_PyString_Resize(&v, new_len) != 0) { 4841 /* XXX if _PyString_Resize() fails, 'v' has been 4842 * deallocated so it cannot be put back into 4843 * 'variable'. The MemoryError is raised when there 4844 * is no value in 'variable', which might (very 4845 * remotely) be a cause of incompatibilities. 4846 */ 4847 return NULL; 4848 } 4849 /* copy 'w' into the newly allocated area of 'v' */ 4850 memcpy(PyString_AS_STRING(v) + v_len, 4851 PyString_AS_STRING(w), w_len); 4852 return v; 4853 } 4854 else { 4855 /* When in-place resizing is not an option. */ 4856 PyString_Concat(&v, w); 4857 return v; 4858 } 4586 4859 } 4587 4860 … … 4591 4864 getarray(long a[256]) 4592 4865 { 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4866 int i; 4867 PyObject *l = PyList_New(256); 4868 if (l == NULL) return NULL; 4869 for (i = 0; i < 256; i++) { 4870 PyObject *x = PyInt_FromLong(a[i]); 4871 if (x == NULL) { 4872 Py_DECREF(l); 4873 return NULL; 4874 } 4875 PyList_SetItem(l, i, x); 4876 } 4877 for (i = 0; i < 256; i++) 4878 a[i] = 0; 4879 return l; 4607 4880 } 4608 4881 … … 4611 4884 { 4612 4885 #ifndef DXPAIRS 4613 4886 return getarray(dxp); 4614 4887 #else 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4888 int i; 4889 PyObject *l = PyList_New(257); 4890 if (l == NULL) return NULL; 4891 for (i = 0; i < 257; i++) { 4892 PyObject *x = getarray(dxpairs[i]); 4893 if (x == NULL) { 4894 Py_DECREF(l); 4895 return NULL; 4896 } 4897 PyList_SetItem(l, i, x); 4898 } 4899 return l; 4627 4900 #endif 4628 4901 }
Note:
See TracChangeset
for help on using the changeset viewer.