gh-143650: Fix importlib race condition on import failure (GH-143651)
Fix a race condition where a thread could receive a partially-initialized
module when another thread's import fails. The race occurs when:
1. Thread 1 starts importing, adds module to sys.modules
2. Thread 2 sees the module in sys.modules via the fast path
3. Thread 1's import fails, removes module from sys.modules
4. Thread 2 returns a stale module reference not in sys.modules
The fix adds verification after the "skip lock" optimization in both Python
and C code paths to check if the module is still in sys.modules. If the
module was removed (due to import failure), we retry the import so the
caller receives the actual exception from the import failure rather than
a stale module reference.
(cherry picked from commit ac8b5b6890)
Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
gh-144629: Add test for the PyFunction_GetAnnotations() function (GH-144630)
(cherry picked from commit cc81707e40)
Co-authored-by: Nybblista <170842536+nybblista@users.noreply.github.com>
gh-144652: Support Windows exit status in support get_signal_name() (GH-144653)
Format Windows exit status as hexadecimal.
(cherry picked from commit b121dc4347)
Co-authored-by: Victor Stinner <vstinner@python.org>
gh-134179: Use sys._clear_internal_caches() at test_cmd_line (GH-134180)
Use sys._clear_internal_caches() instead of deprecated sys._clear_type_cache() at test_cmd_line.
(cherry picked from commit dd2da42ea4)
Co-authored-by: alexey semenyuk <alexsemenyuk88@gmail.com>
Fix thread-safety issues when accessing frame attributes while another
thread is executing the frame:
- Add critical section to frame_repr() to prevent races when accessing
the frame's code object and line number
- Add _Py_NO_SANITIZE_THREAD to PyUnstable_InterpreterFrame_GetLasti()
to allow intentional racy reads of instr_ptr.
- Fix take_ownership() to not write to the original frame's f_executable
(cherry picked from commit 5bb3bbb9c6)
Co-authored-by: Sam Gross <colesbury@gmail.com>
gh-144330: Initialize classmethod and staticmethod in new
Initialize cm_callable and sm_callable to None in classmethod and
staticmethod constructor.
Co-authored-by: Aniket Singh Yadav <singhyadavaniket43@gmail.com>
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
gh-142555: Fix null pointer dereference in array.__setitem__ via re-entrant __index__ (GH-142713)
(cherry picked from commit 39f16a93ef)
Co-authored-by: AN Long <aisk@users.noreply.github.com>
Run a long living subprocess which handles multiple requests instead of
running a new subprocess for each request.
(cherry picked from commit 29acc08c8d)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Modifies handling of `.gz` files in Android app payloads, and ensures that
when the Android testbed streams logs, stream flushes aren't treated as
newlines. This improves the output of test suites that use "one dot per test"
progress indicators.
(cherry picked from commit cb1dc91dcb)
Co-authored-by: Malcolm Smith <smith@chaquo.com>
gh-144348: annotationlib: fix test that relies on set ordering (GH-144359)
(cherry picked from commit b6256014be)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
In `_Py_dict_lookup_threadsafe_stackref`, call `ensure_shared_on_read()` to
prevent a race between the lookup and concurrent dict resizes, which may free
the PyDictKeysObject (i.e., it ensures that the resize uses QSBR).
(cherry picked from commit e666a01ef4)
gh-126014: test_makefile_test_folders: Ignore basically-empty directories (GH-140466)
The code in test_makefile was attempting to ignore any
non-interesting files, but missed some corners:
1. There is never a *file* called `__pycache__`.
2. A directory containing only a `__pycache__` subdirectory should be
ignored.
3. A directory containing only hidden files should be ignored.
Simplify this all into a couple of filters that let us check for empty
lists.
(cherry picked from commit 17d447e993)
Co-authored-by: Stefano Rivera <stefano@rivera.za.net>
gh-144100: Fix crash for POINTER(str) used in ctypes argtypes (GH-144108)
(cherry picked from commit 8f459255eb)
Co-authored-by: VanshAgarwal24036 <148854295+VanshAgarwal24036@users.noreply.github.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
gh-144169: Fix three crashes in AST objects with non-str kwargs (GH-144178)
(cherry picked from commit 639c1ad4f1)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
It does not support an ASCII string.
Also add more tests.
(cherry picked from commit 25a10b60b0)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
[3.14] gh-144023: Prevent follow_symlinks from being allowed with an fd of 0 (GH-144022)
The check was (fd > 0), should be (fd >= 0).
(cherry picked from commit fa44efa0ef)
Co-authored-by: AZero13 <gfunni234@gmail.com>
gh-143874: Use self.message instead of raw print in `_exec_in_closure()` (GH-143875)
(cherry picked from commit e66597d6c8)
Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com>
gh-143774 - Improve IDLE Format Paragraph doc (GH-143775)
Add a reminder to not rewrap code line to the Menu => Format => Reformat Paragraph entry.
In Editing and Nagivagion, add a new 'Format block' subsection that defines 'paragraph'
to better match what is dependably handled as more or less expected.
In particular, specify equal indents and that the resulting indent equals original indent.
Also mention that selections are expanded to complete lines and how to modify max length.
(Also fix a couple case errors in cross references.)
(cherry picked from commit fa3abf5a51)
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Use more relevant terminology instead of "master"/"slave" widgets where possible.
(cherry picked from commit 813fc7a291)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
gh-143916: Reject control characters in wsgiref.headers.Headers (GH-143917)
* Add 'test.support' fixture for C0 control characters
* gh-143916: Reject control characters in wsgiref.headers.Headers
(cherry picked from commit f7fceed79c)
Co-authored-by: Seth Michael Larson <seth@python.org>
This allows to run Tkinter tests with the specified value of
tkinter.wantobjects, for example "-u wantobjects=0".
(cherry picked from commit 21ed1e2a94)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Add tests for negative offset, out of bound offset, invalid type of offset,
non-writeable buffer, non-continuous buffer, invalid type of buffer.
Repeat all tests for struct.Struct.pack_into().
(cherry picked from commit 66680f1230)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
gh-143602: Fix duplicate buffer exports in io.BytesIO.write (#143629)
Fix an inconsistency issue in io.BytesIO.write() where the buffer was exported
twice, which could lead to unexpected data overwrites and position drift when
the buffer changes between exports.
(cherry picked from commit c461aa99e2)
Co-authored-by: zhong <60600792+superboy-zjc@users.noreply.github.com>
The behaviour of Cut in nested parentheses, Repeat, Opt, and similar
is somewhat chaotic. Apparently even the academic papers on PEG aren't
as clear as they could be.
And it doesn't really matter. Python only uses top-level cuts.
When that changes, we can clarify as much as necessary (and even
change the implementation to make sense for what we'll need).
Document that this is deliberately unspecified, and add a test to
make sure any decision is deliberate, tested and documented.
(cherry picked from commit f0a0467c17)
Co-authored-by: Petr Viktorin <encukou@gmail.com>
This happens when the set contained several elements with the same hash,
and then some of them were removed.
(cherry picked from commit b8e925b4f8)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Writing out an object may involve a slot lookup, which is not safe to do with
an exception raised. In debug mode an assertion failure will occur if this
happens.
(cherry picked from commit ce8f5f98c6)
Co-authored-by: Duane Griffin <duaneg@dghda.com>