Skip to content

Commit e27f3cd

Browse files
Merge branch '3.15' into backport-6e9a900-3.15
2 parents 22276a0 + 32d59ce commit e27f3cd

31 files changed

Lines changed: 383 additions & 112 deletions

‎Doc/whatsnew/3.15.rst‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2516,6 +2516,17 @@ New features
25162516

25172517
(Contributed by Victor Stinner in :gh:`141510`.)
25182518

2519+
* Add the :pep:`757` export/import functions to the limited C API:
2520+
2521+
* :c:func:`PyLong_GetNativeLayout`
2522+
* :c:func:`PyLong_Export`
2523+
* :c:func:`PyLong_FreeExport`
2524+
* :c:func:`PyLongWriter_Create`
2525+
* :c:func:`PyLongWriter_Finish`
2526+
* :c:func:`PyLongWriter_Discard`
2527+
2528+
(Contributed by Sergey B Kirpichev in :gh:`143869`.)
2529+
25192530
* Add :c:func:`PyObject_CallFinalizerFromDealloc` function to the limited C
25202531
API.
25212532
(Contributed by Victor Stinner in :gh:`146063`.)

‎Lib/_pydecimal.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5158,8 +5158,8 @@ def number_class(self, a):
51585158
"""Returns an indication of the class of the operand.
51595159
51605160
The class is one of the following strings:
5161-
-sNaN
5162-
-NaN
5161+
sNaN
5162+
NaN
51635163
-Infinity
51645164
-Normal
51655165
-Subnormal
@@ -5668,8 +5668,9 @@ def to_integral_value(self, a):
56685668
When the operand has a negative exponent, the result is the same
56695669
as using the quantize() operation using the given operand as the
56705670
left-hand-operand, 1E+0 as the right-hand-operand, and the precision
5671-
of the operand as the precision setting, except that no flags will
5672-
be set. The rounding mode is taken from the context.
5671+
of the operand as the precision setting, except that the Inexact and
5672+
Rounded flags will not be set. The rounding mode is taken from the
5673+
context.
56735674
56745675
>>> ExtendedContext.to_integral_value(Decimal('2.1'))
56755676
Decimal('2')

‎Lib/test/test_asyncio/test_tasks.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,52 @@ async def notmuch():
431431
self.assertEqual(t.get_name(), '123456789')
432432
self.loop.run_until_complete(t)
433433

434+
# The free-threaded GC publishes allocation counts in batches, so the
435+
# collection cannot be scheduled to run inside get_name().
436+
@support.requires_gil_enabled("needs precise GC scheduling")
437+
def test_task_get_name_reentered_during_formatting(self):
438+
# gh-158159: formatting the lazy default name can run the GC, and a
439+
# finalizer may call get_name() on the same task, replacing the name
440+
# the outer call is still formatting.
441+
loop = self.loop
442+
observed = {}
443+
keep = []
444+
task = None
445+
446+
class Reenter:
447+
def __init__(self):
448+
self.cycle = self
449+
450+
def __del__(self):
451+
observed['inner'] = task.get_name()
452+
# Reuse the memory of a counter that was freed too early.
453+
keep.extend(10**9 + i for i in range(8))
454+
455+
async def notmuch():
456+
pass
457+
458+
async def scenario():
459+
gc.collect()
460+
Reenter()
461+
old_threshold = gc.get_threshold()
462+
gc.set_threshold(1)
463+
try:
464+
scratch = [[0], [0], [0]] # schedule a collection
465+
observed['outer'] = task.get_name()
466+
finally:
467+
gc.set_threshold(*old_threshold)
468+
del scratch
469+
470+
# Names are only freeable once the counter is past the small ints.
471+
for _ in range(1100):
472+
loop.run_until_complete(self.new_task(loop, notmuch(), name=None))
473+
task = self.new_task(loop, scenario(), name=None)
474+
loop.run_until_complete(task)
475+
476+
self.assertRegex(observed['outer'], r'^Task-\d+$')
477+
self.assertEqual(observed['outer'], observed['inner'])
478+
self.assertEqual(task.get_name(), observed['outer'])
479+
434480
def test_task_repr_name_not_str(self):
435481
async def notmuch():
436482
return 123

‎Lib/test/test_augassign.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,25 @@ def testSequences(self):
6464
self.assertEqual(x, [1, 2, 1, 2, 3])
6565
self.assertTrue(x is y)
6666

67+
def testSequenceRepeatRightOperand(self):
68+
# gh-161968
69+
class MyInt(int):
70+
pass
71+
class Index:
72+
def __index__(self):
73+
return 2
74+
75+
seq = [1]
76+
x = MyInt(2)
77+
x *= seq
78+
self.assertEqual(x, [1, 1])
79+
# The right operand is not modified in place.
80+
self.assertEqual(seq, [1])
81+
82+
x = Index()
83+
x *= 'a'
84+
self.assertEqual(x, 'aa')
85+
6786
def testCustomMethods1(self):
6887

6988
class aug_test:

‎Lib/test/test_free_threading/test_gc.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ def mutate():
119119
b.wait()
120120
time.sleep(0.2)
121121

122+
def test_running_method_not_collected(self):
123+
class C:
124+
def m(self):
125+
wr = weakref.ref(C.__dict__["m"])
126+
del C.m
127+
gc.collect()
128+
return wr() is not None
129+
130+
def run(self):
131+
return self.m()
132+
133+
self.assertTrue(C().run())
134+
122135
def test_debug_collectable_python_stderr(self):
123136
# The DEBUG_COLLECTABLE output is written with sys.stderr.write(),
124137
# which may run Python code or wait for another thread (e.g. for the

‎Lib/test/test_free_threading/test_iteration.py‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import sys
12
import threading
23
import unittest
34
from test import support
5+
from test.support import threading_helper
46

57
# The race conditions these tests were written for only happen every now and
68
# then, even with the current numbers. To find rare race conditions, bumping
@@ -112,6 +114,62 @@ def worker():
112114
self.assert_iterator_results(results, list(seq))
113115

114116

117+
class ContendedSeqIterExhaustionTest(unittest.TestCase):
118+
"""Test draining a shared iter() fallback iterator (PySeqIter_Type).
119+
120+
Sequences implementing __getitem__ but not __iter__ iterate through
121+
PySeqIter_Type. Unlike the other tests in this file, this uses a
122+
tiny sequence and many rounds so that many threads reach the racy
123+
exhaustion path simultaneously (see gh-156310, where this
124+
use-after-freed the sequence).
125+
"""
126+
127+
class Seq:
128+
def __init__(self, n):
129+
self.n = n
130+
131+
def __getitem__(self, i):
132+
if i >= self.n:
133+
raise IndexError(i)
134+
return i
135+
136+
@support.refcount_test
137+
def test_shared_iterator_exhaustion(self):
138+
nthreads = 8
139+
nrounds = 20 if support.check_sanitizer(thread=True) else 100
140+
seq = self.Seq(4)
141+
expected = set(range(seq.n))
142+
refcount_before = sys.getrefcount(seq)
143+
144+
def drain(it, barrier, results):
145+
items = []
146+
barrier.wait()
147+
for item in it:
148+
items.append(item)
149+
results.extend(items)
150+
151+
for _ in range(nrounds):
152+
it = iter(seq)
153+
barrier = threading.Barrier(nthreads)
154+
results = []
155+
threads = [
156+
threading.Thread(target=drain, args=(it, barrier, results))
157+
for _ in range(nthreads)
158+
]
159+
with threading_helper.catch_threading_exception() as cm:
160+
with threading_helper.start_threads(threads):
161+
pass
162+
self.assertIsNone(cm.exc_value)
163+
del it
164+
# Threads may see duplicate or missing items, but never
165+
# invented ones.
166+
self.assertEqual(set(results) - expected, set())
167+
168+
# A double-DECREF of the sequence does not always crash; it
169+
# reliably shows up as a sagging reference count.
170+
self.assertEqual(sys.getrefcount(seq), refcount_before)
171+
172+
115173
class ContendedRangeIterationTest(ContendedTupleIterationTest):
116174
def make_testdata(self, n):
117175
return range(n)

‎Lib/test/test_iter.py‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import sys
44
import unittest
5+
from test import support
56
from test.support import cpython_only
67
from test.support.os_helper import TESTFN, unlink
78
from test.support import check_free_after_iterating, ALWAYS_EQ, NEVER_EQ
@@ -249,6 +250,71 @@ def test_mutating_seq_class_exhausted_iter(self):
249250
self.assertEqual(list(empit), [5, 6])
250251
self.assertEqual(list(a), [0, 1, 2, 3, 4, 5, 6])
251252

253+
@support.refcount_test
254+
def test_seq_class_reentrant_exhaustion(self):
255+
# gh-156310: a re-entrant next() from inside __getitem__ (or from
256+
# __del__ of the IndexError instance) that exhausts the iterator
257+
# used to make the outer next() DECREF the sequence a second time.
258+
it = None
259+
260+
class ReentrantGetItem:
261+
def __init__(self):
262+
self.calls = 0
263+
264+
def __getitem__(self, i):
265+
self.calls += 1
266+
if self.calls == 1:
267+
for _ in it:
268+
pass
269+
raise IndexError(i)
270+
271+
seq = ReentrantGetItem()
272+
refcount = sys.getrefcount(seq)
273+
it = iter(seq)
274+
self.assertEqual(list(it), [])
275+
del it
276+
support.gc_collect()
277+
self.assertEqual(sys.getrefcount(seq), refcount)
278+
279+
class ReentrantIndexError(IndexError):
280+
def __del__(self):
281+
try:
282+
next(it)
283+
except StopIteration:
284+
pass
285+
286+
class RaiseReentrant:
287+
def __getitem__(self, i):
288+
raise ReentrantIndexError(i)
289+
290+
seq = RaiseReentrant()
291+
refcount = sys.getrefcount(seq)
292+
it = iter(seq)
293+
self.assertEqual(list(it), [])
294+
del it
295+
support.gc_collect()
296+
self.assertEqual(sys.getrefcount(seq), refcount)
297+
298+
# An outer __getitem__ that succeeds after a re-entrant next()
299+
# exhausted the iterator must not revive it.
300+
class ReviveGetItem:
301+
def __init__(self):
302+
self.calls = 0
303+
304+
def __getitem__(self, i):
305+
self.calls += 1
306+
if self.calls == 1:
307+
for _ in it:
308+
pass
309+
if i >= 3:
310+
raise IndexError(i)
311+
return i
312+
313+
it = iter(ReviveGetItem())
314+
self.assertEqual(next(it), 0)
315+
self.assertEqual(list(it), [])
316+
self.assertEqual(it.__length_hint__(), 0)
317+
252318
def test_reduce_mutating_builtins_iter(self):
253319
# This is a reproducer of issue #101765
254320
# where iter `__reduce__` calls could lead to a segfault or SystemError

‎Lib/test/test_pathlib/test_pathlib.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,6 +2332,9 @@ def test_touch_common(self):
23322332
self.assertFalse(p.exists())
23332333
p.touch()
23342334
self.assertTrue(p.exists())
2335+
# Take the baseline from utime(None) like touch() below; creation can
2336+
# use a different clock (Emscripten NODEFS).
2337+
os.utime(p, None)
23352338
st = p.stat()
23362339
old_mtime = st.st_mtime
23372340
old_mtime_ns = st.st_mtime_ns

‎Lib/test/test_type_annotations.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,32 @@ class C:
505505
""")
506506
self.assertEqual(ns["C"].__annotations__, {"__classdict__": int})
507507

508+
def test_class_binds_dunder_classdict(self):
509+
# The class body binding __classdict__ must not hide the cell that
510+
# annotation and type parameter scopes use to see the class namespace.
511+
for binding in (
512+
"__classdict__ = 1",
513+
"__classdict__: str = 1",
514+
"for __classdict__ in (1,): pass",
515+
"global __classdict__",
516+
):
517+
with self.subTest(binding=binding):
518+
ns = run_code(f"""
519+
class C:
520+
{binding}
521+
y = int
522+
def f(x: y): pass
523+
class D[T: y]: pass
524+
class E[T]: pass
525+
type A[T: y] = y
526+
""")
527+
C = ns["C"]
528+
self.assertEqual(C.f.__annotations__, {"x": int})
529+
self.assertIs(C.D.__type_params__[0].__bound__, int)
530+
self.assertEqual(C.E.__type_params__[0].__name__, "T")
531+
self.assertIs(C.A.__type_params__[0].__bound__, int)
532+
self.assertIs(C.A.__value__, int)
533+
508534
def test_future_annotations(self):
509535
code = """
510536
from __future__ import annotations

‎Lib/test/test_urllib.py‎

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import email.message
88
import io
99
import unittest
10-
import unittest.mock
1110
from test import support
1211
from test.support import os_helper
1312
from test.support import socket_helper
@@ -401,17 +400,11 @@ def test_url_host_with_newline_header_injection_rejected(self):
401400
host = "localhost\r\nX-injected: header\r\n"
402401
schemeless_url = "//" + host + ":8080/test/?test=a"
403402
try:
404-
# Once \r\n are stripped from the URL, the ':' in
405-
# "X-injected:" is mistaken for the port separator, and
406-
# urlsplit()/urlparse() now reject the resulting
407-
# non-numeric "port" at parse time, before a Request is
408-
# even built -- rejecting the injection earlier than
409-
# http.client's own InvalidURL check ever gets a chance to.
403+
InvalidURL = http.client.InvalidURL
410404
with self.assertRaisesRegex(
411-
ValueError, "Port could not be cast to integer value"):
405+
InvalidURL, r"contain control.*\\r"):
412406
urllib.request.urlopen(f"http:{schemeless_url}")
413-
with self.assertRaisesRegex(
414-
ValueError, "Port could not be cast to integer value"):
407+
with self.assertRaisesRegex(InvalidURL, r"contain control.*\\n"):
415408
urllib.request.urlopen(f"https:{schemeless_url}")
416409
finally:
417410
self.unfakehttp()
@@ -1679,17 +1672,6 @@ def test_url2pathname_resolve_host(self):
16791672
self.assertEqual(fn(f'//{socket.gethostname()}/foo/bar'), f'{sep}foo{sep}bar')
16801673
self.assertEqual(fn(f'//{socket.gethostname()}/foo/bar', resolve_host=True), f'{sep}foo{sep}bar')
16811674

1682-
def test_url2pathname_win_netloc_not_validated_as_port(self):
1683-
# The Windows branch of url2pathname() stuffs things like drive
1684-
# letters into the netloc (e.g. '//C:\\' -> authority 'C:\\'),
1685-
# which is not a real host[:port] pair. url2pathname() must not
1686-
# reject it as an invalid port; run the Windows branch directly
1687-
# so this is exercised on every platform, not just win32.
1688-
with unittest.mock.patch('os.name', 'nt'):
1689-
fn = urllib.request.url2pathname
1690-
self.assertEqual(fn('//C:\\'), 'C:\\')
1691-
self.assertEqual(fn('//C:80/'), 'C:80\\')
1692-
16931675
@unittest.skipUnless(sys.platform == 'win32',
16941676
'test specific to Windows pathnames.')
16951677
def test_url2pathname_win(self):

0 commit comments

Comments
 (0)