Commit graph

35360 commits

Author SHA1 Message Date
Shamil
235fa7244a
gh-142831: Fix use-after-free in json encoder during re-entrant mutation (gh-142851)
Hold strong references to borrowed items unconditionally (not only in                                                                        
free-threading builds) in _encoder_iterate_mapping_lock_held and                                                                             
_encoder_iterate_fast_seq_lock_held.  User callbacks invoked during                                                                          
encoding can mutate or clear the underlying container, invalidating                                                                          
borrowed references.                                                                                                                         
                                                                                                                                               
The dict iteration path was already fixed by gh-145244.

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
2026-04-12 00:14:50 +00:00
Artem Yarulin
cef334fd4c
tests: use errno.EBADF instead of hardcoded number in _close_file() (GH-148345)
test_interpreters: use errno.EBADF instead of hardcoded number in _close_file()

Replace the hardcoded `9` check in `Lib/test/test_interpreters/utils.py` with `errno.EBADF`.

Using `errno.EBADF` makes the helper portable across platforms with different errno numbering while preserving the intended behavior.
2026-04-11 17:01:18 -07:00
Ramin Farajpour Cami
8a466fa3d9
gh-145244: Fix use-after-free on borrowed dict key in json encoder (GH-145245)
In encoder_encode_key_value(), key is a borrowed reference from
PyDict_Next(). If the default callback mutates or clears the dict,
key becomes a dangling pointer. The error path then calls
_PyErr_FormatNote("%R", key) on freed memory.

Fix by holding strong references to key and value unconditionally
during encoding, not just in the free-threading build.

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
2026-04-11 22:26:36 +00:00
Jason R. Coombs
daa2578dc0
gh-127012: Traversable.read_text now allows/solicits an errors parameter. (#148401)
Applies changes from importlib_resources 6.5.2.
2026-04-11 22:25:20 +00:00
Ramin Farajpour Cami
20994b1809
gh-145105: Fix crash in csv.reader with re-entrant iterator (GH-145106)
When a custom iterator calls next() on the same csv.reader from
within __next__, the inner iteration sets self->fields to NULL.
The outer iteration then crashes in parse_save_field() by passing
NULL to PyList_Append.

Add a guard after PyIter_Next() to detect that fields was set to
NULL by a re-entrant call, and raise csv.Error instead of crashing.
2026-04-11 15:18:02 -07:00
Ramin Farajpour Cami
c29d75610b
gh-145200: Fix EVP_MAC_CTX leak in hashlib HMAC on init failure (GH-145201)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2026-04-11 15:10:43 -07:00
Gregory P. Smith
64afa947f4
gh-146302: make Py_IsInitialized() thread-safe and reflect true init completion (GH-146303)
## Summary

- Move the `runtime->initialized = 1` store from before `site.py` import to the end of `init_interp_main()`, so `Py_IsInitialized()` only returns true after initialization has fully completed
- Access `initialized` and `core_initialized` through new inline accessors using acquire/release atomics, to also protect from data race undefined behavior
- `PySys_AddAuditHook()` now uses the accessor, so with the flag move it correctly skips audit hook invocation during all init phases (matching the documented "after runtime initialization" behavior) ... We could argue that running these earlier would be good even if the intent was never explicitly expressed, but that'd be its own issue.

## Motivation

`Py_IsInitialized()` returned 1 while `Py_InitializeEx()` was still running — specifically, before `site.py` had been imported. See https://github.com/PyO3/pyo3/issues/5900 where a second thread could acquire the GIL and start executing Python with an incomplete `sys.path` because `site.py` hadn't finished.

The flag was also a plain `int` with no atomic operations, making concurrent reads a C-standard data race, though unlikely to manifest.

## Regression test:

The added test properly fails on `main` with `ERROR: Py_IsInitialized() was true during site import`.

---

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-11 21:54:23 +00:00
Wulian233
a059e85866
gh-131798: Add _IS_NONE to the JIT optimizer (GH-148369) 2026-04-11 23:02:46 +08:00
Pieter Eendebak
1c89817f51
gh-148276: Optimize object creation and method calls in the JIT by resolving __init__ at trace optimization time (GH-148277)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Ken Jin <kenjin4096@gmail.com>
2026-04-11 22:22:42 +08:00
Neko Asakura
9831dea3bf
gh-148211: decompose _POP_TWO/_POP_CALL(_ONE/_TWO) in JIT (GH-148377) 2026-04-11 20:46:56 +08:00
Neko Asakura
72006a71b2
gh-148211: decompose [_POP_TWO/_INSERT_2]_LOAD_CONST_INLINE_BORROW in JIT (GH-148357) 2026-04-11 18:27:51 +08:00
Kumar Aditya
8f17140fc1
gh-131798: split _CALL_BUILTIN_CLASS to smaller uops (#148094) 2026-04-10 17:28:20 +00:00
Seth Larson
05ed7ce7ae
gh-146211: Reject CR/LF in HTTP tunnel request headers (#146212)
Co-authored-by: Illia Volochii <illia.volochii@gmail.com>
2026-04-10 15:21:42 +00:00
Ken Jin
266247c9a6
gh-148171: Convert CALL_BUILTIN_FAST to leave inputs on the stack for refcount elimination in JIT (GH-148172) 2026-04-10 23:11:18 +08:00
Neko Asakura
aea0b91d65
gh-148211: decompose [_POP_CALL_X/_SHUFFLE_2]_LOAD_CONST_INLINE_BORROW in JIT (GH-148313) 2026-04-10 21:57:01 +08:00
Stefan Zetzsche
1a0edb1fa8
gh-145831: email.quoprimime: decode() leaves stray \r when eol='\r\n' (#145832)
decoded[:-1] only strips one character, leaving a stray \r when eol
is two characters. Fix: decoded[:-len(eol)].
2026-04-09 16:21:49 -04:00
Neko Asakura
0f49232664
gh-148211: decompose _INSERT_1_LOAD_CONST_INLINE(_BORROW) in JIT (GH-148283) 2026-04-10 00:45:39 +08:00
gaweng
d8c5658156
gh-148254: Use singular "sec" in timeit verbose output (#148290) 2026-04-09 17:37:19 +03:00
Sacul
38d3aef375
gh-134584 : Optimize and eliminate redundant ref-counting for MAKE_FUNCTION in the JIT (GH-144963) 2026-04-09 22:22:53 +08:00
Victor Stinner
b7e1d51e6b
gh-148241: Fix json serialization for str subclasses (#148249)
Fix json serialization: no longer call str(obj) on str subclasses.

Replace PyUnicodeWriter_WriteStr() with PyUnicodeWriter_WriteASCII()
and private _PyUnicodeWriter_WriteStr().
2026-04-09 13:50:44 +02:00
Kumar Aditya
458aca9237
gh-131798: fold super method lookups in JIT (#148231) 2026-04-09 13:25:01 +05:30
Pablo Galindo Salgado
efde4333bf
gh-148225: Validate profiling.sampling replay input (#148243) 2026-04-08 23:34:46 +00:00
Pablo Galindo Salgado
09968dd2a9
gh-148105: _pyrepl: switch console refresh to structured rendered screens (#146584) 2026-04-08 23:42:26 +01:00
nmartensen
442f83a5ea
gh-70039: smtplib: store the server name in ._host in .connect() (#115259)
Original patch by gigaplastik, extended with a few more tests.

Addresses gh-70039 and bpo-25852: failure of starttls if connect is called explicitly.
2026-04-08 17:46:25 -04:00
Kumar Aditya
6e081614eb
gh-148210: fix incorrect _BINARY_OP_SUBSCR_DICT JIT optimization (GH-148213) 2026-04-08 23:23:20 +08:00
Sacul
bb03c8bd02
gh-145866: Convert _CALL_METHOD_DESCRIPTOR_NOARGS to leave its inputs on the stack to be cleaned up by _POP_TOP (GH-148227) 2026-04-08 23:21:37 +08:00
Neko Asakura
d2fa4b2b13
gh-148211: decompose _POP_TOP_LOAD_CONST_INLINE(_BORROW) in JIT (GH-148230) 2026-04-08 23:20:31 +08:00
Shrey Naithani
461125aaa3
gh-37883: Safely skip test_resource file size tests when limits are strict (GH-145579) 2026-04-08 16:06:05 +02:00
Hugo van Kemenade
8687b9d731
gh-146609: Add colour to timeit CLI output (#146610)
Co-authored-by: Stan Ulbrych <stan@python.org>
2026-04-08 16:18:53 +03:00
Hugo van Kemenade
2c8f26cf5c
gh-146292: Add colour to http.server logs (GH-146293)
Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
2026-04-08 14:10:05 +02:00
Neko Asakura
756358524e
gh-148235: remove duplicate uops _LOAD_CONST_UNDER_INLINE(_BORROW) in JIT (GH-148236) 2026-04-08 16:22:59 +08:00
Petr Viktorin
8923ca418c
gh-145921: Add "_DuringGC" functions for tp_traverse (GH-145925)
There are newly documented restrictions on tp_traverse:

    The traversal function must not have any side effects.
    It must not modify the reference counts of any Python
    objects nor create or destroy any Python objects.

* Add several functions that are guaranteed side-effect-free,
  with a _DuringGC suffix.
* Use these in ctypes
* Consolidate tp_traverse docs in gcsupport.rst, moving unique
  content from typeobj.rst there

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
2026-04-08 09:15:11 +02:00
Gabriel Volles Marinho
0b20bff386
gh-146458: Fix REPL height and width tracking on resize (#146459)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
2026-04-07 23:09:11 +02:00
Ramin Farajpour Cami
957b2cca8e
gh-145846: Fix memory leak in _lsprof clearEntries() context chain (#145847)
clearEntries() only freed the top currentProfilerContext but did not
walk the previous linked list. When clear() is called during active
profiling with nested calls, all contexts except the top one were
leaked. Fix by iterating the entire linked list, matching the existing
freelistProfilerContext cleanup pattern.

Co-authored-by: Victor Stinner <vstinner@python.org>
2026-04-07 22:56:19 +02:00
Kumar Aditya
e371ce10cd
gh-95004: specialize access to enums and fix scaling on free-threading (#148184)
Co-authored-by: Ken Jin <kenjin4096@gmail.com>
2026-04-07 21:43:50 +05:30
Joshua Swanson
7e0a0be409
gh-146333: Fix quadratic regex backtracking in configparser option parsing (GH-146399)
Use negative lookahead in option regex to prevent backtracking, and to avoid changing logic outside the regexes (since people could use the regex directly).
2026-04-07 16:10:34 +02:00
Victor Stinner
feee573f36
gh-148014: Accept a function name in -X presite option (#148015) 2026-04-07 14:05:39 +00:00
grayjk
dfeb160bc3
gh-130273: Fix traceback color output with unicode characters (GH-142529)
Account for the display width of Unicode characters so that colors and underlining in traceback output is correct.

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Co-authored-by: Victor Stinner <vstinner@python.org>
2026-04-07 15:05:23 +02:00
Petr Viktorin
cf59bf7647
gh-146121: Clarify security model of pkgutil.getdata; revert checks (GH-148197)
This reverts commit bcdf231946,
and clarifies get_data's security model.


Co-authored-by: Stan Ulbrych <stan@python.org>
2026-04-07 11:39:50 +02:00
Chris Eibl
a4d9d6483f
GH-145762: Fix test_ci_fuzz_stdlib for source archive builds (#145778)
Co-authored-by: Stan Ulbrych <stan@ulbrych.org>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
2026-04-07 12:02:15 +03:00
Gregory P. Smith
5e9d90b615
gh-144503: Pass sys.argv to forkserver as real argv elements (GH-148194)
Avoid embedding the parent's sys.argv into the forkserver -c command
string via repr().  When sys.argv is large (e.g. thousands of file
paths from a pre-commit hook), the resulting single argument could
exceed the OS per-argument length limit (MAX_ARG_STRLEN on Linux,
typically 128 KiB), causing posix_spawn to fail and the parent to
observe a BrokenPipeError.

Instead, append the argv entries as separate command-line arguments
after -c; the forkserver child reads them back as sys.argv[1:].  This
cannot exceed any limit the parent itself did not already satisfy.

Regression introduced by gh-143706 / 298d5440eb.
2026-04-06 22:41:02 -07:00
Semyon Moroz
132abfd135
gh-112632: Add optional keyword-only argument expand to pprint (#136964)
Co-authored-by: stodoran <stefan.todoran@uipath.com>
Co-authored-by: StefanTodoran <stefan.alex4@gmail.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
2026-04-07 07:42:54 +03:00
Pablo Galindo Salgado
ca960b6f38
gh-148110: Resolve lazy import filter names for relative imports (#148111) 2026-04-06 22:29:02 +01:00
Fionn
a0c57a8d17
gh-137586: Open external osascript program with absolute path (GH-137584)
Open web browser with absolute path

On macOS, web browsers are opened via popen calling osascript. However,
if a user has a colliding osascript executable earlier in their PATH,
this may fail or cause unwanted behaviour.

Depending on one's environment or level of paranoia, this may be considered a security vulnerability.

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
2026-04-06 09:42:10 -07:00
Pablo Galindo Salgado
f8293faf37
gh-130472: Remove readline-only hacks from PyREPL completions (#148161)
PyREPL was still carrying over two readline-specific tricks from the
fancy completer: a synthetic CSI prefix to influence sorting and a fake
blank completion entry to suppress readline's prefix insertion. Those
workarounds are not appropriate in PyREPL because the reader already
owns completion ordering and menu rendering, so the fake entries leaked
into the UI as real terminal attributes and empty menu cells.

Sort completion candidates in ReadlineAlikeReader by their visible text
with stripcolor(), and let the fancy completer return only real matches.
That keeps colored completions stable without emitting bogus escape
sequences, removes the empty completion slot, and adds regression tests
for both the low-level completer output and the reader integration.
2026-04-06 14:57:25 +00:00
Pieter Eendebak
efda60e2ec
gh-100239: Propagate type info through _BINARY_OP_EXTEND in tier 2 (GH-148146) 2026-04-06 20:52:42 +08:00
Stan Ulbrych
1795fccfbc
gh-148157: Check for _PyPegen_add_type_comment_to_arg fail in _PyPegen_name_default_pair (#148158) 2026-04-06 12:56:36 +01:00
Serhiy Storchaka
d0e204fb1d
gh-148153: Do not use assert for parameter validation in base64 (GH-148154)
base64.b32encode() now always raises ValueError instead of
AssertionError for the value of map01 with invalid length.
2026-04-06 13:47:27 +03:00
Loïc Simon
b07becb573
gh-140870: PyREPL auto-complete module attributes in import statements (#140871)
Co-authored-by: Pablo Galindo Salgado <pablogsal@gmail.com>
2026-04-05 19:10:59 +00:00
Loïc Simon
c64baff1d8
gh-69605: Add math.integer to PyREPL module completer hardcoded list (#144811) 2026-04-05 19:31:19 +01:00