847 Commits
Author SHA1 Message Date
ae86660de3 [3.15] gh-161229: Accept None for globals and locals in __lazy_import__() (GH-161231) (#161315)
gh-161229: Accept `None` for `globals` and `locals` in `__lazy_import__()` (GH-161231)
(cherry picked from commit 1941319efb)

Co-authored-by: Lazizbek Ergashev <[email protected]>
2026-11-04 14:28:37 +00:00
c391d1a764 [3.15] gh-150868: Fix sum() deallocating a float subclass as an exact float (GH-151063) (#161048)
gh-150868: Fix sum() deallocating a float subclass as an exact float (GH-151063)

* gh-151060: Fix sum() deallocating a float subclass as an exact float

The complex-total fast path in builtin_sum_impl() guarded with
PyFloat_Check() (which accepts subclasses) but freed the item with
_Py_DECREF_SPECIALIZED(item, _PyFloat_ExactDealloc), which assumes an
exact float. A float subclass instance reaching refcount 0 in that loop
aborts on an assertion in debug builds, and in release builds skips the
subclass tp_dealloc (type refcount leak, __del__ never runs, subclass
object pushed onto the float freelist).

Use PyFloat_CheckExact() instead, matching the float-total branch, so
subclass instances fall through to the generic PyNumber_Add() path.



* gh-150868: Also use exact-type checks for ints in the fast sum() loops

Per review: use PyLong_CheckExact with the specialized exact-int
deallocator in the float and complex fast-sum loops, so int subclasses
take the generic path.



---------
(cherry picked from commit 47c249adf3)

Co-authored-by: Pieter Eendebak <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
2026-11-01 21:27:29 +01:00
462f62819b [3.15] gh-152132: Test Py_CompileString() in test_capi.test_run (GH-153470) (#153472)
gh-152132: Test Py_CompileString() in test_capi.test_run (GH-153470)

PyRun functions now raises ValueError if the start argument is
invalid.

* Add Modules/_testlimitedcapi/run.c.
* Rename _Py_CompileStringObjectWithModule() to
  _Py_CompileStringObject().
(cherry picked from commit d83d26724b)

Co-authored-by: Victor Stinner <[email protected]>
2026-07-10 02:07:38 +00:00
8fe5897853 [3.15] gh-151510: Fix __lazy_import__ without frame (GH-151511) (#151610)
gh-151510: Fix __lazy_import__ without frame (GH-151511)
(cherry picked from commit eff805b7a7)

Co-authored-by: AN Long <[email protected]>
2026-06-17 18:40:33 +00:00
ef89cf56cc [3.15] gh-150285: Fix too long docstrings in builtins (GH-150293) (GH-150336)
(cherry picked from commit e1e06be119)

Co-authored-by: Serhiy Storchaka <[email protected]>
2026-05-24 16:24:57 +03:00
29a92abb60 gh-148829: Implement PEP 661 (#148831)
Co-authored-by: Victorien <[email protected]>
Co-authored-by: Pieter Eendebak <[email protected]>
Co-authored-by: Hugo van Kemenade <[email protected]>
2026-04-27 19:28:30 -07:00
Gregory P. SmithandGitHub 665b7dfcfa Improve hash() builtin docstring with caveats. (GH-125229)
Improve  `hash()` builtin docstring with caveats.

Mention its return type and that the value can be expected to change between
processes (hash randomization).

Why? The `hash` builtin gets reached for and used by a lot of people whether it
is the right tool or not. IDEs surface docstrings and people use pydoc and
`help(hash)`.
2026-04-24 16:36:46 +00:00
sunmy2019andGitHub dcb260eff2 gh-146615: Fix format specifiers in Python/ directory (GH-146619) 2026-03-31 10:59:17 +03:00
Sergey MiryanovandGitHub d19de375a2 GH-145247: Use _PyTuple_FromPair in Parser and Python (#145842)
Use _PyTuple_FromPair in Parser and Python
2026-03-11 21:08:18 +00:00
Victor StinnerandGitHub c0ecf211b2 gh-145055: Accept frozendict for globals in exec() and eval() (#145072) 2026-03-05 12:35:43 +01:00
Victor StinnerandGitHub 2be2dd5fc2 gh-145076: Check globals type in __lazy_import__() (#145086) 2026-02-21 22:06:59 +01:00
696cdfc0a2 gh-141510, PEP 814: Add built-in frozendict type (#144757)
Add TYPE_FROZENDICT to the marshal module.

Add C API functions:

* PyAnyDict_Check()
* PyAnyDict_CheckExact()
* PyFrozenDict_Check()
* PyFrozenDict_CheckExact()
* PyFrozenDict_New()

Add PyFrozenDict_Type C type.

Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: Adam Johnson <[email protected]>
Co-authored-by: Benedikt Johannes <[email protected]>
2026-02-17 10:54:41 +01:00
46d5106cfa gh-142349: Implement PEP 810 - Explicit lazy imports (#142351)
Co-authored-by: T. Wouters <[email protected] >
Co-authored-by: Brittany Reynoso <[email protected]>
Co-authored-by: Dino Viehland <[email protected]>
2026-02-12 00:15:33 +00:00
Serhiy StorchakaandGitHub d8e6bdc0d0 gh-135801: Add the module parameter to compile() etc (GH-139652)
Many functions related to compiling or parsing Python code, such as
compile(), ast.parse(), symtable.symtable(),
and importlib.abc.InspectLoader.source_to_code() now allow to pass
the module name used when filtering syntax warnings.
2025-11-13 13:21:32 +02:00
Mikhail EfimovandGitHub be5af997f3 gh-140517: fix leak in map_next in strict mode (#140543) 2025-10-24 21:29:16 +05:30
Victor StinnerandGitHub 166cdaa6fb gh-111489: Remove _PyTuple_FromArray() alias (#139973)
Replace _PyTuple_FromArray() with PyTuple_FromArray().
Remove pycore_tuple.h includes.
2025-10-11 22:58:14 +02:00
Bénédikt TranandGitHub b04a57deef gh-139748: fix leaks in AC error paths when using unicode FS-based converters (#139765) 2025-10-08 20:52:44 +05:30
Serhiy StorchakaandGitHub b8c90e72b3 gh-137609: Change names of some positional-only parameters in builtins (GH-137611)
This is for a pair with GH-137610.
2025-08-25 15:36:06 +03:00
Adam TurnerandGitHub 918e3ba6c0 GH-137623: Use an AC decorator for docstring line length enforcement (#137690) 2025-08-18 18:29:00 +01:00
Serhiy StorchakaandGitHub 35759fe2fa gh-137668: Document that ord() supports also bytes and bytearray (GH-137669) 2025-08-13 12:12:16 +03:00
Eric SnowandGitHub a450a0ddec gh-135443: Sometimes Fall Back to __main__.__dict__ For Globals (gh-135491)
For several builtin functions, we now fall back to __main__.__dict__ for the globals
when there is no current frame and _PyInterpreterState_IsRunningMain() returns
true.  This allows those functions to be run with Interpreter.call().

The affected builtins:

* exec()
* eval()
* globals()
* locals()
* vars()
* dir()

We take a similar approach with "stateless" functions, which don't use any
global variables.
2025-06-16 17:34:19 -06:00
Serhiy StorchakaandGitHub bac3fcba5b gh-108512: Add and use new replacements for PySys_GetObject() (GH-111035)
Add functions PySys_GetAttr(), PySys_GetAttrString(),
PySys_GetOptionalAttr() and PySys_GetOptionalAttrString().
2025-05-28 20:11:09 +03:00
Yan YanchiiandGitHub 0a1fedb70b gh-126835: Rename ast_opt.c to ast_preprocess.c and related stuff after moving const folding to the peephole optimizier (#131830) 2025-05-04 21:07:35 +03:00
Mark ShannonandGitHub 44e4c479fb GH-124715: Move trashcan mechanism into Py_Dealloc (GH-132280) 2025-04-30 11:37:53 +01:00
954b2cf031 gh-130070: Fix exec(<string>, closure=<non-None>) unexpected path (#130071)
Fixed an assertion error (so, it could be reproduced only in builds with assertions enabled)
for `exec` when the `source` argument is a string and the `closure` argument is not `None`.

Co-authored-by: sobolevn <[email protected]>
2025-04-17 10:14:55 +03:00
8614f86b71 gh-131525: Cache the result of tuple_hash (#131529)
* gh-131525: Cache the result of tuple_hash

* Fix debug builds

* Add blurb

* Fix formatting

* Pre-compute empty tuple singleton

* Mostly set the cache within tuple_alloc

* Fixes for TSAN

* Pre-compute empty tuple singleton

* Fix for 32-bit platforms

* Assert that op != NULL in _PyTuple_RESET_HASH_CACHE

* Use FT_ATOMIC_STORE_SSIZE_RELAXED macro

* Update Include/internal/pycore_tuple.h

Co-authored-by: Bénédikt Tran <[email protected]>

* Fix alignment

* atomic load

* Update Objects/tupleobject.c

Co-authored-by: Chris Eibl <[email protected]>

---------

Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: Chris Eibl <[email protected]>
2025-03-27 09:57:06 -04:00
Sam GrossandGitHub 67fbfb42bd gh-131586: Avoid refcount contention in some "special" calls (#131588)
In the free threaded build, the `_PyObject_LookupSpecial()` call can lead to
reference count contention on the returned function object becuase it
doesn't use stackrefs. Refactor some of the callers to use
`_PyObject_MaybeCallSpecialNoArgs`, which uses stackrefs internally.

This fixes the scaling bottleneck in the "lookup_special" microbenchmark
in `ftscalingbench.py`. However, the are still some uses of
`_PyObject_LookupSpecial()` that need to be addressed in future PRs.
2025-03-26 14:38:47 -04:00
929afd1d6e gh-131670: Fix crash in anext() when __anext__ is sync and raises (#131682)
Co-authored-by: Bénédikt Tran <[email protected]>
2025-03-24 19:00:48 +00:00
Victor StinnerandGitHub 20c5f969dd gh-131238: Remove more includes from pycore_interp.h (#131480) 2025-03-19 23:01:32 +01:00
Victor StinnerandGitHub 4b54031323 gh-131238: Remove pycore_runtime.h from pycore_pystate.h (#131356)
* Remove includes from pycore_pystate.h:

  * pycore_runtime_structs.h
  * pycore_runtime.h
  * pycore_tstate.h
  * pycore_interp.h

* Reorganize internal headers. Move _gc_thread_state from
  pycore_interp_structs.h to pycore_tstate.h.
* Add 3 new header files to PCbuild/pythoncore.vcxproj.
2025-03-19 17:33:24 +01:00
Irit KatrielandGitHub ffc2f1dd1c gh-130080: implement PEP 765 (#130087) 2025-03-17 20:48:54 +00:00
Mark ShannonandGitHub a45f25361d GH-131238: More refactoring of core header files (GH-131351)
Adds new pycore_stats.h header file to help break dependencies involving the pycore_code.h header.
2025-03-17 14:41:05 +00:00
Victor StinnerandGitHub 9d759b63d8 gh-111178: Change Argument Clinic signature for METH_O (#130682)
Use "PyObject*" for METH_O functions to fix an undefined behavior.
2025-03-11 16:33:36 +01:00
Serhiy StorchakaandGitHub 0ef4ffeefd gh-130163: Fix crashes related to PySys_GetObject() (GH-130503)
The use of PySys_GetObject() and _PySys_GetAttr(), which return a borrowed
reference, has been replaced by using one of the following functions, which
return a strong reference and distinguish a missing attribute from an error:
_PySys_GetOptionalAttr(), _PySys_GetOptionalAttrString(),
_PySys_GetRequiredAttr(), and _PySys_GetRequiredAttrString().
2025-02-25 23:04:27 +02:00
Irit KatrielandGitHub c9b1bf302c gh-130139: always check ast node type in ast.parse() with ast input (#130140) 2025-02-16 13:32:39 +00:00
Bénédikt TranandGitHub 28ffdc5702 gh-111178: fix UBSan failures in Python/bltinmodule.c (GH-128235)
* fix UBSan failures for `filterobject`
* fix UBSan failures for `mapobject`
* fix UBSan failures for `zipobject`
2025-01-10 11:51:24 +01:00
Neil SchemenauerandGitHub fc5a0dc224 gh-127271: Replace use of PyCell_GET/SET (gh-127272)
* Replace uses of `PyCell_GET` and `PyCell_SET`.  These macros are not
  safe to use in the free-threaded build.  Use `PyCell_GetRef()` and
  `PyCell_SetTakeRef()` instead. 

* Since `PyCell_GetRef()` returns a strong rather than borrowed ref, some
  code restructuring was required, e.g. `frame_get_var()` returns a strong
  ref now.

* Add critical sections to `PyCell_GET` and `PyCell_SET`.

* Move critical_section.h earlier in the Python.h file.

* Add `PyCell_GET` to the free-threading howto table of APIs that return
  borrowed refs.

* Add additional unit tests for free-threading.
2024-12-03 10:33:06 -08:00
Sergey B KirpichevandGitHub 987311d42e gh-69639: Add mixed-mode rules for complex arithmetic (C-like) (GH-124829)
"Generally, mixed-mode arithmetic combining real and complex variables should
be performed directly, not by first coercing the real to complex, lest the sign
of zero be rendered uninformative; the same goes for combinations of pure
imaginary quantities with complex variables." (c) Kahan, W: Branch cuts for
complex elementary functions.

This patch implements mixed-mode arithmetic rules, combining real and
complex variables as specified by C standards since C99 (in particular,
there is no special version for the true division with real lhs
operand).  Most C compilers implementing C99+ Annex G have only these
special rules (without support for imaginary type, which is going to be
deprecated in C2y).
2024-11-26 17:57:39 +02:00
Yuxuan ZhangandGitHub 8cc6e5c875 gh-126757: fix minor typo (GH-126758) 2024-11-12 13:23:40 -08:00
Serhiy StorchakaandGitHub 06a8b0bb5e gh-122943: Remove the object converter for var-positional parameter (GH-126560) 2024-11-08 05:41:54 +00:00
3032fcd90e gh-119793: Add optional length-checking to map() (GH-120471)
Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: Pieter Eendebak <[email protected]>
Co-authored-by: Erlend E. Aasland <[email protected]>
Co-authored-by: Raymond Hettinger <[email protected]>
2024-11-04 15:00:19 +01:00
Sam GrossandGitHub 332356b880 gh-125900: Clean-up logic around immortalization in free-threading (#125901)
* 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.
2024-10-24 18:09:59 -04:00
Pieter EendebakandGitHub 7e38e6745d gh-123271: Make builtin zip method safe under free-threading (#123272)
The `zip_next` function uses a common optimization technique for methods
that generate tuples. The iterator maintains an internal reference to
the returned tuple. When the method is called again, it checks if the
internal tuple's reference count is 1. If so, the tuple can be reused.
However, this approach is not safe under the free-threading build:
after checking the reference count, another thread may perform the same
check and also reuse the tuple. This can result in a double decref on
the items of the replaced tuple and a double incref (memory leak) on
the items of the tuple being set.

This adds a function, `_PyObject_IsUniquelyReferenced` that
encapsulates the stricter logic necessary for the free-threaded build:
the internal tuple must be owned by the current thread, have a local
refcount of one, and a shared refcount of zero.
2024-08-27 15:22:43 -04:00
89fa05fdce gh-122234: Add DECREFs to error paths (#122406)
Co-Authored-By: Kirill Podoprigora <[email protected]>
2024-07-29 16:10:25 +00:00
Sergey B KirpichevandGitHub 169e7138ab gh-122234: fix accuracy issues for sum() (#122236)
* Use compensated summation for complex sums with floating-point items.
  This amends #121176.

* sum() specializations for floats and complexes now use
  PyLong_AsDouble() instead of PyLong_AsLongAndOverflow() and
  compensated summation as well.
2024-07-29 05:56:40 +02:00
Serhiy StorchakaandGitHub 18015451d0 gh-121153: Fix some errors with use of _PyLong_CompactValue() (GH-121154)
* The result has type Py_ssize_t, not intptr_t.
* Type cast between unsigned and signdet integer types should be explicit.
* Downcasting should be explicit.
* Fix integer overflow check in sum().
2024-07-13 13:40:44 +03:00
Sergey B KirpichevandGitHub d4faa7bd32 gh-121149: improve accuracy of builtin sum() for complex inputs (gh-121176) 2024-07-05 10:01:05 -05:00
Adam WilliamsonandGitHub d4039d3f6f gh-120526: Correct signature of map() builtin (GH-120528)
map() requires at least one iterable arg.

Signed-off-by: Adam Williamson <[email protected]>
2024-06-15 05:33:09 +00:00
Sam GrossandGitHub 47fb4327b5 gh-117657: Fix race involving immortalizing objects (#119927)
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.
2024-06-03 20:58:41 +00:00
Sergey B KirpichevandGitHub cd11ff12ac gh-119613: Use C99+ functions instead of Py_IS_NAN/INFINITY/FINITE (#119619) 2024-05-29 09:51:19 +02:00