summaryrefslogtreecommitdiffstats
path: root/Objects/codeobject.c
Commit message (Collapse)AuthorAgeFilesLines
* [3.13] GH-127953: Make line number lookup O(1) regardless of the size of the ↵Mark Shannon2025-04-071-0/+3
| | | | | code object (#129127) GH-127953: Make line number lookup O(1) regardless of the size of the code object (GH-128350)
* [3.13] gh-130851: Only intern constants of types generated by the compiler ↵Sam Gross2025-03-081-2/+34
| | | | | | | | | | | | | | | (GH-130901) (#130953) The free-threading build interns and immortalizes most constants generated by the bytecode compiler. However, users can construct their own code objects with arbitrary constants. We should not intern or immortalize these objects if they are not of a type that we know how to handle. This change fixes a reference leak failure in the recently added `test_code.test_unusual_constants` test. It also addresses a potential crash that could occur when attempting to destroy an immortalized object during interpreter shutdown. (cherry picked from commit 12db45211d411583cbe272c7ba6811a811b721ca)
* [3.13] gh-130851: Don't crash when deduping unusual code constants ↵Miss Islington (bot)2025-03-051-6/+12
| | | | | | | | | | | | | | | (GH-130853) (#130880) The bytecode compiler only generates a few different types of constants, like str, int, tuple, slices, etc. Users can construct code objects with various unusual constants, including ones that are not hashable or not even constant. The free threaded build previously crashed with a fatal error when confronted with these constants. Instead, treat distinct objects of otherwise unhandled types as not equal for the purposes of deduplication. (cherry picked from commit 2905690a91bf72cdf0fb919b5193849bb67732e2) Co-authored-by: Sam Gross <colesbury@gmail.com>
* [3.13] Postpone <stdbool.h> inclusion after Python.h (#130641) (#130675)Victor Stinner2025-02-281-2/+2
| | | | | | | | | | | | | Postpone <stdbool.h> inclusion after Python.h (#130641) Remove inclusions prior to Python.h. <stdbool.h> will cause <features.h> to be included before Python.h can define some macros to enable some additional features, causing multiple types not to be defined down the line. (cherry picked from commit 830f04b5056db92ba96387db0a778dcd19a39522) Co-authored-by: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
* [3.13] gh-127582: Make object resurrection thread-safe for free threading. ↵Sam Gross2024-12-051-5/+2
| | | | | | | | | | | | | | | (GH-127612) (GH-127659) Objects may be temporarily "resurrected" in destructors when calling finalizers or watcher callbacks. We previously undid the resurrection by decrementing the reference count using `Py_SET_REFCNT`. This was not thread-safe because other threads might be accessing the object (modifying its reference count) if it was exposed by the finalizer, watcher callback, or temporarily accessed by a racy dictionary or list access. This adds internal-only thread-safe functions for temporary object resurrection during destructors. (cherry picked from commit f4f530804b9d8f089eba0f157ec2144c03b13651)
* [3.13] gh-127020: Make `PyCode_GetCode` thread-safe for free threading ↵Miss Islington (bot)2024-11-211-27/+51
| | | | | | | | | (GH-127043) (GH-127107) Some fields in PyCodeObject are lazily initialized. Use atomics and critical sections to make their initializations and accesses thread-safe. (cherry picked from commit 3926842117feffe5d2c9727e1899bea5ae2adb28) Co-authored-by: Sam Gross <colesbury@gmail.com>
* [3.13] gh-121863: Immortalize names in code objects to avoid crash ↵Miss Islington (bot)2024-07-171-1/+1
| | | | | | | (GH-121903) (GH-121904) (cherry picked from commit cffad5c6ef9371b26e32556296cea2bfe8358b1a) Co-authored-by: Petr Viktorin <encukou@gmail.com>
* [3.13] gh-113993: Allow interned strings to be mortal, and fix related ↵Petr Viktorin2024-06-241-6/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | issues (GH-120520) (GH-120945) * Add an InternalDocs file describing how interning should work and how to use it. * Add internal functions to *explicitly* request what kind of interning is done: - `_PyUnicode_InternMortal` - `_PyUnicode_InternImmortal` - `_PyUnicode_InternStatic` * Switch uses of `PyUnicode_InternInPlace` to those. * Disallow using `_Py_SetImmortal` on strings directly. You should use `_PyUnicode_InternImmortal` instead: - Strings should be interned before immortalization, otherwise you're possibly interning a immortalizing copy. - `_Py_SetImmortal` doesn't handle the `SSTATE_INTERNED_MORTAL` to `SSTATE_INTERNED_IMMORTAL` update, and those flags can't be changed in backports, as they are now part of public API and version-specific ABI. * Add private `_only_immortal` argument for `sys.getunicodeinternedsize`, used in refleak test machinery. * Make sure the statically allocated string singletons are unique. This means these sets are now disjoint: - `_Py_ID` - `_Py_STR` (including the empty string) - one-character latin-1 singletons Now, when you intern a singleton, that exact singleton will be interned. * Add a `_Py_LATIN1_CHR` macro, use it instead of `_Py_ID`/`_Py_STR` for one-character latin-1 singletons everywhere (including Clinic). * Intern `_Py_STR` singletons at startup. * For free-threaded builds, intern `_Py_LATIN1_CHR` singletons at startup. * Beef up the tests. Cover internal details (marked with `@cpython_only`). * Add lots of assertions Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
* [3.13] gh-111389: Add PyHASH_MULTIPLIER constant (GH-119214) (#119334)Miss Islington (bot)2024-06-041-1/+1
| | | | | | gh-111389: Add PyHASH_MULTIPLIER constant (GH-119214) (cherry picked from commit f6da790122fdae1a28f444edfbb55202d6829cd1) Co-authored-by: Victor Stinner <vstinner@python.org>
* [3.13] gh-117657: Fix race involving immortalizing objects (GH-119927) (#120005)Sam Gross2024-06-031-2/+2
| | | | | | | | | | | The free-threaded build currently immortalizes objects that use deferred reference counting (see gh-117783). This typically happens once the first non-main thread is created, but the behavior can be suppressed for tests, in subinterpreters, or during a compile() call. This fixes a race condition involving the tracking of whether the behavior is suppressed. (cherry picked from commit 47fb4327b5c405da6df066dcaa01b7c1aefab313)
* gh-118527: Intern code consts in free-threaded build (#118667)Sam Gross2024-05-071-14/+288
| | | | | | We already intern and immortalize most string constants. In the free-threaded build, other constants can be a source of reference count contention because they are shared by all threads running the same code objects.
* gh-118527: Intern code name and filename on default build (#118576)Sam Gross2024-05-061-2/+0
| | | | Interned and non-interned strings are treated differently by `marshal`, so be consistent between the default and free-threaded build.
* gh-118527: Intern filename, name, and qualname in code objects. (#118558)Sam Gross2024-05-031-0/+5
| | | | | | This interns the strings for `co_filename`, `co_name`, and `co_qualname` on codeobjects in the free-threaded build. This partially addresses a reference counting bottleneck when creating closures concurrently. The closures take the name and qualified name from the code object.
* gh-117657: Disable the function/code cache in free-threaded builds (#118301)mpage2024-05-031-0/+6
| | | | This is only used by the specializing interpreter and the tier 2 optimizer, both of which are disabled in free-threaded builds.
* GH-118095: Make invalidating and clearing executors memory safe (GH-118459)Mark Shannon2024-05-011-1/+2
|
* gh-118335: Configure Tier 2 interpreter at build time (#118339)Guido van Rossum2024-05-011-0/+6
| | | | | | | | | | | | | | | | | | | | | | The code for Tier 2 is now only compiled when configured with `--enable-experimental-jit[=yes|interpreter]`. We drop support for `PYTHON_UOPS` and -`Xuops`, but you can disable the interpreter or JIT at runtime by setting `PYTHON_JIT=0`. You can also build it without enabling it by default using `--enable-experimental-jit=yes-off`; enable with `PYTHON_JIT=1`. On Windows, the `build.bat` script supports `--experimental-jit`, `--experimental-jit-off`, `--experimental-interpreter`. In the C code, `_Py_JIT` is defined as before when the JIT is enabled; the new variable `_Py_TIER2` is defined when the JIT *or* the interpreter is enabled. It is actually a bitmask: 1: JIT; 2: default-off; 4: interpreter.
* gh-117376: Make code objects use deferred reference counting (#117823)Sam Gross2024-04-161-1/+32
| | | | | | We want code objects to use deferred reference counting in the free-threaded build. This requires them to be tracked by the GC, so we set `Py_TPFLAGS_HAVE_GC` in the free-threaded build, but not the default build.
* gh-117764: Add docstrings and signatures for the __replace__ methods (GH-117768)Serhiy Storchaka2024-04-121-1/+2
|
* gh-108716:: Remove _PyStaticCode_Init/Fini (#117141)Guido van Rossum2024-03-221-46/+0
| | | More deepfreeze cleanup.
* gh-117045: Add code object to function version cache (#117028)Guido van Rossum2024-03-211-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | Changes to the function version cache: - In addition to the function object, also store the code object, and allow the latter to be retrieved even if the function has been evicted. - Stop assigning new function versions after a critical attribute (e.g. `__code__`) has been modified; the version is permanently reset to zero in this case. - Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.) Changes to the Tier 2 optimization machinery: - If we cannot map a function version to a function, but it is still mapped to a code object, we continue projecting the trace. The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL, a function object, or a code object with the lowest bit set. This allows us to trace through code that calls an ephemeral function, i.e., a function that may not be alive when we are constructing the executor, e.g. a generator expression or certain nested functions. We will lose globals removal inside such functions, but we can still do other peephole operations (and even possibly [call inlining](https://github.com/python/cpython/pull/116290), if we decide to do it), which only need the code object. As before, if we cannot retrieve the code object from the cache, we stop projecting.
* gh-116916: Remove separate next_func_version counter (#116918)Guido van Rossum2024-03-181-3/+3
| | | | | Somehow we ended up with two separate counter variables tracking "the next function version". Most likely this was a historical accident where an old branch was updated incorrectly. This PR merges the two counters into a single one: `interp->func_state.next_version`.
* GH-114695: Add `sys._clear_internal_caches` (GH-115152)Brandt Bucher2024-02-121-15/+7
|
* gh-110543: Fix CodeType.replace in presence of comprehensions (#110586)Jelle Zijlstra2023-11-081-0/+29
|
* gh-111354: simplify detection of RESUME after YIELD_VALUE at except-depth 1 ↵Irit Katriel2023-11-021-1/+2
| | | | (#111459)
* gh-108082: Use PyErr_FormatUnraisable() (GH-111580)Serhiy Storchaka2023-11-021-17/+3
| | | | | | Replace most of calls of _PyErr_WriteUnraisableMsg() and some calls of PyErr_WriteUnraisable(NULL) with PyErr_FormatUnraisable(). Co-authored-by: Victor Stinner <vstinner@python.org>
* GH-107265: Add missing deoptimizations for ENTER_EXECUTOR's original opcode ↵Tian Gao2023-09-221-1/+1
| | | | (GH-109420)
* gh-109052: Use the base opcode when comparing code objects (gh-109107)Tian Gao2023-09-091-6/+4
|
* GH-108716: Turn off deep-freezing of code objects. (GH-108722)Mark Shannon2023-09-081-3/+4
|
* gh-107265: Remove all ENTER_EXECUTOR when execute _Py_Instrument (gh-108539)Dong-hee Na2023-09-071-0/+17
|
* gh-108751: Add copy.replace() function (GH-108752)Serhiy Storchaka2023-09-061-0/+1
| | | | | | | | | It creates a modified copy of an object by calling the object's __replace__() method. It is a generalization of dataclasses.replace(), named tuple's _replace() method and replace() methods in various classes, and supports all these stdlib classes.
* gh-105481: remove regen-opcode. Generated _PyOpcode_Caches in regen-cases. ↵Irit Katriel2023-08-231-2/+1
| | | | (#108367)
* gh-107265: Fix code_hash for ENTER_EXECUTOR case (#108188)Dong-hee Na2023-08-211-19/+34
|
* gh-107265: Fix code_richcompare for ENTER_EXECUTOR case (gh-108165)Dong-hee Na2023-08-211-0/+17
|
* gh-106581: Project through calls (#108067)Guido van Rossum2023-08-171-0/+3
| | | | This finishes the work begun in gh-107760. When, while projecting a superblock, we encounter a call to a short, simple function, the superblock will now enter the function using `_PUSH_FRAME`, continue through it, and leave it using `_POP_FRAME`, and then continue through the original code. Multiple frame pushes and pops are even possible. It is also possible to stop appending to the superblock in the middle of a called function, when running out of space or encountering an unsupported bytecode.
* gh-105481: generate op IDs from bytecode.c instead of hard coding them in ↵Irit Katriel2023-08-161-1/+2
| | | | opcode.py (#107971)
* gh-86457: Fix signature for code.replace() (GH-23199)Serhiy Storchaka2023-08-071-29/+29
| | | | Also add support of @text_signature in Argument Clinic.
* gh-106869: Use new PyMemberDef constant names (#106871)Victor Stinner2023-07-251-15/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Remove '#include "structmember.h"'. * If needed, add <stddef.h> to get offsetof() function. * Update Parser/asdl_c.py to regenerate Python/Python-ast.c. * Replace: * T_SHORT => Py_T_SHORT * T_INT => Py_T_INT * T_LONG => Py_T_LONG * T_FLOAT => Py_T_FLOAT * T_DOUBLE => Py_T_DOUBLE * T_STRING => Py_T_STRING * T_OBJECT => _Py_T_OBJECT * T_CHAR => Py_T_CHAR * T_BYTE => Py_T_BYTE * T_UBYTE => Py_T_UBYTE * T_USHORT => Py_T_USHORT * T_UINT => Py_T_UINT * T_ULONG => Py_T_ULONG * T_STRING_INPLACE => Py_T_STRING_INPLACE * T_BOOL => Py_T_BOOL * T_OBJECT_EX => Py_T_OBJECT_EX * T_LONGLONG => Py_T_LONGLONG * T_ULONGLONG => Py_T_ULONGLONG * T_PYSSIZET => Py_T_PYSSIZET * T_NONE => _Py_T_NONE * READONLY => Py_READONLY * PY_AUDIT_READ => Py_AUDIT_READ * READ_RESTRICTED => Py_AUDIT_READ * PY_WRITE_RESTRICTED => _Py_WRITE_RESTRICTED * RESTRICTED => (READ_RESTRICTED | _Py_WRITE_RESTRICTED)
* gh-106320: Move private _PySet API to the internal API (#107041)Victor Stinner2023-07-221-0/+1
| | | | | | | | * Add pycore_setobject.h header file. * Move the following API to the internal C API: * _PySet_Dummy * _PySet_NextEntry() * _PySet_Update()
* Fix possible refleak in CodeType.replace() (GH-106243)Serhiy Storchaka2023-06-291-0/+1
| | | A reference to c_code was leaked if PySys_Audit() failed.
* GH-104584: Assorted fixes for the optimizer API. (GH-105683)Mark Shannon2023-06-191-4/+20
| | | | | | * Add test for long loops * Clear ENTER_EXECUTOR when deopting code objects.
* GH-100987: Allow objects other than code objects as the "executable" of an ↵Mark Shannon2023-06-141-73/+0
| | | | | | | | | | internal frame. (GH-105727) * Add table describing possible executable classes for out-of-process debuggers. * Remove shim code object creation code as it is no longer needed. * Make lltrace a bit more robust w.r.t. non-standard frames.
* GH-104584: Plugin optimizer API (GH-105100)Mark Shannon2023-06-021-0/+7
|
* gh-92536: Remove PyUnicode_READY() calls (#105210)Victor Stinner2023-06-011-15/+0
| | | | Since Python 3.12, PyUnicode_READY() does nothing and always returns 0.
* GH-103082: Implementation of PEP 669: Low Impact Monitoring for CPython ↵Mark Shannon2023-04-121-69/+38
| | | | | | | | | | (GH-103083) * The majority of the monitoring code is in instrumentation.c * The new instrumentation bytecodes are in bytecodes.c * legacy_tracing.c adapts the new API to the old sys.setrace and sys.setprofile APIs
* gh-101865: Deprecate `co_lnotab` from code objects as per PEP 626 (#101866)Nikita Sobolev2023-04-031-0/+5
| | | Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
* gh-102381: don't call watcher callback with dead object (#102382)Carl Meyer2023-03-081-1/+37
| | | Co-authored-by: T. Wouters <thomas@python.org>
* gh-101101: Unstable C API tier (PEP 689) (GH-101102)Petr Viktorin2023-02-281-4/+5
|
* gh-101907: Stop using `_Py_OPCODE` and `_Py_OPARG` macros (GH-101912)Steve Dower2023-02-201-10/+10
| | | | | | * gh-101907: Removes use of non-standard C++ extension from Include/cpython/code.h * Make cases_generator correct on Windows
* GH-100117: Make `co_lines` more efficient (GH-100447)Brandt Bucher2023-01-101-32/+18
|
* GH-100719: Remove the `co_nplaincellvars` field from code objects. (GH-100721)Mark Shannon2023-01-041-10/+4
|