PEP-734 has been accepted (for 3.14).
(FTR, I'm opposed to putting this under the concurrent package, but
doing so is the SC condition under which the module can land in 3.14.)
(cherry picked from commit 62143736b, AKA gh-133958)
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)
Co-authored-by: Gyeongjae Choi <def6488@gmail.com>
Apply Intel Control-flow Technology for x86-64 on asm_trampoline.S.
Required for mitigation against return-oriented programming (ROP)
and Call or Jump Oriented Programming (COP/JOP) attacks.
Manual application is required for the assembly files.
See also: https://sourceware.org/annobin/annobin.html/Test-cf-protection.html
Fix warnings `set but not used [-Wunused-but-set-variable]` in remote_debug.h (GH-135290)
(cherry picked from commit 49fc1f215aeb0f71445505191ccb65517b58a5aa)
Co-authored-by: Chris Eibl <138194463+chris-eibl@users.noreply.github.com>
Fix definition of `_Py_RemoteDebug_` symbols for static linking (GH-135146)
(cherry picked from commit 2e1ad6eb26871a379e5d3aa626d6fc93eba72a86)
Co-authored-by: Zanie Blue <contact@zanie.dev>
gh-133968: Add PyUnicodeWriter_WriteASCII() function (#133973)
Replace most PyUnicodeWriter_WriteUTF8() calls with
PyUnicodeWriter_WriteASCII().
(cherry picked from commit f49a07b531543dd8a42d90f5b1c89c0312fbf806)
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
gh-134876: Add fallback for when process_vm_readv fails with ENOSYS (GH-134878)
(cherry picked from commit ac9c3431cc5916a795c42b3e2b965233ceffe6f0)
Co-authored-by: Daniel Golding <goldingd89@gmail.com>
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.
(cherry picked from commit 6b77af257c25d31f1f137e477cb23e63692ddf29)
On Windows, the `_PyOS_SigintEvent()` event handle is used to interrupt
the main thread when Ctrl-C is pressed. Previously, we also waited on
the event from other threads, but ignored the result. However, this can
race with interpreter shutdown because the main thread closes the handle
in `_PySignal_Fini` and threads may still be running and using mutexes
during interpreter shtudown.
Only use `_PyOS_SigintEvent()` in the main thread in parking_lot.c, like
we do in other places in the CPython codebase.
(cherry picked from commit cc581f32bf5f15e9f2f89b830ec64ea25684d0cd)
Co-authored-by: Sam Gross <colesbury@gmail.com>
Apply Intel Control-flow Technology for x86-64 on asm_trampoline.S.
Required for mitigation against return-oriented programming (ROP)
and Call or Jump Oriented Programming (COP/JOP) attacks.
Manual application is required for the assembly files.
See also: https://sourceware.org/annobin/annobin.html/Test-cf-protection.html
It now supports most callables, full args, and return values.
(cherry picked from commit 52deabe, AKA gh-133484)
Co-authored-by: Eric Snow ericsnowcurrently@gmail.com
gh-132917: fix data race on `last_mem` in free-threading gc (GH-134692)
(cherry picked from commit ac539e7e0d1622c61988f30c91acfd4c06521ced)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
This is a small follow-up to gh-133481. There's a corner case
in the behavior of PyImport_ImportModuleAttrString(), where
it expects __builtins__ to be set if __globals__ is set.
(cherry picked from commit 9b5e80000, AKA gh-134758)
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
Disable immortalization around Py_CompileString*().
The same approach as 332356b that fixed the refleaks in compile() and eval().
E: 09e72cf can pass test_capi, test_sys and test__interpchannels with this patch for me.
(cherry picked from commit c60f39ada625562bff26400f304690c19fe9f504, AKA gh-134686)
Co-authored-by: neonene <53406459+neonene@users.noreply.github.com>
Correct Apple platform includes for iOS.
(cherry picked from commit 965662ee4a986605b60da470d9e7c1e9a6f922b3)
Co-authored-by: Russell Keith-Magee <russell@keith-magee.com>
gh-91048: Refactor and optimize remote debugging module (#134652)
Completely refactor Modules/_remote_debugging_module.c with improved
code organization, replacing scattered reference counting and error
handling with centralized goto error paths. This cleanup improves
maintainability and reduces code duplication throughout the module while
preserving the same external API.
Implement memory page caching optimization in Python/remote_debug.h to
avoid repeated reads of the same memory regions during debugging
operations. The cache stores previously read memory pages and reuses
them for subsequent reads, significantly reducing system calls and
improving performance.
Add code object caching mechanism with a new code_object_generation
field in the interpreter state that tracks when code object caches need
invalidation. This allows efficient reuse of parsed code object metadata
and eliminates redundant processing of the same code objects across
debugging sessions.
Optimize memory operations by replacing multiple individual structure
copies with single bulk reads for the same data structures. This reduces
the number of memory operations and system calls required to gather
debugging information from the target process.
Update Makefile.pre.in to include Python/remote_debug.h in the headers
list, ensuring that changes to the remote debugging header force proper
recompilation of dependent modules and maintain build consistency across
the codebase.
Also, make the module compatible with the free threading build as an extra :)
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
(cherry picked from commit 42b25ad4d3d6bcdc28ddfe07d2bf8831378bb0d1)
GH-130397: remove special-casing of C stack depth for WASI (GH-134469)
Removed special-casing for WASI when setting C stack depth limits. Since WASI has its own C stack checking this isn't a security risk.
Also disabled some tests that stopped passing. They all happened to have already been disabled under Emscripten.
(cherry picked from commit ad42dc1909bdf8ec775b63fb22ed48ff42797a17)
Co-authored-by: Brett Cannon <brett@python.org>
This is mostly a refactor to clean things up a bit, most notably the "XI namespace" code.
Making the session opaque requires adding the following internal-only functions:
* _PyXI_NewSession()
* _PyXI_FreeSession()
* _PyXI_GetMainNamespace()
(cherry picked from commit 4a4ac3ab4d, gh-134452)
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
This change includes some semi-related refactoring of queues and channels.
(cherry picked from commit d0eedfa10e, gh-134440)
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
It now supports a "full" fallback to _PyFunction_GetXIData() and then `_PyPickle_GetXIData()`.
There's also room for other fallback modes if that later makes sense.
(cherry picked from commit 88f8102a8f, AKA gh-133482)
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
Using the if instruction results in slightly shorter trampoline code.
(cherry picked from commit 3b7888bf3d43b903f0a7ebd16f39d8bb61dfbb9e)
Co-authored-by: Hood Chatham <roberthoodchatham@gmail.com>
It now supports non-ASCII paths in non-UTF-8 locales and
non-UTF-8 paths in UTF-8 locales.
(cherry picked from commit c09cec5d69f2ef6ab5e64c7e0579fbd9dcb2ca45)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
gh-133590: ensure that `TableEntry.linenumber_borrow` is initialized (GH-133681)
(cherry picked from commit c838e21fda21bb3ff91f586374e0da776c1c567b)
Co-authored-by: Lauta <ljfp@ljfp.xyz>
On Linux, use /proc/self/status for mem usage info. Using smaps_rollup is quite a lot slower and
we can get the similar info from /proc/self/status.
(cherry picked from commit 751db4e64993b5af98aa1c12cc9d7c7c5882961b)
Co-authored-by: Neil Schemenauer <nas-github@arctrix.com>
This converts functions, code, str, bytes, bytearray, and memoryview objects to PyCodeObject,
and ensure that the object looks like a script. That means no args, no return, and no closure.
_PyCode_GetPureScriptXIData() takes it a step further and ensures there are no globals.
We also add _PyObject_SupportedAsScript() to the internal C-API.
(cherry picked from commit c81fa2b9cd1ed4d4ff5c13984d6582c27e5f2633, AKA gh-133480)
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
Fix data race detected by tsan
(https://github.com/python/cpython/actions/runs/14857021107/job/41712717208?pr=133502):
young.count can be modified by other threads even while the gcstate is
locked.
This is the simplest fix to (potentially) unblock beta 1, although this
particular code path seems like it could just be an atomic swap followed by
an atomic add, without having the lock at all.
This is a follow-up to gh-133107. I realized that we could end up with an
infinite recursion if we try to run a function from __main__ in a subinterpreter.
For the free-threaded build, check the process resident set size (RSS)
increase before triggering a full automatic garbage collection. If the RSS
has not increased 10% since the last collection then it is deferred.