[3.14] gh-135376: Fix and improve test_random (GH-135377) (GH-135680)
* Remove duplicated code. Tests for Random and SystemRandom now share
the code.
* Move implementation agnostic tests that was only run for SystemRandom,
so they are now run for Random too.
* Add tests for __index__() support.
* Add tests for randint().
(cherry picked from commit c55512311b7cb8b7c27c19f56cd8f872be29aedc)
[3.14] gh-135335: flush stdout/stderr in forkserver after preloading modules (GH-135338) (#135670)
gh-135335: flush stdout/stderr in forkserver after preloading modules (GH-135338)
If a preloaded module writes to stdout or stderr, and the stream is buffered,
child processes will inherit the buffered data after forking. Attempt to
prevent this by flushing the streams after preload.
(cherry picked from commit 9877d191f441741fc27ae5e7a6dd7ab6d4bcc6b7)
[3.14] gh-119180: Only fetch globals and locals if necessary in `annotationlib.get_annotations()` (GH-135644) (#135654)
gh-119180: Only fetch globals and locals if necessary in `annotationlib.get_annotations()` (GH-135644)
(cherry picked from commit 504ae606e192a88805996a7e934f38bc5cb7f28a)
[3.14] gh-127146: Emscripten: Fix pathlib glob_dotdot test (GH-135624) (#135653)
The Emscripten path resolver uses the same mechanism for resolving `..`
at a file system root as for resolving symlinks. This is because
roots don't store their mountpoints. If the parent of a node is itself,
it is a root but it might be a mountpoint in some other file system.
Emscripten currently `gethostbyname_r()` returns an incorrect
IP address for `localhost`. Will be resolved by upstream PR:
https://github.com/emscripten-core/emscripten/pull/24593
(cherry picked from commit 2a49c54ab2c5cf0ee96a8e577971c1525a966940)
As noted in the new tests, there are a few situations we must carefully accommodate
for functions that get pickled during interp.call(). We do so by running the script
from the main interpreter's __main__ module in a hidden module in the other
interpreter. That hidden module is used as the function __globals__.
(cherry picked from commit 269e19e0a7, AKA gh-135595)
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
[3.14] gh-134632: Fix `build-details.json` to use `INCLUDEPY` path (GH-134633) (#135605)
gh-134632: Fix `build-details.json` to use `INCLUDEPY` path (GH-134633)
* gh-134632: Fix `build-details.json` to use `INCLUDEPY` path
Fix ``build-details.json`` generation to use ``INCLUDEPY``, in order to
reference the ``pythonX.Y`` subdirectory of the include directory, as
required in :pep:`739`, instead of the top-level include directory.
* test_build_details: Add tests for the c_api section
[3.14] gh-135443: Sometimes Fall Back to __main__.__dict__ For Globals (gh-135593)
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().
[3.14] gh-119132: Update 'Using Python on macOS' documentation. (GH-135591) (#135594)
Remove `experimental` qualification for free-threading in the document text. Note that images included in the document will be updated later in the release cycle.
(cherry picked from commit 7c685894cd9c2c669f09fad31365e3ad6c0c3861)
[3.14] gh-116738: Make _heapq module thread-safe (GH-135036) (gh-135309)
Use critical sections to make heapq methods that update the heap thread-safe when the GIL is disabled.
(cherry picked from commit a58026a5e3da9ca2d09ef51aa90fe217f9a975ec)
Use `ma_used` instead of `ma_keys->dk_nentries` for modification check
so that we only check if the dictionary is modified, not if new keys are
added to a different dictionary that shared the same keys object.
(cherry picked from commit d8994b0a77cc9821772d05db00a6ab23382fa17d)
Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Donghee Na <donghee.na@python.org> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Co-authored-by: Peter Bierma <zintensitydev@gmail.com> Co-authored-by: T. Wouters <thomas@python.org>
[3.14] gh-67022: Document bytes/str inconsistency in email.header.decode_header() and suggest email.headerregistry.HeaderRegistry as a sane alternative (GH-92900) (#135548)
gh-67022: Document bytes/str inconsistency in email.header.decode_header() and suggest email.headerregistry.HeaderRegistry as a sane alternative (GH-92900)
* gh-67022: Document bytes/str inconsistency in email.header.decode_header()
This function's possible return types have been surprising and error-prone
for the entirety of its Python 3.x history. It can return either:
1. `typing.List[typing.Tuple[bytes, typing.Optional[str]]]` of length >1
2. or `typing.List[typing.Tuple[str, None]]`, of length exactly 1
This means that any user of this function must be prepared to accept either
`bytes` or `str` for the first member of the 2-tuples it returns, which is a
very surprising behavior in Python 3.x, particularly given that the second
member of the tuple is supposed to represent the charset/encoding of the
first member.
This patch documents the behavior of this function, and adds test cases
to demonstrate it.
As discussed in bpo-22833, this cannot be changed in a backwards-compatible
way, and some users of this function depend precisely on the existing
behavior.
Add warnings about obsolescence of 'email.header.decode_header' and 'email.header.make_header' functions.
Recommend use of `email.headerregistry.HeaderRegistry` instead, as suggested
in https://github.com/python/cpython/pull/92900#discussion_r1112472177
(cherry picked from commit 60181f4ed0e48ff35dc296da6b51473bfc553d16)
Co-authored-by: Jacob Austin Lincoln <99031153+lincolnj1@users.noreply.github.com> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
[3.14] gh-135462: Fix quadratic complexity in processing special input in HTMLParser (GH-135464) (GH-135481)
End-of-file errors are now handled according to the HTML5 specs --
comments and declarations are automatically closed, tags are ignored.
(cherry picked from commit 6eb6c5dbfb528bd07d77b60fd71fd05d81d45c41)
[3.14] gh-132969: Fix error/hang when shutdown(wait=False) and task exited abnormally (GH-133222) (GH-135344)
gh-132969: Fix error/hang when shutdown(wait=False) and task exited abnormally (GH-133222)
When shutdown is called with wait=False, the executor thread keeps running
even after the ProcessPoolExecutor's state is reset. The executor then tries
to replenish the worker processes pool resulting in an error and a potential hang
when it comes across a worker that has died. Fixed the issue by having
_adjust_process_count() return without doing anything if the ProcessPoolExecutor's
state has been reset.
Added unit tests to validate two scenarios:
max_workers < num_tasks (exception)
max_workers > num_tasks (exception + hang)
(cherry picked from commit 598aa7cc98bc1b39f10ec41decddd8dd88799fe1)
[3.14] gh-128627: Fix iPad detection in wasm-gc (GH-135388) (#135419)
On some iPad versions, Safari reports as "macOS". Modifies the GC trampoline detection
to add a feature-based check to detect this case.
(cherry picked from commit d4471297586335d8c24db8b2c030d32c94570344)
[3.14] gh-133264: Correct documentation of how Py_Main and Py_RunMain handle SystemExit (GH-135337) (#135394)
gh-133264: Correct documentation of how Py_Main and Py_RunMain handle SystemExit (GH-135337)
(cherry picked from commit b706ff003c536c5bca24dfdd3a8917bffcfa3df1)
[3.14] gh-91048: Reorder result tuple of parse_code_object (GH-134898) (#134956)
* gh-91048: Reorder result tuple of parse_code_object (GH-134898)
Reorder result tuple of parse_code_object
The standard followed by APIs like pstat.Stats is to take a file, line,
function triplet. The parse_code_object function (and callers exposing
this in Python like RemoteUnwinder.get_stack_trace) return function,
file, line triplets which requires the caller to reorder these when
using it in classes like pstat.Stats.
(cherry picked from commit 8e8786f8986353e20c1c4406c34409a6139fa073)
Co-authored-by: László Kiss Kollár <kiss.kollar.laszlo@gmail.com>
* Reorder asyncio
---------
Co-authored-by: László Kiss Kollár <kiss.kollar.laszlo@gmail.com> Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
[3.14] gh-127833: Reword and expand the Notation section (GH-134443) (GH-135301)
Prepare the docs for using the notation used in the `python.gram`
file. If we want to sync the two, the meta-syntax should be the same.
Link the Full Grammar docs here; keep only a few extras.
Also, remove the distinction between lexical and syntactic rules,
except for whitespace handling.
With f- and t-strings, the line between the two is blurry.
(cherry picked from commit 28d91d06f13ceda3df0cefacec497c370eff4802)
Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Blaise Pabon <blaise@gmail.com> Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com> Co-authored-by: Colin Marquardt <cmarqu42@gmail.com>
Signed-off-by: Edward Z. Yang <ezyang@fb.com> Signed-off-by: Edward Z. Yang <ezyang@meta.com> Co-authored-by: Edward Z. Yang <ezyang@mit.edu> Co-authored-by: Victor Stinner <vstinner@python.org>
[3.14] GH-135287: clang-cl PGO builds on Windows fail with `could not open '/GENPROFILE'` (GH-135289)
GH-135287: clang-cl PGO builds on Windows fail with `could not open '/GENPROFILE'` (GH-135289)
(cherry picked from commit 0045100ccbc3919e8990fa59bc413fe38d21b075)
Co-authored-by: Chris Eibl <138194463+chris-eibl@users.noreply.github.com>
mpage [Thu, 5 Jun 2025 20:06:51 +0000 (13:06 -0700)]
[3.14] gh-134889: Fix handling of a few opcodes when optimizing `LOAD_FAST` (#134958) (#135187)
We were incorrectly handling a few opcodes that leave their operands on the stack. Treat all of these conservatively; assume that they always leave operands on the stack.