Skip to content

Commit debc710

Browse files
authored
Merge branch 'main' into fix-shutil-recursion-loop
2 parents cdebad2 + 748c4b4 commit debc710

8 files changed

Lines changed: 70 additions & 46 deletions

File tree

Doc/howto/free-threading-python.rst

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ available processing power by running threads in parallel on available CPU cores
1111
While not all software will benefit from this automatically, programs
1212
designed with threading in mind will run faster on multi-core hardware.
1313

14-
The free-threaded mode is working and continues to be improved, but
15-
there is some additional overhead in single-threaded workloads compared
16-
to the regular build. Additionally, third-party packages, in particular ones
14+
Some third-party packages, in particular ones
1715
with an :term:`extension module`, may not be ready for use in a
1816
free-threaded build, and will re-enable the :term:`GIL`.
1917

@@ -101,63 +99,42 @@ This section describes known limitations of the free-threaded CPython build.
10199
Immortalization
102100
---------------
103101

104-
The free-threaded build of the 3.13 release makes some objects :term:`immortal`.
102+
In the free-threaded build, some objects are :term:`immortal`.
105103
Immortal objects are not deallocated and have reference counts that are
106104
never modified. This is done to avoid reference count contention that would
107105
prevent efficient multi-threaded scaling.
108106

109-
An object will be made immortal when a new thread is started for the first time
110-
after the main thread is running. The following objects are immortalized:
107+
As of the 3.14 release, immortalization is limited to:
111108

112-
* :ref:`function <user-defined-funcs>` objects declared at the module level
113-
* :ref:`method <instance-methods>` descriptors
114-
* :ref:`code <code-objects>` objects
115-
* :term:`module` objects and their dictionaries
116-
* :ref:`classes <classes>` (type objects)
117-
118-
Because immortal objects are never deallocated, applications that create many
119-
objects of these types may see increased memory usage under Python 3.13. This
120-
has been addressed in the 3.14 release, where the aforementioned objects use
121-
deferred reference counting to avoid reference count contention.
122-
123-
Additionally, numeric and string literals in the code as well as strings
124-
returned by :func:`sys.intern` are also immortalized in the 3.13 release. This
125-
behavior is part of the 3.14 release as well and it is expected to remain in
126-
future free-threaded builds.
109+
* Code constants: numeric literals, string literals, and tuple literals
110+
composed of other constants.
111+
* Strings interned by :func:`sys.intern`.
127112

128113

129114
Frame objects
130115
-------------
131116

132-
It is not safe to access :ref:`frame <frame-objects>` objects from other
133-
threads and doing so may cause your program to crash . This means that
134-
:func:`sys._current_frames` is generally not safe to use in a free-threaded
135-
build. Functions like :func:`inspect.currentframe` and :func:`sys._getframe`
136-
are generally safe as long as the resulting frame object is not passed to
137-
another thread.
117+
It is not safe to access :attr:`frame.f_locals` from a :ref:`frame <frame-objects>`
118+
object if that frame is currently executing in another thread, and doing so may
119+
crash the interpreter.
120+
138121

139122
Iterators
140123
---------
141124

142-
Sharing the same iterator object between multiple threads is generally not
143-
safe and threads may see duplicate or missing elements when iterating or crash
144-
the interpreter.
125+
It is generally not thread-safe to access the same iterator object from
126+
multiple threads concurrently, and threads may see duplicate or missing
127+
elements.
145128

146129

147130
Single-threaded performance
148131
---------------------------
149132

150133
The free-threaded build has additional overhead when executing Python code
151-
compared to the default GIL-enabled build. In 3.13, this overhead is about
152-
40% on the `pyperformance <https://pyperformance.readthedocs.io/>`_ suite.
153-
Programs that spend most of their time in C extensions or I/O will see
154-
less of an impact. The largest impact is because the specializing adaptive
155-
interpreter (:pep:`659`) is disabled in the free-threaded build.
156-
157-
The specializing adaptive interpreter has been re-enabled in a thread-safe way
158-
in the 3.14 release. The performance penalty on single-threaded code in
159-
free-threaded mode is now roughly 5-10%, depending on the platform and C
160-
compiler used.
134+
compared to the default GIL-enabled build. The amount of overhead depends
135+
on the workload and hardware. On the pyperformance benchmark suite, the
136+
average overhead ranges from about 1% on macOS aarch64 to 8% on x86-64 Linux
137+
systems.
161138

162139

163140
Behavioral changes

Doc/library/select.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ The module defines the following:
115115
:ref:`kevent-objects` below for the methods supported by kevent objects.
116116

117117

118-
.. function:: select(rlist, wlist, xlist[, timeout])
118+
.. function:: select(rlist, wlist, xlist, timeout=None)
119119

120120
This is a straightforward interface to the Unix :c:func:`!select` system call.
121121
The first three arguments are iterables of 'waitable objects': either
@@ -131,7 +131,7 @@ The module defines the following:
131131
platform-dependent. (It is known to work on Unix but not on Windows.) The
132132
optional *timeout* argument specifies a time-out in seconds; it may be
133133
a non-integer to specify fractions of seconds.
134-
When the *timeout* argument is omitted the function blocks until
134+
When the *timeout* argument is omitted or ``None``, the function blocks until
135135
at least one file descriptor is ready. A time-out value of zero specifies a
136136
poll and never blocks.
137137

Include/pyport.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,15 @@ extern "C" {
509509
# define Py_CAN_START_THREADS 1
510510
#endif
511511

512-
#ifdef WITH_THREAD
513-
// HAVE_THREAD_LOCAL is just defined here for compatibility's sake
512+
513+
/* gh-142163: Some libraries rely on HAVE_THREAD_LOCAL being undefined, so
514+
* we can only define it only when Py_BUILD_CORE is set.*/
515+
#ifdef Py_BUILD_CORE
516+
// This is no longer coupled to _Py_thread_local.
514517
# define HAVE_THREAD_LOCAL 1
518+
#endif
519+
520+
#ifdef WITH_THREAD
515521
# ifdef thread_local
516522
# define _Py_thread_local thread_local
517523
# elif __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)

Lib/pdb.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def find_first_executable_line(code):
130130
return code.co_firstlineno
131131

132132
def find_function(funcname, filename):
133-
cre = re.compile(r'def\s+%s(\s*\[.+\])?\s*[(]' % re.escape(funcname))
133+
cre = re.compile(r'(?:async\s+)?def\s+%s(\s*\[.+\])?\s*[(]' % re.escape(funcname))
134134
try:
135135
fp = tokenize.open(filename)
136136
except OSError:
@@ -1487,7 +1487,9 @@ def lineinfo(self, identifier):
14871487
f = self.lookupmodule(parts[0])
14881488
if f:
14891489
fname = f
1490-
item = parts[1]
1490+
item = parts[1]
1491+
else:
1492+
return failed
14911493
answer = find_function(item, self.canonic(fname))
14921494
return answer or failed
14931495

Lib/test/test_pdb.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4587,6 +4587,41 @@ def bar():
45874587
]))
45884588
self.assertIn('break in bar', stdout)
45894589

4590+
@unittest.skipIf(SKIP_CORO_TESTS, "Coroutine tests are skipped")
4591+
def test_async_break(self):
4592+
script = """
4593+
import asyncio
4594+
4595+
async def main():
4596+
pass
4597+
4598+
asyncio.run(main())
4599+
"""
4600+
commands = """
4601+
break main
4602+
continue
4603+
quit
4604+
"""
4605+
stdout, stderr = self.run_pdb_script(script, commands)
4606+
self.assertRegex(stdout, r"Breakpoint 1 at .*main\.py:5")
4607+
self.assertIn("pass", stdout)
4608+
4609+
def test_issue_59000(self):
4610+
script = """
4611+
def foo():
4612+
pass
4613+
4614+
class C:
4615+
def foo(self):
4616+
pass
4617+
"""
4618+
commands = """
4619+
break C.foo
4620+
quit
4621+
"""
4622+
stdout, stderr = self.run_pdb_script(script, commands)
4623+
self.assertIn("The specified object 'C.foo' is not a function", stdout)
4624+
45904625

45914626
class ChecklineTests(unittest.TestCase):
45924627
def setUp(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the ``HAVE_THREAD_LOCAL`` macro being defined without the
2+
``Py_BUILD_CORE`` macro set after including :file:`Python.h`.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :mod:`pdb` breakpoint resolution for class methods when the module defining the class is not imported.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow :mod:`pdb` to set breakpoints on async functions with function names.

0 commit comments

Comments
 (0)