Skip to content

Commit 636b9dd

Browse files
Merge branch 'main' into gh-161955
2 parents 04486e2 + f4c13c8 commit 636b9dd

27 files changed

Lines changed: 757 additions & 729 deletions

File tree

‎.github/ISSUE_TEMPLATE/documentation.yml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ body:
77
value: |
88
> [!NOTE]
99
> Trivial changes (for example typos) don’t require an issue before opening a PR.
10+
- type: input
11+
id: url
12+
attributes:
13+
label: "Documentation URL"
14+
description: "A link to the relevant documentation page, if applicable."
15+
placeholder: "https://docs.python.org/3/..."
1016
- type: textarea
1117
id: description
1218
attributes:
1319
label: "Documentation"
14-
description: "A clear and concise description of the issue. Include a link to the page."
20+
description: "A clear and concise description of the issue."
1521
validations:
1622
required: true

‎AGENTS.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,16 @@ When acting on this repository, apply the policy's core principles:
1414
- Follow existing coding style and patterns.
1515
- Write tests that exercise the change.
1616
- Keep backwards compatibility with prior releases in mind.
17+
18+
## Opening issues
19+
20+
Do not auto-generate issues with AI tools.
21+
22+
Search GitHub for duplicate issues before opening a new issue. Do not open
23+
duplicates.
24+
25+
```bash
26+
gh search issues --repo python/cpython "<symptom keywords>"
27+
gh search issues --repo python/cpython "<function, module or option name>"
28+
gh issue list --repo python/cpython --state all --label "<topic label>" --search "<short area keywords>"
29+
```

‎Doc/using/ios.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ To add Python to an iOS Xcode project:
226226
.. code-block:: bash
227227
228228
set -e
229-
source $PROJECT_DIR/Python.xcframework/build/build_utils.sh
229+
source "$PROJECT_DIR/Python.xcframework/build/utils.sh"
230230
install_python Python.xcframework app
231231
232232
If you have placed your XCframework somewhere other than the root of your

‎Lib/test/test_capi/test_object.py‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,83 @@ def test_pyobject_dump(self):
354354
output = self.pyobject_dump(NULL)
355355
self.assertRegex(output, r'<object at .* is freed>')
356356

357+
def test_object_clear_managed_dict(self):
358+
# PyObject_ClearManagedDict
359+
clear_managed_dict = _testcapi.pyobject_clear_managed_dict
360+
ManagedDictType = _testcapi.ManagedDictType
361+
362+
obj = ManagedDictType()
363+
obj.value = 'first'
364+
# asserting `__dict__` is a must to materialize the value before clean:
365+
self.assertEqual(obj.__dict__, {'value': 'first'})
366+
self.assertIs(type(obj.__dict__), dict)
367+
clear_managed_dict(obj)
368+
self.assertEqual(obj.__dict__, {})
369+
self.assertIs(type(obj.__dict__), dict)
370+
371+
# big object:
372+
obj = ManagedDictType()
373+
existing_dict = {f'attr_{x}': x for x in range(100)}
374+
obj.__dict__ = existing_dict
375+
self.assertIs(obj.__dict__, existing_dict)
376+
clear_managed_dict(obj)
377+
self.assertEqual(obj.__dict__, {})
378+
self.assertIs(type(obj.__dict__), dict)
379+
380+
# empty dict:
381+
obj = ManagedDictType()
382+
self.assertEqual(obj.__dict__, {})
383+
self.assertIs(type(obj.__dict__), dict)
384+
clear_managed_dict(obj)
385+
self.assertEqual(obj.__dict__, {})
386+
self.assertIs(type(obj.__dict__), dict)
387+
388+
# double clear:
389+
obj = ManagedDictType()
390+
obj.value = 'first'
391+
self.assertEqual(obj.__dict__, {'value': 'first'})
392+
clear_managed_dict(obj)
393+
clear_managed_dict(obj)
394+
self.assertEqual(obj.__dict__, {})
395+
self.assertIs(type(obj.__dict__), dict)
396+
397+
# unmaterialized:
398+
obj = ManagedDictType()
399+
obj.value = 'first'
400+
clear_managed_dict(obj)
401+
self.assertEqual(obj.__dict__, {})
402+
self.assertIs(type(obj.__dict__), dict)
403+
404+
# CRASHES: clear_managed_dict(NULL)
405+
406+
@threading_helper.requires_working_threading()
407+
@support.nomemtest
408+
def test_object_clear_managed_dict_ft_memory_error(self):
409+
# gh-152254: Test PyObject_ClearManagedDict() error.
410+
# The setup must contain: object owned by another thread
411+
# and the attempt to clear the managed dict with no memory left.
412+
clear_managed_dict = _testcapi.pyobject_clear_managed_dict
413+
ManagedDictType = _testcapi.ManagedDictType
414+
415+
obj = ManagedDictType()
416+
obj.value = 'first'
417+
self.assertEqual(len(obj.__dict__), 1)
418+
419+
def worker():
420+
# This needs to be in a separate thread for the crash to happen:
421+
with support.catch_unraisable_exception() as ex:
422+
# `PyObject_ClearManagedDict()` used to crash:
423+
_testcapi.call_with_nomemory(0, 1, clear_managed_dict, obj)
424+
self.assertIsNotNone(ex.unraisable)
425+
self.assertIs(ex.unraisable.exc_type, MemoryError)
426+
self.assertIn('managed dict', ex.unraisable.err_msg)
427+
self.assertEqual(obj.__dict__, {})
428+
self.assertIs(type(obj.__dict__), dict)
429+
430+
threading_helper.run_concurrently([worker])
431+
self.assertEqual(obj.__dict__, {})
432+
self.assertIs(type(obj.__dict__), dict)
433+
357434

358435
class RefTracerTest(unittest.TestCase):
359436
@support.requires_resource('cpu')

‎Lib/test/test_capi/test_unicode.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1883,7 +1883,9 @@ def test_check_interned(self):
18831883

18841884
def fresh_string(text):
18851885
# Create a new string object by decoding a bytes string.
1886-
return text.encode().decode()
1886+
# Add a suffix to make sure that it's not interned, even if
1887+
# the string is interned in the PYC file (Free Threading).
1888+
return (text + ' suffix').encode().decode()
18871889

18881890
s = fresh_string('hello')
18891891
self.assertEqual(check_interned(s), SSTATE_NOT_INTERNED)

‎Lib/test/test_external_inspection.py‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
try:
5656
from _remote_debugging import PROCESS_VM_READV_SUPPORTED
5757
from _remote_debugging import RemoteUnwinder
58-
from _remote_debugging import FrameInfo, CoroInfo, TaskInfo
58+
from _remote_debugging import FrameInfo, CoroInfo, TaskInfo, AwaitedInfo
5959
except ImportError:
6060
raise unittest.SkipTest(
6161
"Test only runs when _remote_debugging is available"
@@ -487,6 +487,23 @@ def _extract_coroutine_funcnames(self, trace):
487487
for task in info.awaited_by
488488
}
489489

490+
@skip_if_not_supported
491+
def test_async_awaited_info(self):
492+
async def main():
493+
task = asyncio.current_task()
494+
unwinder = RemoteUnwinder(os.getpid())
495+
for method in (unwinder.get_async_stack_trace,
496+
unwinder.get_all_awaited_by):
497+
with self.subTest(method=method.__name__):
498+
info, = (info for info in method()
499+
if info.thread_id == threading.get_native_id())
500+
self.assertIsInstance(info, AwaitedInfo)
501+
task_info, = info.awaited_by
502+
self.assertEqual(task_info.task_id, id(task))
503+
self.assertEqual(task_info.task_name, task.get_name())
504+
505+
asyncio.run(main())
506+
490507
@skip_if_not_supported
491508
def test_long_task_name_is_truncated(self):
492509
# gh-157788
@@ -520,6 +537,22 @@ async def rec(n):
520537

521538
self.assertEqual(asyncio.run(rec(3)), ["rec"] * 4)
522539

540+
@skip_if_not_supported
541+
def test_future_waiter_has_empty_stack(self):
542+
async def main():
543+
task = asyncio.current_task()
544+
future = asyncio.get_running_loop().create_future()
545+
asyncio.future_add_to_awaited_by(task, future)
546+
try:
547+
trace = RemoteUnwinder(os.getpid()).get_all_awaited_by()
548+
task_info = self._get_task_id_map(trace)[id(task)]
549+
self.assertEqual(task_info.awaited_by, [([], id(future))])
550+
self.assertTrue(task_info.coroutine_stack[0].call_stack)
551+
finally:
552+
asyncio.future_discard_from_awaited_by(task, future)
553+
554+
asyncio.run(main())
555+
523556
@skip_if_not_supported
524557
def test_async_generator_stack_is_not_truncated(self):
525558
# gh-159620
@@ -663,6 +696,17 @@ async def main():
663696
stacks = self._extract_coroutine_funcnames(asyncio.run(main()))
664697
self.assertEqual(stacks["consumer"], ["consumer"])
665698

699+
@skip_if_not_supported
700+
def test_self_trace_with_empty_linetable(self):
701+
def sample():
702+
return RemoteUnwinder(os.getpid()).get_stack_trace()
703+
704+
sample.__code__ = sample.__code__.replace(co_linetable=b"")
705+
frame = self._find_frame_in_trace(
706+
sample(), lambda f: f.funcname == sample.__qualname__
707+
)
708+
self.assertEqual(frame.location, (-1, -1, -1, -1))
709+
666710
@skip_if_not_supported
667711
@unittest.skipIf(
668712
sys.platform == "linux" and not PROCESS_VM_READV_SUPPORTED,
@@ -4687,6 +4731,28 @@ def test_dead_process_raises_not_degrades(self):
46874731
else:
46884732
self.fail("ProcessLookupError never raised for dead process")
46894733

4734+
def test_dead_process_before_first_async_read(self):
4735+
script_body = """\
4736+
import asyncio
4737+
sock.sendall(b"ready")
4738+
sock.recv(16)
4739+
"""
4740+
with self._target_process(script_body) as (p, client_socket, _):
4741+
_wait_for_signal(client_socket, b"ready")
4742+
unwinders = [
4743+
RemoteUnwinder(p.pid, debug=debug)
4744+
for debug in (False, True)
4745+
]
4746+
4747+
p.kill()
4748+
p.wait()
4749+
4750+
for debug, unwinder in zip((False, True), unwinders):
4751+
for method in ("get_async_stack_trace", "get_all_awaited_by"):
4752+
with self.subTest(debug=debug, method=method):
4753+
with self.assertRaises(ProcessLookupError):
4754+
getattr(unwinder, method)()
4755+
46904756

46914757
@requires_remote_subprocess_debugging()
46924758
class TestFrameChainLimits(RemoteInspectionTestBase):

‎Lib/test/test_time.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,8 @@ def test_tzset(self):
562562
# on some operating systems (e.g. FreeBSD), which is wrong. See for
563563
# example this bug:
564564
# http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=93810
565-
self.assertIn(time.tzname[0], ('AEST' 'EST'), time.tzname[0])
566-
self.assertTrue(time.tzname[1] in ('AEDT', 'EDT'), str(time.tzname[1]))
565+
self.assertIn(time.tzname[0], ('AEST', 'EST'))
566+
self.assertIn(time.tzname[1], ('AEDT', 'EDT'))
567567
self.assertEqual(len(time.tzname), 2)
568568
self.assertEqual(time.daylight, 1)
569569
self.assertEqual(time.timezone, -36000)

‎Lib/test/test_venv.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,28 @@ def test_prefixes(self):
302302
self.assertEqual(pathlib.Path(out.strip().decode()),
303303
pathlib.Path(expected), prefix)
304304

305+
# gh-157917: the "\\?\" extended-length prefix must not leak into the prefix
306+
@requireVenvCreate
307+
@unittest.skipUnless(os.name == 'nt', 'only relevant on Windows')
308+
def test_prefixes_extended_length_path(self):
309+
"""
310+
Test that invoking a venv through a "\\?\" extended-length path does
311+
not leave that prefix behind in the prefixes. Windows does not
312+
resolve relative components (such as ".." or "/") in such paths, so
313+
they break tools that join them onto sysconfig-derived paths.
314+
"""
315+
rmtree(self.env_dir)
316+
self.run_with_capture(venv.create, self.env_dir)
317+
envpy = os.path.join('\\\\?\\' + self.env_dir, self.bindir, self.exe)
318+
for prefix in ('prefix', 'exec_prefix'):
319+
with self.subTest(prefix):
320+
out, err = check_output(
321+
[envpy, '-c', 'import sys; print(sys.%s)' % prefix],
322+
encoding='utf-8')
323+
self.assertFalse(out.strip().startswith('\\\\?\\'), out)
324+
self.assertEqual(pathlib.Path(out.strip()),
325+
pathlib.Path(self.env_dir), prefix)
326+
305327
@requireVenvCreate
306328
def test_sysconfig(self):
307329
"""
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash during the clean up of managed dict objects under a memory
2+
pressure in :c:func:`PyObject_ClearManagedDict`.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The Apple testbed projects now build when the path of the project contains a
2+
space.

0 commit comments

Comments
 (0)