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

Delta Between Two Patch Sets: Python/ceval.c

Issue 11682: PEP 380 reference implementation for 3.3
Left Patch Set: Created 7 years, 10 months ago
Right Patch Set: Created 7 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« Objects/exceptions.c ('K') | « Python/ast.c ('k') | Python/compile.c » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 1
2 /* Execute compiled code */ 2 /* Execute compiled code */
3 3
4 /* XXX TO DO: 4 /* XXX TO DO:
5 XXX speed up searching for keywords by using a dictionary 5 XXX speed up searching for keywords by using a dictionary
6 XXX document it! 6 XXX document it!
7 */ 7 */
8 8
9 /* enable more aggressive intra-module optimizations, where available */ 9 /* enable more aggressive intra-module optimizations, where available */
10 #define PY_LOCAL_AGGRESSIVE 10 #define PY_LOCAL_AGGRESSIVE
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 } 365 }
366 366
367 /* This function is called from PyOS_AfterFork to ensure that newly 367 /* This function is called from PyOS_AfterFork to ensure that newly
368 created child processes don't hold locks referring to threads which 368 created child processes don't hold locks referring to threads which
369 are not running in the child process. (This could also be done using 369 are not running in the child process. (This could also be done using
370 pthread_atfork mechanism, at least for the pthreads implementation.) */ 370 pthread_atfork mechanism, at least for the pthreads implementation.) */
371 371
372 void 372 void
373 PyEval_ReInitThreads(void) 373 PyEval_ReInitThreads(void)
374 { 374 {
375 _Py_IDENTIFIER(_after_fork);
375 PyObject *threading, *result; 376 PyObject *threading, *result;
376 PyThreadState *tstate = PyThreadState_GET(); 377 PyThreadState *tstate = PyThreadState_GET();
377 378
378 if (!gil_created()) 379 if (!gil_created())
379 return; 380 return;
380 recreate_gil(); 381 recreate_gil();
381 pending_lock = PyThread_allocate_lock(); 382 pending_lock = PyThread_allocate_lock();
382 take_gil(tstate); 383 take_gil(tstate);
383 main_thread = PyThread_get_thread_ident(); 384 main_thread = PyThread_get_thread_ident();
384 385
385 /* Update the threading module with the new state. 386 /* Update the threading module with the new state.
386 */ 387 */
387 tstate = PyThreadState_GET(); 388 tstate = PyThreadState_GET();
388 threading = PyMapping_GetItemString(tstate->interp->modules, 389 threading = PyMapping_GetItemString(tstate->interp->modules,
389 "threading"); 390 "threading");
390 if (threading == NULL) { 391 if (threading == NULL) {
391 /* threading not imported */ 392 /* threading not imported */
392 PyErr_Clear(); 393 PyErr_Clear();
393 return; 394 return;
394 } 395 }
395 result = PyObject_CallMethod(threading, "_after_fork", NULL); 396 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
396 if (result == NULL) 397 if (result == NULL)
397 PyErr_WriteUnraisable(threading); 398 PyErr_WriteUnraisable(threading);
398 else 399 else
399 Py_DECREF(result); 400 Py_DECREF(result);
400 Py_DECREF(threading); 401 Py_DECREF(threading);
401 } 402 }
402 403
403 #else 404 #else
404 static _Py_atomic_int eval_breaker = {0}; 405 static _Py_atomic_int eval_breaker = {0};
405 static int pending_async_exc = 0; 406 static int pending_async_exc = 0;
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 callback. 485 callback.
485 */ 486 */
486 487
487 #define NPENDINGCALLS 32 488 #define NPENDINGCALLS 32
488 static struct { 489 static struct {
489 int (*func)(void *); 490 int (*func)(void *);
490 void *arg; 491 void *arg;
491 } pendingcalls[NPENDINGCALLS]; 492 } pendingcalls[NPENDINGCALLS];
492 static int pendingfirst = 0; 493 static int pendingfirst = 0;
493 static int pendinglast = 0; 494 static int pendinglast = 0;
494 static char pendingbusy = 0;
495 495
496 int 496 int
497 Py_AddPendingCall(int (*func)(void *), void *arg) 497 Py_AddPendingCall(int (*func)(void *), void *arg)
498 { 498 {
499 int i, j, result=0; 499 int i, j, result=0;
500 PyThread_type_lock lock = pending_lock; 500 PyThread_type_lock lock = pending_lock;
501 501
502 /* try a few times for the lock. Since this mechanism is used 502 /* try a few times for the lock. Since this mechanism is used
503 * for signal handling (on the main thread), there is a (slim) 503 * for signal handling (on the main thread), there is a (slim)
504 * chance that a signal is delivered on the same thread while we 504 * chance that a signal is delivered on the same thread while we
(...skipping 26 matching lines...) Expand all
531 /* signal main loop */ 531 /* signal main loop */
532 SIGNAL_PENDING_CALLS(); 532 SIGNAL_PENDING_CALLS();
533 if (lock != NULL) 533 if (lock != NULL)
534 PyThread_release_lock(lock); 534 PyThread_release_lock(lock);
535 return result; 535 return result;
536 } 536 }
537 537
538 int 538 int
539 Py_MakePendingCalls(void) 539 Py_MakePendingCalls(void)
540 { 540 {
541 static int busy = 0;
541 int i; 542 int i;
542 int r = 0; 543 int r = 0;
543 544
544 if (!pending_lock) { 545 if (!pending_lock) {
545 /* initial allocation of the lock */ 546 /* initial allocation of the lock */
546 pending_lock = PyThread_allocate_lock(); 547 pending_lock = PyThread_allocate_lock();
547 if (pending_lock == NULL) 548 if (pending_lock == NULL)
548 return -1; 549 return -1;
549 } 550 }
550 551
551 /* only service pending calls on main thread */ 552 /* only service pending calls on main thread */
552 if (main_thread && PyThread_get_thread_ident() != main_thread) 553 if (main_thread && PyThread_get_thread_ident() != main_thread)
553 return 0; 554 return 0;
554 /* don't perform recursive pending calls */ 555 /* don't perform recursive pending calls */
555 if (pendingbusy) 556 if (busy)
556 return 0; 557 return 0;
557 pendingbusy = 1; 558 busy = 1;
558 /* perform a bounded number of calls, in case of recursion */ 559 /* perform a bounded number of calls, in case of recursion */
559 for (i=0; i<NPENDINGCALLS; i++) { 560 for (i=0; i<NPENDINGCALLS; i++) {
560 int j; 561 int j;
561 int (*func)(void *); 562 int (*func)(void *);
562 void *arg = NULL; 563 void *arg = NULL;
563 564
564 /* pop one item off the queue while holding the lock */ 565 /* pop one item off the queue while holding the lock */
565 PyThread_acquire_lock(pending_lock, WAIT_LOCK); 566 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
566 j = pendingfirst; 567 j = pendingfirst;
567 if (j == pendinglast) { 568 if (j == pendinglast) {
568 func = NULL; /* Queue empty */ 569 func = NULL; /* Queue empty */
569 } else { 570 } else {
570 func = pendingcalls[j].func; 571 func = pendingcalls[j].func;
571 arg = pendingcalls[j].arg; 572 arg = pendingcalls[j].arg;
572 pendingfirst = (j + 1) % NPENDINGCALLS; 573 pendingfirst = (j + 1) % NPENDINGCALLS;
573 } 574 }
574 if (pendingfirst != pendinglast) 575 if (pendingfirst != pendinglast)
575 SIGNAL_PENDING_CALLS(); 576 SIGNAL_PENDING_CALLS();
576 else 577 else
577 UNSIGNAL_PENDING_CALLS(); 578 UNSIGNAL_PENDING_CALLS();
578 PyThread_release_lock(pending_lock); 579 PyThread_release_lock(pending_lock);
579 /* having released the lock, perform the callback */ 580 /* having released the lock, perform the callback */
580 if (func == NULL) 581 if (func == NULL)
581 break; 582 break;
582 r = func(arg); 583 r = func(arg);
583 if (r) 584 if (r)
584 break; 585 break;
585 } 586 }
586 pendingbusy = 0; 587 busy = 0;
587 return r; 588 return r;
588 } 589 }
589 590
590 #else /* if ! defined WITH_THREAD */ 591 #else /* if ! defined WITH_THREAD */
591 592
592 /* 593 /*
593 WARNING! ASYNCHRONOUSLY EXECUTING CODE! 594 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
594 This code is used for signal handling in python that isn't built 595 This code is used for signal handling in python that isn't built
595 with WITH_THREAD. 596 with WITH_THREAD.
596 Don't use this implementation when Py_AddPendingCalls() can happen 597 Don't use this implementation when Py_AddPendingCalls() can happen
(...skipping 743 matching lines...) Expand 10 before | Expand all | Expand 10 after
1340 /* Main switch on opcode */ 1341 /* Main switch on opcode */
1341 READ_TIMESTAMP(inst0); 1342 READ_TIMESTAMP(inst0);
1342 1343
1343 switch (opcode) { 1344 switch (opcode) {
1344 1345
1345 /* BEWARE! 1346 /* BEWARE!
1346 It is essential that any operation that fails sets either 1347 It is essential that any operation that fails sets either
1347 x to NULL, err to nonzero, or why to anything but WHY_NOT, 1348 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1348 and that no operation that succeeds does this! */ 1349 and that no operation that succeeds does this! */
1349 1350
1350 /* case STOP_CODE: this is an error! */
1351
1352 TARGET(NOP) 1351 TARGET(NOP)
1353 FAST_DISPATCH(); 1352 FAST_DISPATCH();
1354 1353
1355 TARGET(LOAD_FAST) 1354 TARGET(LOAD_FAST)
1356 x = GETLOCAL(oparg); 1355 x = GETLOCAL(oparg);
1357 if (x != NULL) { 1356 if (x != NULL) {
1358 Py_INCREF(x); 1357 Py_INCREF(x);
1359 PUSH(x); 1358 PUSH(x);
1360 FAST_DISPATCH(); 1359 FAST_DISPATCH();
1361 } 1360 }
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after
1826 1825
1827 TARGET(RETURN_VALUE) 1826 TARGET(RETURN_VALUE)
1828 retval = POP(); 1827 retval = POP();
1829 why = WHY_RETURN; 1828 why = WHY_RETURN;
1830 goto fast_block_end; 1829 goto fast_block_end;
1831 1830
1832 TARGET(YIELD_FROM) 1831 TARGET(YIELD_FROM)
1833 u = POP(); 1832 u = POP();
1834 x = PyObject_GetIter(u); 1833 x = PyObject_GetIter(u);
1835 Py_DECREF(u); 1834 Py_DECREF(u);
1836 if (!x) 1835 if (x == NULL)
1837 break; 1836 break;
1838 /* x is now the iterator, make the first next() call */ 1837 /* x is now the iterator, make the first next() call */
1839 retval = (*x->ob_type->tp_iternext)(x); 1838 retval = (*Py_TYPE(x)->tp_iternext)(x);
Benjamin Peterson 2011/07/09 18:41:14 Py_TYPE
Nick Coghlan 2011/09/20 13:21:02 Done.
1840 if (!retval) { 1839 if (!retval) {
1841 /* iter may be exhausted */ 1840 /* iter may be exhausted */
1842 Py_DECREF(x); 1841 Py_CLEAR(x);
1843 if (PyErr_ExceptionMatches(PyExc_StopIteration)) { 1842 if (!PyErr_ExceptionMatches(PyExc_StopIteration)) {
1844 /* try to get return value from exception */ 1843 /* some other exception */
1845 PyObject *et, *ev, *tb; 1844 break;
1846 PyErr_Fetch(&et, &ev, &tb); 1845 }
1847 Py_XDECREF(et); Py_XDECREF(tb); 1846 /* try to get return value from exception */
Benjamin Peterson 2011/07/09 18:41:14 Two lines!
Nick Coghlan 2011/09/20 13:21:02 Done.
1848 u = NULL; 1847 PyObject *et, *ev, *tb;
1849 if (ev) { 1848 PyErr_Fetch(&et, &ev, &tb);
1850 u = PyObject_GetAttrString(ev, "value"); 1849 Py_XDECREF(et);
1851 Py_DECREF(ev); 1850 Py_XDECREF(tb);
1852 if (!u) PyErr_Clear(); 1851 /* u is return value */
Benjamin Peterson 2011/07/09 18:41:14 Kindly check for AttributeError only as well as sp
Nick Coghlan 2011/09/20 13:21:02 Done.
1852 u = NULL;
1853 if (ev) {
1854 u = PyObject_GetAttrString(ev, "value");
1855 Py_DECREF(ev);
1856 if (u == NULL) {
1857 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
1858 /* some other exception */
1859 break;
1860 }
1861 PyErr_Clear();
1853 } 1862 }
1854 if (!u) {
1855 u = Py_None;
1856 Py_INCREF(u);
1857 }
1858 /* u is return value */
1859 PUSH(u);
1860 continue;
1861 } 1863 }
1862 /* some other exception */ 1864 if (u == NULL) {
1863 x = NULL; 1865 u = Py_None;
1864 break; 1866 Py_INCREF(u);
1867 }
1868 PUSH(u);
1869 continue;
1865 } 1870 }
1866 /* x is iterator, retval is value to be yielded */ 1871 /* x is iterator, retval is value to be yielded */
1867 f->f_yieldfrom = x; 1872 f->f_yieldfrom = x;
1868 goto yield_retval; 1873 f->f_stacktop = stack_pointer;
Benjamin Peterson 2011/07/09 18:41:14 Uh, how about copying those 3 lines? At least figu
Nick Coghlan 2011/09/20 13:21:02 Done.
1874 why = WHY_YIELD;
1875 goto fast_yield;
1869 1876
1870 TARGET(YIELD_VALUE) 1877 TARGET(YIELD_VALUE)
1871 retval = POP(); 1878 retval = POP();
1872 yield_retval:
1873 f->f_stacktop = stack_pointer; 1879 f->f_stacktop = stack_pointer;
1874 why = WHY_YIELD; 1880 why = WHY_YIELD;
1875 goto fast_yield; 1881 goto fast_yield;
1876 1882
1877 TARGET(POP_EXCEPT) 1883 TARGET(POP_EXCEPT)
1878 { 1884 {
1879 PyTryBlock *b = PyFrame_BlockPop(f); 1885 PyTryBlock *b = PyFrame_BlockPop(f);
1880 if (b->b_type != EXCEPT_HANDLER) { 1886 if (b->b_type != EXCEPT_HANDLER) {
1881 PyErr_SetString(PyExc_SystemError, 1887 PyErr_SetString(PyExc_SystemError,
1882 "popped block is not an except handler"); 1888 "popped block is not an except handler");
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
2088 } 2094 }
2089 PUSH(x); 2095 PUSH(x);
2090 DISPATCH(); 2096 DISPATCH();
2091 2097
2092 TARGET(LOAD_GLOBAL) 2098 TARGET(LOAD_GLOBAL)
2093 w = GETITEM(names, oparg); 2099 w = GETITEM(names, oparg);
2094 if (PyUnicode_CheckExact(w)) { 2100 if (PyUnicode_CheckExact(w)) {
2095 /* Inline the PyDict_GetItem() calls. 2101 /* Inline the PyDict_GetItem() calls.
2096 WARNING: this is an extreme speed hack. 2102 WARNING: this is an extreme speed hack.
2097 Do not try this at home. */ 2103 Do not try this at home. */
2098 Py_hash_t hash = ((PyUnicodeObject *)w)->hash; 2104 Py_hash_t hash = ((PyASCIIObject *)w)->hash;
2099 if (hash != -1) { 2105 if (hash != -1) {
2100 PyDictObject *d; 2106 PyDictObject *d;
2101 PyDictEntry *e; 2107 PyDictEntry *e;
2102 d = (PyDictObject *)(f->f_globals); 2108 d = (PyDictObject *)(f->f_globals);
2103 e = d->ma_lookup(d, w, hash); 2109 e = d->ma_lookup(d, w, hash);
2104 if (e == NULL) { 2110 if (e == NULL) {
2105 x = NULL; 2111 x = NULL;
2106 break; 2112 break;
2107 } 2113 }
2108 x = e->me_value; 2114 x = e->me_value;
(...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after
2720 } 2726 }
2721 2727
2722 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function) 2728 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2723 TARGET(MAKE_FUNCTION) 2729 TARGET(MAKE_FUNCTION)
2724 _make_function: 2730 _make_function:
2725 { 2731 {
2726 int posdefaults = oparg & 0xff; 2732 int posdefaults = oparg & 0xff;
2727 int kwdefaults = (oparg>>8) & 0xff; 2733 int kwdefaults = (oparg>>8) & 0xff;
2728 int num_annotations = (oparg >> 16) & 0x7fff; 2734 int num_annotations = (oparg >> 16) & 0x7fff;
2729 2735
2736 w = POP(); /* qualname */
2730 v = POP(); /* code object */ 2737 v = POP(); /* code object */
2731 x = PyFunction_New(v, f->f_globals); 2738 x = PyFunction_NewWithQualName(v, f->f_globals, w);
2732 Py_DECREF(v); 2739 Py_DECREF(v);
2740 Py_DECREF(w);
2733 2741
2734 if (x != NULL && opcode == MAKE_CLOSURE) { 2742 if (x != NULL && opcode == MAKE_CLOSURE) {
2735 v = POP(); 2743 v = POP();
2736 if (PyFunction_SetClosure(x, v) != 0) { 2744 if (PyFunction_SetClosure(x, v) != 0) {
2737 /* Can't happen unless bytecode is corrupt. */ 2745 /* Can't happen unless bytecode is corrupt. */
2738 why = WHY_EXCEPTION; 2746 why = WHY_EXCEPTION;
2739 } 2747 }
2740 Py_DECREF(v); 2748 Py_DECREF(v);
2741 } 2749 }
2742 2750
(...skipping 710 matching lines...) Expand 10 before | Expand all | Expand 10 after
3453 Py_XINCREF(tstate->exc_value); 3461 Py_XINCREF(tstate->exc_value);
3454 Py_XINCREF(tstate->exc_traceback); 3462 Py_XINCREF(tstate->exc_traceback);
3455 type = f->f_exc_type; 3463 type = f->f_exc_type;
3456 value = f->f_exc_value; 3464 value = f->f_exc_value;
3457 traceback = f->f_exc_traceback; 3465 traceback = f->f_exc_traceback;
3458 f->f_exc_type = tstate->exc_type; 3466 f->f_exc_type = tstate->exc_type;
3459 f->f_exc_value = tstate->exc_value; 3467 f->f_exc_value = tstate->exc_value;
3460 f->f_exc_traceback = tstate->exc_traceback; 3468 f->f_exc_traceback = tstate->exc_traceback;
3461 Py_XDECREF(type); 3469 Py_XDECREF(type);
3462 Py_XDECREF(value); 3470 Py_XDECREF(value);
3463 Py_XDECREF(traceback); 3471 Py_XDECREF(traceback);
3464 } 3472 }
3465 3473
3466 static void 3474 static void
3467 swap_exc_state(PyThreadState *tstate, PyFrameObject *f) 3475 swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
3468 { 3476 {
3469 PyObject *tmp; 3477 PyObject *tmp;
3470 tmp = tstate->exc_type; 3478 tmp = tstate->exc_type;
3471 tstate->exc_type = f->f_exc_type; 3479 tstate->exc_type = f->f_exc_type;
3472 f->f_exc_type = tmp; 3480 f->f_exc_type = tmp;
3473 tmp = tstate->exc_value; 3481 tmp = tstate->exc_value;
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
3526 /* We support the following forms of raise: 3534 /* We support the following forms of raise:
3527 raise 3535 raise
3528 raise <instance> 3536 raise <instance>
3529 raise <type> */ 3537 raise <type> */
3530 3538
3531 if (PyExceptionClass_Check(exc)) { 3539 if (PyExceptionClass_Check(exc)) {
3532 type = exc; 3540 type = exc;
3533 value = PyObject_CallObject(exc, NULL); 3541 value = PyObject_CallObject(exc, NULL);
3534 if (value == NULL) 3542 if (value == NULL)
3535 goto raise_error; 3543 goto raise_error;
3544 if (!PyExceptionInstance_Check(value)) {
3545 PyErr_Format(PyExc_TypeError,
3546 "calling %R should have returned an instance of "
3547 "BaseException, not %R",
3548 type, Py_TYPE(value));
3549 goto raise_error;
3550 }
3536 } 3551 }
3537 else if (PyExceptionInstance_Check(exc)) { 3552 else if (PyExceptionInstance_Check(exc)) {
3538 value = exc; 3553 value = exc;
3539 type = PyExceptionInstance_Class(exc); 3554 type = PyExceptionInstance_Class(exc);
3540 Py_INCREF(type); 3555 Py_INCREF(type);
3541 } 3556 }
3542 else { 3557 else {
3543 /* Not something you can raise. You get an exception 3558 /* Not something you can raise. You get an exception
3544 anyway, just not what you specified :-) */ 3559 anyway, just not what you specified :-) */
3545 Py_DECREF(exc); 3560 Py_DECREF(exc);
(...skipping 902 matching lines...) Expand 10 before | Expand all | Expand 10 after
4448 x = PyObject_GetAttr(v, name); 4463 x = PyObject_GetAttr(v, name);
4449 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { 4464 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4450 PyErr_Format(PyExc_ImportError, "cannot import name %S", name); 4465 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4451 } 4466 }
4452 return x; 4467 return x;
4453 } 4468 }
4454 4469
4455 static int 4470 static int
4456 import_all_from(PyObject *locals, PyObject *v) 4471 import_all_from(PyObject *locals, PyObject *v)
4457 { 4472 {
4458 PyObject *all = PyObject_GetAttrString(v, "__all__"); 4473 _Py_IDENTIFIER(__all__);
4474 _Py_IDENTIFIER(__dict__);
4475 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
4459 PyObject *dict, *name, *value; 4476 PyObject *dict, *name, *value;
4460 int skip_leading_underscores = 0; 4477 int skip_leading_underscores = 0;
4461 int pos, err; 4478 int pos, err;
4462 4479
4463 if (all == NULL) { 4480 if (all == NULL) {
4464 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) 4481 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4465 return -1; /* Unexpected error */ 4482 return -1; /* Unexpected error */
4466 PyErr_Clear(); 4483 PyErr_Clear();
4467 dict = PyObject_GetAttrString(v, "__dict__"); 4484 dict = _PyObject_GetAttrId(v, &PyId___dict__);
4468 if (dict == NULL) { 4485 if (dict == NULL) {
4469 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) 4486 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4470 return -1; 4487 return -1;
4471 PyErr_SetString(PyExc_ImportError, 4488 PyErr_SetString(PyExc_ImportError,
4472 "from-import-* object has no __dict__ and no __all__"); 4489 "from-import-* object has no __dict__ and no __all__");
4473 return -1; 4490 return -1;
4474 } 4491 }
4475 all = PyMapping_Keys(dict); 4492 all = PyMapping_Keys(dict);
4476 Py_DECREF(dict); 4493 Py_DECREF(dict);
4477 if (all == NULL) 4494 if (all == NULL)
4478 return -1; 4495 return -1;
4479 skip_leading_underscores = 1; 4496 skip_leading_underscores = 1;
4480 } 4497 }
4481 4498
4482 for (pos = 0, err = 0; ; pos++) { 4499 for (pos = 0, err = 0; ; pos++) {
4483 name = PySequence_GetItem(all, pos); 4500 name = PySequence_GetItem(all, pos);
4484 if (name == NULL) { 4501 if (name == NULL) {
4485 if (!PyErr_ExceptionMatches(PyExc_IndexError)) 4502 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4486 err = -1; 4503 err = -1;
4487 else 4504 else
4488 PyErr_Clear(); 4505 PyErr_Clear();
4489 break; 4506 break;
4490 } 4507 }
4491 if (skip_leading_underscores && 4508 if (skip_leading_underscores &&
4492 PyUnicode_Check(name) && 4509 PyUnicode_Check(name) &&
4493 PyUnicode_AS_UNICODE(name)[0] == '_') 4510 PyUnicode_READY(name) != -1 &&
4511 PyUnicode_READ_CHAR(name, 0) == '_')
4494 { 4512 {
4495 Py_DECREF(name); 4513 Py_DECREF(name);
4496 continue; 4514 continue;
4497 } 4515 }
4498 value = PyObject_GetAttr(v, name); 4516 value = PyObject_GetAttr(v, name);
4499 if (value == NULL) 4517 if (value == NULL)
4500 err = -1; 4518 err = -1;
4501 else if (PyDict_CheckExact(locals)) 4519 else if (PyDict_CheckExact(locals))
4502 err = PyDict_SetItem(locals, name, value); 4520 err = PyDict_SetItem(locals, name, value);
4503 else 4521 else
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
4543 } else { 4561 } else {
4544 name = PyTuple_GET_ITEM(co->co_freevars, oparg - 4562 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4545 PyTuple_GET_SIZE(co->co_cellvars)); 4563 PyTuple_GET_SIZE(co->co_cellvars));
4546 format_exc_check_arg(PyExc_NameError, 4564 format_exc_check_arg(PyExc_NameError,
4547 UNBOUNDFREE_ERROR_MSG, name); 4565 UNBOUNDFREE_ERROR_MSG, name);
4548 } 4566 }
4549 } 4567 }
4550 4568
4551 static PyObject * 4569 static PyObject *
4552 unicode_concatenate(PyObject *v, PyObject *w, 4570 unicode_concatenate(PyObject *v, PyObject *w,
4553 PyFrameObject *f, unsigned char *next_instr) 4571 PyFrameObject *f, unsigned char *next_instr)
4554 { 4572 {
4555 /* This function implements 'variable += expr' when both arguments 4573 PyObject *res;
4556 are (Unicode) strings. */
4557 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4558 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
4559 Py_ssize_t new_len = v_len + w_len;
4560 if (new_len < 0) {
4561 PyErr_SetString(PyExc_OverflowError,
4562 "strings are too large to concat");
4563 return NULL;
4564 }
4565
4566 if (Py_REFCNT(v) == 2) { 4574 if (Py_REFCNT(v) == 2) {
4567 /* In the common case, there are 2 references to the value 4575 /* In the common case, there are 2 references to the value
4568 * stored in 'variable' when the += is performed: one on the 4576 * stored in 'variable' when the += is performed: one on the
4569 * value stack (in 'v') and one still stored in the 4577 * value stack (in 'v') and one still stored in the
4570 * 'variable'. We try to delete the variable now to reduce 4578 * 'variable'. We try to delete the variable now to reduce
4571 * the refcnt to 1. 4579 * the refcnt to 1.
4572 */ 4580 */
4573 switch (*next_instr) { 4581 switch (*next_instr) {
4574 case STORE_FAST: 4582 case STORE_FAST:
4575 { 4583 {
(...skipping 20 matching lines...) Expand all
4596 if (PyDict_CheckExact(locals) && 4604 if (PyDict_CheckExact(locals) &&
4597 PyDict_GetItem(locals, name) == v) { 4605 PyDict_GetItem(locals, name) == v) {
4598 if (PyDict_DelItem(locals, name) != 0) { 4606 if (PyDict_DelItem(locals, name) != 0) {
4599 PyErr_Clear(); 4607 PyErr_Clear();
4600 } 4608 }
4601 } 4609 }
4602 break; 4610 break;
4603 } 4611 }
4604 } 4612 }
4605 } 4613 }
4606 4614 res = v;
4607 if (Py_REFCNT(v) == 1 && !PyUnicode_CHECK_INTERNED(v)) { 4615 PyUnicode_Append(&res, w);
4608 /* Now we own the last reference to 'v', so we can resize it 4616 return res;
4609 * in-place.
4610 */
4611 if (PyUnicode_Resize(&v, new_len) != 0) {
4612 /* XXX if PyUnicode_Resize() fails, 'v' has been
4613 * deallocated so it cannot be put back into
4614 * 'variable'. The MemoryError is raised when there
4615 * is no value in 'variable', which might (very
4616 * remotely) be a cause of incompatibilities.
4617 */
4618 return NULL;
4619 }
4620 /* copy 'w' into the newly allocated area of 'v' */
4621 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4622 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
4623 return v;
4624 }
4625 else {
4626 /* When in-place resizing is not an option. */
4627 w = PyUnicode_Concat(v, w);
4628 Py_DECREF(v);
4629 return w;
4630 }
4631 } 4617 }
4632 4618
4633 #ifdef DYNAMIC_EXECUTION_PROFILE 4619 #ifdef DYNAMIC_EXECUTION_PROFILE
4634 4620
4635 static PyObject * 4621 static PyObject *
4636 getarray(long a[256]) 4622 getarray(long a[256])
4637 { 4623 {
4638 int i; 4624 int i;
4639 PyObject *l = PyList_New(256); 4625 PyObject *l = PyList_New(256);
4640 if (l == NULL) return NULL; 4626 if (l == NULL) return NULL;
(...skipping 25 matching lines...) Expand all
4666 Py_DECREF(l); 4652 Py_DECREF(l);
4667 return NULL; 4653 return NULL;
4668 } 4654 }
4669 PyList_SetItem(l, i, x); 4655 PyList_SetItem(l, i, x);
4670 } 4656 }
4671 return l; 4657 return l;
4672 #endif 4658 #endif
4673 } 4659 }
4674 4660
4675 #endif 4661 #endif
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+