aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Objects/codeobject.c
Commit message (Collapse)AuthorAge
* Revert "gh-132775: Add _PyCode_GetVarCounts() (gh-133128)" (gh-133232)Eric Snow10 days
| | | | | The change broke the s390 builds, so I'm reverting it while I investigate. This reverts commit 94b4fcd806e7b692955173d309ea3b70a193ad96.
* gh-132775: Add _PyCode_GetVarCounts() (gh-133128)Eric Snow11 days
| | | | | | | This helper is useful in a variety of ways, including in demonstrating how the different counts relate to one another. It will be used in a later change to help identify if a function is "stateless", meaning it doesn't have any free vars or globals. Note that a majority of this change is tests.
* gh-132775: Add _PyCode_ReturnsOnlyNone() (gh-132981)Eric Snow12 days
| | | | | The function indicates whether or not the function has a return statement. This is used by a later change related treating some functions like scripts.
* gh-132399: fix invalid function signatures on the free-threaded build (#132400)Bénédikt Tran2025-04-12
|
* gh-131238: Remove pycore_object_deferred.h from pycore_object.h (#131549)Victor Stinner2025-03-21
| | | Remove also pycore_function.h from pycore_typeobject.h.
* GH-131498: Remove conditional stack effects (GH-131499)Mark Shannon2025-03-20
| | | * Adds some missing #includes
* gh-131238: Remove includes from pycore_interp.h (#131495)Victor Stinner2025-03-20
| | | Remove also now unused includes in C files.
* gh-131238: Remove more includes from pycore_interp.h (#131480)Victor Stinner2025-03-19
|
* gh-111178: Fix function signatures to fix undefined behavior (#131191)Victor Stinner2025-03-14
|
* gh-130851: Only intern constants of types generated by the compiler (#130901)Sam Gross2025-03-07
| | | | | | | | | | | | | 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.
* gh-130851: Don't crash when deduping unusual code constants (#130853)Sam Gross2025-03-05
| | | | | | | | | | 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.
* Postpone <stdbool.h> inclusion after Python.h (#130641)Hugo Beauzée-Luyssen2025-02-28
| | | | | | | 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.
* GH-128872: Remove unused argument from _PyCode_Quicken (GH-128873)Yan Yanchii2025-02-02
| | | Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
* GH-127953: Make line number lookup O(1) regardless of the size of the code ↵Mark Shannon2025-01-21
| | | | object (GH-128350)
* gh-111178: Generate correct signature for most self converters (#128447)Erlend E. Aasland2025-01-20
|
* gh-128923: Use zero to indicate unassigned unique id (#128925)Sam Gross2025-01-17
| | | | | | | | In the free threading build, the per thread reference counting uses a unique id for some objects to index into the local reference count table. Use 0 instead of -1 to indicate that the id is not assigned. This avoids bugs where zero-initialized heap type objects look like they have a unique id assigned.
* gh-111178: fix UBSan failures in `Objects/codeobject.c` (GH-128240)Bénédikt Tran2025-01-13
|
* GH-122548: Implement branch taken and not taken events for sys.monitoring ↵Mark Shannon2024-12-19
| | | | (GH-122564)
* gh-127582: Make object resurrection thread-safe for free threading. (GH-127612)Sam Gross2024-12-05
| | | | | | | | | | | | 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.
* gh-114940: Add _Py_FOR_EACH_TSTATE_UNLOCKED(), and Friends (gh-127077)Eric Snow2024-11-21
| | | This is a precursor to the actual fix for gh-114940, where we will change these macros to use the new lock. This change is almost entirely mechanical; the exceptions are the loops in codeobject.c and ceval.c, which now hold the "head" lock. Note that almost all of the uses of _Py_FOR_EACH_TSTATE_UNLOCKED() here will change to _Py_FOR_EACH_TSTATE_BEGIN() once we add the new per-interpreter lock.
* gh-127020: Make `PyCode_GetCode` thread-safe for free threading (#127043)Sam Gross2024-11-21
| | | | Some fields in PyCodeObject are lazily initialized. Use atomics and critical sections to make their initializations and accesses thread-safe.
* gh-126298: Don't deduplicate slice constants based on equality (#126398)Michael Droettboom2024-11-07
| | | | | | | | | | | | | | | * gh-126298: Don't deduplicated slice constants based on equality * NULL check for PySlice_New * Fix refcounting * Fix refcounting some more * Fix refcounting * Make tests more complete * Fix tests
* gh-115999: Implement thread-local bytecode and enable specialization for ↵mpage2024-11-04
| | | | | | | | | `BINARY_OP` (#123926) Each thread specializes a thread-local copy of the bytecode, created on the first RESUME, in free-threaded builds. All copies of the bytecode for a code object are stored in the co_tlbc array on the code object. Threads reserve a globally unique index identifying its copy of the bytecode in all co_tlbc arrays at thread creation and release the index at thread destruction. The first entry in every co_tlbc array always points to the "main" copy of the bytecode that is stored at the end of the code object. This ensures that no bytecode is copied for programs that do not use threads. Thread-local bytecode can be disabled at runtime by providing either -X tlbc=0 or PYTHON_TLBC=0. Disabling thread-local bytecode also disables specialization. Concurrent modifications to the bytecode made by the specializing interpreter and instrumentation use atomics, with specialization taking care not to overwrite an instruction that was instrumented concurrently.
* gh-125900: Clean-up logic around immortalization in free-threading (#125901)Sam Gross2024-10-24
| | | | | | | | | * Remove `@suppress_immortalization` decorator * Make suppression flag per-thread instead of per-interpreter * Suppress immortalization in `eval()` to avoid refleaks in three tests (test_datetime.test_roundtrip, test_logging.test_config8_ok, and test_random.test_after_fork). * frozenset() is constant, but not a singleton. When run multiple times, the test could fail due to constant interning.
* gh-124218: Use per-thread refcounts for code objects (#125216)Sam Gross2024-10-15
| | | | | | | Use per-thread refcounting for the reference from function objects to their corresponding code object. This can be a source of contention when frequently creating nested functions. Deferred refcounting alone isn't a great fit here because these references are on the heap and may be modified by other libraries.
* gh-111178: Fix function signatures in codeobject.c (#125180)Victor Stinner2024-10-09
|
* gh-125063: Emit slices as constants in the bytecode compiler (#125064)Michael Droettboom2024-10-08
| | | | | | | | | | | | | | | | | | | | | | | * Make slices marshallable * Emit slices as constants * Update Python/marshal.c Co-authored-by: Peter Bierma <zintensitydev@gmail.com> * Refactor codegen_slice into two functions so it always has the same net effect * Fix for free-threaded builds * Simplify marshal loading of slices * Only return SUCCESS/ERROR from codegen_slice --------- Co-authored-by: Mark Shannon <mark@hotpy.org> Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
* gh-122854: Add Py_HashBuffer() function (#122855)Victor Stinner2024-08-30
|
* GH-122390: Replace `_Py_GetbaseOpcode` with `_Py_GetBaseCodeUnit` (GH-122942)Mark Shannon2024-08-13
|
* Replace PyObject_Del with PyObject_Free (#122453)Victor Stinner2024-08-01
| | | | PyObject_Del() is just a alias to PyObject_Free() kept for backward compatibility. Use directly PyObject_Free() instead.
* gh-121863: Immortalize names in code objects to avoid crash (GH-121903)Petr Viktorin2024-07-17
|
* Fix typos in comments (#120821)Xie Yanbo2024-06-24
|
* Fixes loop variables to be the same types as their limit (GH-120958)Steve Dower2024-06-24
|
* gh-113993: Allow interned strings to be mortal, and fix related issues ↵Petr Viktorin2024-06-21
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | (GH-120520) * 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>
* gh-117657: Fix race involving immortalizing objects (#119927)Sam Gross2024-06-03
| | | | | | | | | 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.
* gh-119180: Add LOAD_COMMON_CONSTANT opcode (#119321)Jelle Zijlstra2024-05-22
| | | | | | | | | | The PEP 649 implementation will require a way to load NotImplementedError from the bytecode. @markshannon suggested implementing this by converting LOAD_ASSERTION_ERROR into a more general mechanism for loading constants. This PR adds this new opcode. I will work on the rest of the implementation of the PEP separately. Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
* gh-111389: Add PyHASH_MULTIPLIER constant (#119214)Victor Stinner2024-05-21
|
* gh-118527: Intern code consts in free-threaded build (#118667)Sam Gross2024-05-06
| | | | | | 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-06
| | | | 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-03
| | | | | | 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-03
| | | | 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-01
|
* gh-118335: Configure Tier 2 interpreter at build time (#118339)Guido van Rossum2024-04-30
| | | | | | | | | | | | | | | | | | | | | | 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-16
| | | | | | 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-12
|
* gh-108716:: Remove _PyStaticCode_Init/Fini (#117141)Guido van Rossum2024-03-22
| | | More deepfreeze cleanup.
* gh-117045: Add code object to function version cache (#117028)Guido van Rossum2024-03-21
| | | | | | | | | | | | | | | | | | | | | | | | | | 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-18
| | | | | 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-12
|
* gh-110543: Fix CodeType.replace in presence of comprehensions (#110586)Jelle Zijlstra2023-11-08
|