GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cpython/Objects/codeobject.c at 3.15 · python/cpython Expand file tree/ Copy pathcodeobject.cMore file actions 3580 lines (3265 loc) · 110 KB / Copy pathcodeobject.cFile metadata and controls3580 lines (3265 loc) · 110 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 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 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 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 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 #include "Python.h" #include "opcode.h" #include "pycore_code.h" // _PyCodeConstructor #include "pycore_function.h" // _PyFunction_ClearCodeByVersion() #include "pycore_hashtable.h" // _Py_hashtable_t #include "pycore_index_pool.h" // _PyIndexPool_Fini() #include "pycore_initconfig.h" // _PyStatus_OK() #include "pycore_interp.h" // PyInterpreterState.co_extra_freefuncs #include "pycore_interpframe.h" // FRAME_SPECIALS_SIZE #include "pycore_opcode_metadata.h" // _PyOpcode_Caches #include "pycore_opcode_utils.h" // RESUME_AT_FUNC_START #include "pycore_optimizer.h" // _Py_ExecutorDetach #include "pycore_pymem.h" // _PyMem_FreeDelayed() #include "pycore_pystate.h" // _PyInterpreterState_GET() #include "pycore_setobject.h" // _PySet_NextEntry() #include "pycore_tuple.h" // _PyTuple_ITEMS() #include "pycore_unicodeobject.h" // _PyUnicode_InternImmortal() #include "pycore_uniqueid.h" // _PyObject_AssignUniqueId() #include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() #include "clinic/codeobject.c.h" #include <stdbool.h> #define INITIAL_SPECIALIZED_CODE_SIZE 16 static const char * code_event_name(PyCodeEvent event) { switch (event) { #define CASE(op) \ case PY_CODE_EVENT_##op: \ return "PY_CODE_EVENT_" #op; PY_FOREACH_CODE_EVENT(CASE) #undef CASE } Py_UNREACHABLE(); } static void notify_code_watchers(PyCodeEvent event, PyCodeObject *co) { assert(Py_REFCNT(co) > 0); PyInterpreterState *interp = _PyInterpreterState_GET(); assert(interp->_initialized); uint8_t bits = interp->active_code_watchers; int i = 0; while (bits) { assert(i < CODE_MAX_WATCHERS); if (bits & 1) { PyCode_WatchCallback cb = interp->code_watchers[i]; // callback must be non-null if the watcher bit is set assert(cb != NULL); if (cb(event, co) < 0) { PyErr_FormatUnraisable( "Exception ignored in %s watcher callback for %R", code_event_name(event), co); } } i++; bits >>= 1; } } int PyCode_AddWatcher(PyCode_WatchCallback callback) { PyInterpreterState *interp = _PyInterpreterState_GET(); assert(interp->_initialized); for (int i = 0; i < CODE_MAX_WATCHERS; i++) { if (!interp->code_watchers[i]) { interp->code_watchers[i] = callback; interp->active_code_watchers |= (1 << i); return i; } } PyErr_SetString(PyExc_RuntimeError, "no more code watcher IDs available"); return -1; } static inline int validate_watcher_id(PyInterpreterState *interp, int watcher_id) { if (watcher_id < 0 || watcher_id >= CODE_MAX_WATCHERS) { PyErr_Format(PyExc_ValueError, "Invalid code watcher ID %d", watcher_id); return -1; } if (!interp->code_watchers[watcher_id]) { PyErr_Format(PyExc_ValueError, "No code watcher set for ID %d", watcher_id); return -1; } return 0; } int PyCode_ClearWatcher(int watcher_id) { PyInterpreterState *interp = _PyInterpreterState_GET(); assert(interp->_initialized); if (validate_watcher_id(interp, watcher_id) < 0) { return -1; } interp->code_watchers[watcher_id] = NULL; interp->active_code_watchers &= ~(1 << watcher_id); return 0; } /****************** * generic helpers ******************/ #define _PyCodeObject_CAST(op) (assert(PyCode_Check(op)), (PyCodeObject *)(op)) static int should_intern_string(PyObject *o) { #ifdef Py_GIL_DISABLED // The free-threaded build interns (and immortalizes) all string constants return 1; #else // compute if s matches [a-zA-Z0-9_] const unsigned char *s, *e; if (!PyUnicode_IS_ASCII(o)) return 0; s = PyUnicode_1BYTE_DATA(o); e = s + PyUnicode_GET_LENGTH(o); for (; s != e; s++) { if (!Py_ISALNUM(*s) && *s != '_') return 0; } return 1; #endif } #ifdef Py_GIL_DISABLED static PyObject *intern_one_constant(PyObject *op); // gh-130851: In the free threading build, we intern and immortalize most // constants, except code objects. However, users can generate code objects // with arbitrary co_consts. We don't want to immortalize or intern unexpected // constants or tuples/sets containing unexpected constants. static int should_immortalize_constant(PyObject *v) { // Only immortalize containers if we've already immortalized all their // elements. if (PyTuple_CheckExact(v)) { for (Py_ssize_t i = PyTuple_GET_SIZE(v); --i >= 0; ) { if (!_Py_IsImmortal(PyTuple_GET_ITEM(v, i))) { return 0; } } return 1; } else if (PyFrozenSet_CheckExact(v)) { PyObject *item; Py_hash_t hash; Py_ssize_t pos = 0; while (_PySet_NextEntry(v, &pos, &item, &hash)) { if (!_Py_IsImmortal(item)) { return 0; } } return 1; } else if (PySlice_Check(v)) { PySliceObject *slice = (PySliceObject *)v; return (_Py_IsImmortal(slice->start) && _Py_IsImmortal(slice->stop) && _Py_IsImmortal(slice->step)); } // The hash of a NaN depends on its address, so the table would keep every // NaN object it gets (and merging them would shrink frozensets). if (PyFloat_CheckExact(v)) { return !isnan(PyFloat_AS_DOUBLE(v)); } if (PyComplex_CheckExact(v)) { Py_complex c = ((PyComplexObject *)v)->cval; return !isnan(c.real) && !isnan(c.imag); } return PyLong_CheckExact(v) || PyBytes_CheckExact(v); } #endif static int intern_strings(PyObject *tuple) { PyInterpreterState *interp = _PyInterpreterState_GET(); Py_ssize_t i; for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { PyObject *v = PyTuple_GET_ITEM(tuple, i); if (v == NULL || !PyUnicode_CheckExact(v)) { PyErr_SetString(PyExc_SystemError, "non-string found in code slot"); return -1; } _PyUnicode_InternImmortal(interp, &_PyTuple_ITEMS(tuple)[i]); } return 0; } /* Intern constants. In the default build, this interns selected string constants. In the free-threaded build, this also interns non-string constants. */ static int intern_constants(PyObject *tuple, int *modified) { PyInterpreterState *interp = _PyInterpreterState_GET(); for (Py_ssize_t i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { PyObject *v = PyTuple_GET_ITEM(tuple, i); if (PyUnicode_CheckExact(v)) { if (should_intern_string(v)) { PyObject *w = v; _PyUnicode_InternMortal(interp, &v); if (w != v) { PyTuple_SET_ITEM(tuple, i, v); if (modified) { *modified = 1; } } } } else if (PyTuple_CheckExact(v)) { if (intern_constants(v, NULL) < 0) { return -1; } } else if (PyFrozenSet_CheckExact(v)) { PyObject *w = v; PyObject *tmp = PySequence_Tuple(v); if (tmp == NULL) { return -1; } int tmp_modified = 0; if (intern_constants(tmp, &tmp_modified) < 0) { Py_DECREF(tmp); return -1; } if (tmp_modified) { v = PyFrozenSet_New(tmp); if (v == NULL) { Py_DECREF(tmp); return -1; } PyTuple_SET_ITEM(tuple, i, v); Py_DECREF(w); if (modified) { *modified = 1; } } Py_DECREF(tmp); } #ifdef Py_GIL_DISABLED else if (PySlice_Check(v)) { PySliceObject *slice = (PySliceObject *)v; PyObject *tmp = PyTuple_New(3); if (tmp == NULL) { return -1; } PyTuple_SET_ITEM(tmp, 0, Py_NewRef(slice->start)); PyTuple_SET_ITEM(tmp, 1, Py_NewRef(slice->stop)); PyTuple_SET_ITEM(tmp, 2, Py_NewRef(slice->step)); int tmp_modified = 0; if (intern_constants(tmp, &tmp_modified) < 0) { Py_DECREF(tmp); return -1; } if (tmp_modified) { v = PySlice_New(PyTuple_GET_ITEM(tmp, 0), PyTuple_GET_ITEM(tmp, 1), PyTuple_GET_ITEM(tmp, 2)); if (v == NULL) { Py_DECREF(tmp); return -1; } PyTuple_SET_ITEM(tuple, i, v); Py_DECREF(slice); if (modified) { *modified = 1; } } Py_DECREF(tmp); } // Intern non-string constants in the free-threaded build _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET(); if (!_Py_IsImmortal(v) && !PyUnicode_CheckExact(v) && should_immortalize_constant(v) && !tstate->suppress_co_const_immortalization) { PyObject *interned = intern_one_constant(v); if (interned == NULL) { return -1; } else if (interned != v) { PyTuple_SET_ITEM(tuple, i, interned); Py_SETREF(v, interned); if (modified) { *modified = 1; } } } #endif } return 0; } /* Return a shallow copy of a tuple that is guaranteed to contain exact strings, by converting string subclasses to exact strings and complaining if a non-string is found. */ static PyObject* validate_and_copy_tuple(PyObject *tup) { PyObject *newtuple; PyObject *item; Py_ssize_t i, len; len = PyTuple_GET_SIZE(tup); newtuple = PyTuple_New(len); if (newtuple == NULL) return NULL; for (i = 0; i < len; i++) { item = PyTuple_GET_ITEM(tup, i); if (PyUnicode_CheckExact(item)) { Py_INCREF(item); } else if (!PyUnicode_Check(item)) { PyErr_Format( PyExc_TypeError, "name tuples must contain only " "strings, not '%.500s'", Py_TYPE(item)->tp_name); Py_DECREF(newtuple); return NULL; } else { item = _PyUnicode_Copy(item); if (item == NULL) { Py_DECREF(newtuple); return NULL; } } PyTuple_SET_ITEM(newtuple, i, item); } return newtuple; } static int init_co_cached(PyCodeObject *self) { _PyCoCached *cached = FT_ATOMIC_LOAD_PTR(self->_co_cached); if (cached != NULL) { return 0; } Py_BEGIN_CRITICAL_SECTION(self); cached = self->_co_cached; if (cached == NULL) { cached = PyMem_New(_PyCoCached, 1); if (cached == NULL) { PyErr_NoMemory(); } else { cached->_co_code = NULL; cached->_co_cellvars = NULL; cached->_co_freevars = NULL; cached->_co_varnames = NULL; FT_ATOMIC_STORE_PTR(self->_co_cached, cached); } } Py_END_CRITICAL_SECTION(); return cached != NULL ? 0 : -1; } /****************** * _PyCode_New() ******************/ // This is also used in compile.c. void _Py_set_localsplus_info(int offset, PyObject *name, _PyLocals_Kind kind, PyObject *names, PyObject *kinds) { PyTuple_SET_ITEM(names, offset, Py_NewRef(name)); _PyLocals_SetKind(kinds, offset, kind); } static void get_localsplus_counts(PyObject *names, PyObject *kinds, int *pnlocals, int *pncellvars, int *pnfreevars) { int nlocals = 0; int ncellvars = 0; int nfreevars = 0; Py_ssize_t nlocalsplus = PyTuple_GET_SIZE(names); for (int i = 0; i < nlocalsplus; i++) { _PyLocals_Kind kind = _PyLocals_GetKind(kinds, i); if (kind & CO_FAST_LOCAL) { nlocals += 1; if (kind & CO_FAST_CELL) { ncellvars += 1; } } else if (kind & CO_FAST_CELL) { ncellvars += 1; } else if (kind & CO_FAST_FREE) { nfreevars += 1; } } if (pnlocals != NULL) { *pnlocals = nlocals; } if (pncellvars != NULL) { *pncellvars = ncellvars; } if (pnfreevars != NULL) { *pnfreevars = nfreevars; } } static PyObject * get_localsplus_names(PyCodeObject *co, _PyLocals_Kind kind, int num) { PyObject *names = PyTuple_New(num); if (names == NULL) { return NULL; } int index = 0; for (int offset = 0; offset < co->co_nlocalsplus; offset++) { _PyLocals_Kind k = _PyLocals_GetKind(co->co_localspluskinds, offset); if ((k & kind) == 0) { continue; } assert(index < num); PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, offset); PyTuple_SET_ITEM(names, index, Py_NewRef(name)); index += 1; } assert(index == num); return names; } int _PyCode_Validate(struct _PyCodeConstructor *con) { /* Check argument types */ if (con->argcount < con->posonlyargcount || con->posonlyargcount < 0 || con->kwonlyargcount < 0 || con->stacksize < 0 || con->flags < 0 || con->code == NULL || !PyBytes_Check(con->code) || con->consts == NULL || !PyTuple_Check(con->consts) || con->names == NULL || !PyTuple_Check(con->names) || con->localsplusnames == NULL || !PyTuple_Check(con->localsplusnames) || con->localspluskinds == NULL || !PyBytes_Check(con->localspluskinds) || PyTuple_GET_SIZE(con->localsplusnames) != PyBytes_GET_SIZE(con->localspluskinds) || con->name == NULL || !PyUnicode_Check(con->name) || con->qualname == NULL || !PyUnicode_Check(con->qualname) || con->filename == NULL || !PyUnicode_Check(con->filename) || con->linetable == NULL || !PyBytes_Check(con->linetable) || con->exceptiontable == NULL || !PyBytes_Check(con->exceptiontable) ) { PyErr_BadInternalCall(); return -1; } /* Make sure that code is indexable with an int, this is a long running assumption in ceval.c and many parts of the interpreter. */ if (PyBytes_GET_SIZE(con->code) > INT_MAX) { PyErr_SetString(PyExc_OverflowError, "code: co_code larger than INT_MAX"); return -1; } if (PyBytes_GET_SIZE(con->code) % sizeof(_Py_CODEUNIT) != 0 || !_Py_IS_ALIGNED(PyBytes_AS_STRING(con->code), sizeof(_Py_CODEUNIT)) ) { PyErr_SetString(PyExc_ValueError, "code: co_code is malformed"); return -1; } /* Ensure that the co_varnames has enough names to cover the arg counts. * Note that totalargs = nlocals - nplainlocals. We check nplainlocals * here to avoid the possibility of overflow (however remote). */ int nlocals; get_localsplus_counts(con->localsplusnames, con->localspluskinds, &nlocals, NULL, NULL); int nplainlocals = nlocals - con->argcount - con->kwonlyargcount - ((con->flags & CO_VARARGS) != 0) - ((con->flags & CO_VARKEYWORDS) != 0); if (nplainlocals < 0) { PyErr_SetString(PyExc_ValueError, "code: co_varnames is too small"); return -1; } return 0; } extern void _PyCode_Quicken(_Py_CODEUNIT *instructions, Py_ssize_t size, int enable_counters, int flags); #ifdef Py_GIL_DISABLED static _PyCodeArray * _PyCodeArray_New(Py_ssize_t size); #endif static int init_code(PyCodeObject *co, struct _PyCodeConstructor *con) { int nlocalsplus = (int)PyTuple_GET_SIZE(con->localsplusnames); int nlocals, ncellvars, nfreevars; get_localsplus_counts(con->localsplusnames, con->localspluskinds, &nlocals, &ncellvars, &nfreevars); if (con->stacksize == 0) { con->stacksize = 1; } PyInterpreterState *interp = _PyInterpreterState_GET(); co->co_filename = Py_NewRef(con->filename); co->co_name = Py_NewRef(con->name); co->co_qualname = Py_NewRef(con->qualname); _PyUnicode_InternMortal(interp, &co->co_filename); _PyUnicode_InternMortal(interp, &co->co_name); _PyUnicode_InternMortal(interp, &co->co_qualname); co->co_flags = con->flags; co->co_firstlineno = con->firstlineno; co->co_linetable = Py_NewRef(con->linetable); co->co_consts = Py_NewRef(con->consts); co->co_names = Py_NewRef(con->names); co->co_localsplusnames = Py_NewRef(con->localsplusnames); co->co_localspluskinds = Py_NewRef(con->localspluskinds); co->co_argcount = con->argcount; co->co_posonlyargcount = con->posonlyargcount; co->co_kwonlyargcount = con->kwonlyargcount; co->co_stacksize = con->stacksize; co->co_exceptiontable = Py_NewRef(con->exceptiontable); /* derived values */ co->co_nlocalsplus = nlocalsplus; co->co_nlocals = nlocals; co->co_framesize = nlocalsplus + con->stacksize + FRAME_SPECIALS_SIZE; co->co_ncellvars = ncellvars; co->co_nfreevars = nfreevars; FT_MUTEX_LOCK(&interp->func_state.mutex); co->co_version = interp->func_state.next_version; if (interp->func_state.next_version != 0) { interp->func_state.next_version++; } FT_MUTEX_UNLOCK(&interp->func_state.mutex); co->_co_monitoring = NULL; co->_co_instrumentation_version = 0; /* not set */ co->co_weakreflist = NULL; co->co_extra = NULL; co->_co_cached = NULL; co->co_executors = NULL; memcpy(_PyCode_CODE(co), PyBytes_AS_STRING(con->code), PyBytes_GET_SIZE(con->code)); #ifdef Py_GIL_DISABLED co->co_tlbc = _PyCodeArray_New(INITIAL_SPECIALIZED_CODE_SIZE); if (co->co_tlbc == NULL) { return -1; } co->co_tlbc->entries[0] = co->co_code_adaptive; #endif int entry_point = 0; while (entry_point < Py_SIZE(co)) { if (_PyCode_CODE(co)[entry_point].op.code == RESUME && (_PyCode_CODE(co)[entry_point].op.arg & RESUME_OPARG_LOCATION_MASK) != RESUME_AT_GEN_EXPR_START ) { break; } entry_point++; } co->_co_firsttraceable = entry_point; #ifdef Py_GIL_DISABLED int enable_counters = interp->config.tlbc_enabled && interp->opt_config.specialization_enabled; _PyCode_Quicken(_PyCode_CODE(co), Py_SIZE(co), enable_counters, co->co_flags); #else _PyCode_Quicken(_PyCode_CODE(co), Py_SIZE(co), interp->opt_config.specialization_enabled, co->co_flags); #endif notify_code_watchers(PY_CODE_EVENT_CREATE, co); return 0; } static int scan_varint(const uint8_t *ptr) { unsigned int read = *ptr++; unsigned int val = read & 63; unsigned int shift = 0; while (read & 64) { read = *ptr++; shift += 6; val |= (read & 63) << shift; } return val; } static int scan_signed_varint(const uint8_t *ptr) { unsigned int uval = scan_varint(ptr); if (uval & 1) { return -(int)(uval >> 1); } else { return uval >> 1; } } static int get_line_delta(const uint8_t *ptr) { int code = ((*ptr) >> 3) & 15; switch (code) { case PY_CODE_LOCATION_INFO_NONE: return 0; case PY_CODE_LOCATION_INFO_NO_COLUMNS: case PY_CODE_LOCATION_INFO_LONG: return scan_signed_varint(ptr+1); case PY_CODE_LOCATION_INFO_ONE_LINE0: return 0; case PY_CODE_LOCATION_INFO_ONE_LINE1: return 1; case PY_CODE_LOCATION_INFO_ONE_LINE2: return 2; default: /* Same line */ return 0; } } static PyObject * remove_column_info(PyObject *locations) { Py_ssize_t offset = 0; const uint8_t *data = (const uint8_t *)PyBytes_AS_STRING(locations); PyObject *res = PyBytes_FromStringAndSize(NULL, 32); if (res == NULL) { PyErr_NoMemory(); return NULL; } uint8_t *output = (uint8_t *)PyBytes_AS_STRING(res); while (offset < PyBytes_GET_SIZE(locations)) { Py_ssize_t write_offset = output - (uint8_t *)PyBytes_AS_STRING(res); if (write_offset + 16 >= PyBytes_GET_SIZE(res)) { if (_PyBytes_Resize(&res, PyBytes_GET_SIZE(res) * 2) < 0) { return NULL; } output = (uint8_t *)PyBytes_AS_STRING(res) + write_offset; } int code = (data[offset] >> 3) & 15; if (code == PY_CODE_LOCATION_INFO_NONE) { *output++ = data[offset]; } else { int blength = (data[offset] & 7)+1; output += write_location_entry_start( output, PY_CODE_LOCATION_INFO_NO_COLUMNS, blength); int ldelta = get_line_delta(&data[offset]); output += write_signed_varint(output, ldelta); } offset++; while (offset < PyBytes_GET_SIZE(locations) && (data[offset] & 128) == 0) { offset++; } } Py_ssize_t write_offset = output - (uint8_t *)PyBytes_AS_STRING(res); if (_PyBytes_Resize(&res, write_offset)) { return NULL; } return res; } static int intern_code_constants(struct _PyCodeConstructor *con) { #ifdef Py_GIL_DISABLED PyInterpreterState *interp = _PyInterpreterState_GET(); struct _py_code_state *state = &interp->code_state; FT_MUTEX_LOCK(&state->mutex); #endif if (intern_strings(con->names) < 0) { goto error; } if (intern_constants(con->consts, NULL) < 0) { goto error; } if (intern_strings(con->localsplusnames) < 0) { goto error; } FT_MUTEX_UNLOCK(&state->mutex); return 0; error: FT_MUTEX_UNLOCK(&state->mutex); return -1; } /* The caller is responsible for ensuring that the given data is valid. */ PyCodeObject * _PyCode_New(struct _PyCodeConstructor *con) { if (intern_code_constants(con) < 0) { return NULL; } PyObject *replacement_locations = NULL; // Compact the linetable if we are opted out of debug // ranges. if (!_Py_GetConfig()->code_debug_ranges) { replacement_locations = remove_column_info(con->linetable); if (replacement_locations == NULL) { return NULL; } con->linetable = replacement_locations; } Py_ssize_t size = PyBytes_GET_SIZE(con->code) / sizeof(_Py_CODEUNIT); PyCodeObject *co; #ifdef Py_GIL_DISABLED co = PyObject_GC_NewVar(PyCodeObject, &PyCode_Type, size); #else co = PyObject_NewVar(PyCodeObject, &PyCode_Type, size); #endif if (co == NULL) { Py_XDECREF(replacement_locations); PyErr_NoMemory(); return NULL; } #ifdef Py_GIL_DISABLED co->_co_unique_id = _Py_INVALID_UNIQUE_ID; #endif if (init_code(co, con) < 0) { Py_DECREF(co); return NULL; } #ifdef Py_GIL_DISABLED co->_co_unique_id = _PyObject_AssignUniqueId((PyObject *)co); _PyObject_GC_TRACK(co); #endif Py_XDECREF(replacement_locations); return co; } /****************** * the legacy "constructors" ******************/ PyCodeObject * PyUnstable_Code_NewWithPosOnlyArgs( int argcount, int posonlyargcount, int kwonlyargcount, int nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *filename, PyObject *name, PyObject *qualname, int firstlineno, PyObject *linetable, PyObject *exceptiontable) { PyCodeObject *co = NULL; PyObject *localsplusnames = NULL; PyObject *localspluskinds = NULL; if (varnames == NULL || !PyTuple_Check(varnames) || cellvars == NULL || !PyTuple_Check(cellvars) || freevars == NULL || !PyTuple_Check(freevars) ) { PyErr_BadInternalCall(); return NULL; } // Set the "fast locals plus" info. int nvarnames = (int)PyTuple_GET_SIZE(varnames); int ncellvars = (int)PyTuple_GET_SIZE(cellvars); int nfreevars = (int)PyTuple_GET_SIZE(freevars); int nlocalsplus = nvarnames + ncellvars + nfreevars; localsplusnames = PyTuple_New(nlocalsplus); if (localsplusnames == NULL) { goto error; } localspluskinds = PyBytes_FromStringAndSize(NULL, nlocalsplus); if (localspluskinds == NULL) { goto error; } int offset = 0; for (int i = 0; i < nvarnames; i++, offset++) { PyObject *name = PyTuple_GET_ITEM(varnames, i); _Py_set_localsplus_info(offset, name, CO_FAST_LOCAL, localsplusnames, localspluskinds); } for (int i = 0; i < ncellvars; i++, offset++) { PyObject *name = PyTuple_GET_ITEM(cellvars, i); int argoffset = -1; for (int j = 0; j < nvarnames; j++) { int cmp = PyUnicode_Compare(PyTuple_GET_ITEM(varnames, j), name); assert(!PyErr_Occurred()); if (cmp == 0) { argoffset = j; break; } } if (argoffset >= 0) { // Merge the localsplus indices. nlocalsplus -= 1; offset -= 1; _PyLocals_Kind kind = _PyLocals_GetKind(localspluskinds, argoffset); _PyLocals_SetKind(localspluskinds, argoffset, kind | CO_FAST_CELL); continue; } _Py_set_localsplus_info(offset, name, CO_FAST_CELL, localsplusnames, localspluskinds); } for (int i = 0; i < nfreevars; i++, offset++) { PyObject *name = PyTuple_GET_ITEM(freevars, i); _Py_set_localsplus_info(offset, name, CO_FAST_FREE, localsplusnames, localspluskinds); } // gh-110543: Make sure the CO_FAST_HIDDEN flag is set correctly. if (!(flags & CO_OPTIMIZED)) { Py_ssize_t code_len = PyBytes_GET_SIZE(code); _Py_CODEUNIT *code_data = (_Py_CODEUNIT *)PyBytes_AS_STRING(code); Py_ssize_t num_code_units = code_len / sizeof(_Py_CODEUNIT); int extended_arg = 0; for (int i = 0; i < num_code_units; i += 1 + _PyOpcode_Caches[code_data[i].op.code]) { _Py_CODEUNIT *instr = &code_data[i]; uint8_t opcode = instr->op.code; if (opcode == EXTENDED_ARG) { extended_arg = extended_arg << 8 | instr->op.arg; continue; } if (opcode == LOAD_FAST_AND_CLEAR) { int oparg = extended_arg << 8 | instr->op.arg; if (oparg >= nlocalsplus) { PyErr_Format(PyExc_ValueError, "code: LOAD_FAST_AND_CLEAR oparg %d out of range", oparg); goto error; } _PyLocals_Kind kind = _PyLocals_GetKind(localspluskinds, oparg); _PyLocals_SetKind(localspluskinds, oparg, kind | CO_FAST_HIDDEN); } extended_arg = 0; } } // If any cells were args then nlocalsplus will have shrunk. if (nlocalsplus != PyTuple_GET_SIZE(localsplusnames)) { if (_PyTuple_Resize(&localsplusnames, nlocalsplus) < 0 || _PyBytes_Resize(&localspluskinds, nlocalsplus) < 0) { goto error; } } struct _PyCodeConstructor con = { .filename = filename, .name = name, .qualname = qualname, .flags = flags, .code = code, .firstlineno = firstlineno, .linetable = linetable, .consts = consts, .names = names, .localsplusnames = localsplusnames, .localspluskinds = localspluskinds, .argcount = argcount, .posonlyargcount = posonlyargcount, .kwonlyargcount = kwonlyargcount, .stacksize = stacksize, .exceptiontable = exceptiontable, }; if (_PyCode_Validate(&con) < 0) { goto error; } assert(PyBytes_GET_SIZE(code) % sizeof(_Py_CODEUNIT) == 0); assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(code), sizeof(_Py_CODEUNIT))); if (nlocals != PyTuple_GET_SIZE(varnames)) { PyErr_SetString(PyExc_ValueError, "code: co_nlocals != len(co_varnames)"); goto error; } co = _PyCode_New(&con); if (co == NULL) { goto error; } error: Py_XDECREF(localsplusnames); Py_XDECREF(localspluskinds); return co; } PyCodeObject * PyUnstable_Code_New(int argcount, int kwonlyargcount, int nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *filename, PyObject *name, PyObject *qualname, int firstlineno, PyObject *linetable, PyObject *exceptiontable) { return PyCode_NewWithPosOnlyArgs(argcount, 0, kwonlyargcount, nlocals, stacksize, flags, code, consts, names, varnames, freevars, cellvars, filename, name, qualname, firstlineno, linetable, exceptiontable); } // NOTE: When modifying the construction of PyCode_NewEmpty, please also change // test.test_code.CodeLocationTest.test_code_new_empty to keep it in sync! static const uint8_t assert0[8] = { RESUME, RESUME_AT_FUNC_START, CACHE, 0, LOAD_COMMON_CONSTANT, CONSTANT_ASSERTIONERROR, RAISE_VARARGS, 1 }; static const uint8_t linetable[2] = { (1 << 7) // New entry. | (PY_CODE_LOCATION_INFO_NO_COLUMNS << 3) | (4 - 1), // Four code units. 0, // Offset from co_firstlineno. }; PyCodeObject * PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno) { PyObject *nulltuple = NULL; PyObject *filename_ob = NULL; PyObject *funcname_ob = NULL; PyObject *code_ob = NULL; PyObject *linetable_ob = NULL; PyCodeObject *result = NULL; nulltuple = PyTuple_New(0); if (nulltuple == NULL) { goto failed; } funcname_ob = PyUnicode_FromString(funcname); if (funcname_ob == NULL) { goto failed; } filename_ob = PyUnicode_DecodeFSDefault(filename); if (filename_ob == NULL) { goto failed; } code_ob = PyBytes_FromStringAndSize((const char *)assert0, 8); if (code_ob == NULL) { goto failed; } linetable_ob = PyBytes_FromStringAndSize((const char *)linetable, 2); if (linetable_ob == NULL) { goto failed; } #define emptystring (PyObject *)&_Py_SINGLETON(bytes_empty) struct _PyCodeConstructor con = { .filename = filename_ob, .name = funcname_ob, .qualname = funcname_ob, |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |