Skip to content

Commit 2098702

Browse files
Merge branch 'main' into gh-98278-bracket-carets
2 parents 287c14f + be3dfe5 commit 2098702

14 files changed

Lines changed: 144 additions & 188 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

‎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_external_inspection.py‎

Lines changed: 45 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,
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.

‎Modules/_remote_debugging/_remote_debugging.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ typedef struct {
121121
#define SIZEOF_GEN_OBJ sizeof(PyGenObject)
122122
#define SIZEOF_INTERP_FRAME sizeof(_PyInterpreterFrame)
123123
#define SIZEOF_LLIST_NODE sizeof(struct llist_node)
124-
#define SIZEOF_PAGE_CACHE_ENTRY sizeof(page_cache_entry_t)
125124
#define SIZEOF_PYOBJECT sizeof(PyObject)
126125
#define SIZEOF_SET_OBJ sizeof(PySetObject)
127126
#define SIZEOF_TASK_OBJ 4096
@@ -575,7 +574,6 @@ extern int parse_frame_object(
575574
RemoteUnwinderObject *unwinder,
576575
PyObject** result,
577576
uintptr_t address,
578-
uintptr_t* address_of_code_object,
579577
uintptr_t* previous_frame
580578
);
581579

@@ -584,7 +582,6 @@ extern int parse_frame_from_chunks(
584582
PyObject **result,
585583
uintptr_t address,
586584
uintptr_t *previous_frame,
587-
uintptr_t *stackpointer,
588585
StackChunkList *chunks
589586
);
590587

‎Modules/_remote_debugging/asyncio.c‎

Lines changed: 57 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,7 @@ parse_coro_chain(
386386
// Parse the previous frame using the gi_iframe from local copy
387387
uintptr_t prev_frame;
388388
uintptr_t gi_iframe_addr = coro_address + (uintptr_t)unwinder->debug_offsets.gen_object.gi_iframe;
389-
uintptr_t address_of_code_object = 0;
390-
if (parse_frame_object(unwinder, &name, gi_iframe_addr, &address_of_code_object, &prev_frame) < 0) {
389+
if (parse_frame_object(unwinder, &name, gi_iframe_addr, &prev_frame) < 0) {
391390
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to parse frame object in coro chain");
392391
return -1;
393392
}
@@ -422,13 +421,12 @@ parse_coro_chain(
422421
static PyObject*
423422
create_task_result(
424423
RemoteUnwinderObject *unwinder,
425-
uintptr_t task_address
424+
uintptr_t task_address,
425+
char is_task
426426
) {
427427
PyObject* result = NULL;
428428
PyObject *call_stack = NULL;
429429
PyObject *tn = NULL;
430-
char task_obj[SIZEOF_TASK_OBJ];
431-
uintptr_t coro_addr;
432430

433431
// Create call_stack first since it's the first tuple element
434432
call_stack = PyList_New(0);
@@ -444,25 +442,27 @@ create_task_result(
444442
goto error;
445443
}
446444

447-
// Parse coroutine chain
448-
if (_Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, task_address,
449-
(size_t)unwinder->async_debug_offsets.asyncio_task_object.size,
450-
task_obj) < 0) {
451-
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to read task object for coro chain");
452-
goto error;
453-
}
454-
455-
coro_addr = GET_MEMBER_NO_TAG(uintptr_t, task_obj, unwinder->async_debug_offsets.asyncio_task_object.task_coro);
456-
457-
if ((void*)coro_addr != NULL) {
458-
if (parse_coro_chain(unwinder, coro_addr, call_stack) < 0) {
459-
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to parse coroutine chain");
445+
if (is_task) {
446+
char task_obj[SIZEOF_TASK_OBJ];
447+
if (_Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, task_address,
448+
(size_t)unwinder->async_debug_offsets.asyncio_task_object.size,
449+
task_obj) < 0) {
450+
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to read task object for coro chain");
460451
goto error;
461452
}
462453

463-
if (PyList_Reverse(call_stack)) {
464-
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to reverse call stack");
465-
goto error;
454+
uintptr_t coro_addr = GET_MEMBER_NO_TAG(uintptr_t, task_obj, unwinder->async_debug_offsets.asyncio_task_object.task_coro);
455+
456+
if ((void*)coro_addr != NULL) {
457+
if (parse_coro_chain(unwinder, coro_addr, call_stack) < 0) {
458+
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to parse coroutine chain");
459+
goto error;
460+
}
461+
462+
if (PyList_Reverse(call_stack)) {
463+
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to reverse call stack");
464+
goto error;
465+
}
466466
}
467467
}
468468

@@ -506,33 +506,10 @@ parse_task(
506506
goto error;
507507
}
508508

509-
if (is_task) {
510-
result = create_task_result(unwinder, task_address);
511-
if (!result) {
512-
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create task result");
513-
goto error;
514-
}
515-
} else {
516-
// Create an empty CoroInfo for non-task objects
517-
RemoteDebuggingState *state = RemoteDebugging_GetStateFromObject((PyObject*)unwinder);
518-
result = PyStructSequence_New(state->CoroInfo_Type);
519-
if (result == NULL) {
520-
set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create empty CoroInfo");
521-
goto error;
522-
}
523-
PyObject *empty_list = PyList_New(0);
524-
if (empty_list == NULL) {
525-
set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create empty list");
526-
goto error;
527-
}
528-
PyObject *task_name = PyLong_FromUnsignedLongLong(task_address);
529-
if (task_name == NULL) {
530-
Py_DECREF(empty_list);
531-
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create task name");
532-
goto error;
533-
}
534-
PyStructSequence_SetItem(result, 0, empty_list); // This steals the reference
535-
PyStructSequence_SetItem(result, 1, task_name); // This steals the reference
509+
result = create_task_result(unwinder, task_address, is_task);
510+
if (result == NULL) {
511+
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create task result");
512+
goto error;
536513
}
537514
if (PyList_Append(render_to, result)) {
538515
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to append task result to render list");
@@ -798,14 +775,12 @@ parse_async_frame_chain(
798775
return -1;
799776
}
800777
PyObject* frame_info = NULL;
801-
uintptr_t address_of_code_object;
802778

803779
uintptr_t this_frame = address_of_current_frame;
804780
int res = parse_frame_object(
805781
unwinder,
806782
&frame_info,
807783
address_of_current_frame,
808-
&address_of_code_object,
809784
&address_of_current_frame
810785
);
811786

@@ -894,11 +869,11 @@ append_awaited_by_for_thread(
894869
return 0;
895870
}
896871

897-
int
898-
append_awaited_by(
872+
static int
873+
append_awaited_info(
899874
RemoteUnwinderObject *unwinder,
900875
unsigned long tid,
901-
uintptr_t head_addr,
876+
PyObject *task_list,
902877
PyObject *result)
903878
{
904879
PyObject *tid_py = PyLong_FromUnsignedLong(tid);
@@ -907,33 +882,47 @@ append_awaited_by(
907882
return -1;
908883
}
909884

910-
PyObject* awaited_by_for_thread = PyList_New(0);
911-
if (awaited_by_for_thread == NULL) {
912-
Py_DECREF(tid_py);
913-
set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create awaited_by thread list");
914-
return -1;
915-
}
916-
917885
RemoteDebuggingState *state = RemoteDebugging_GetStateFromObject((PyObject*)unwinder);
918886
PyObject *result_item = PyStructSequence_New(state->AwaitedInfo_Type);
919887
if (result_item == NULL) {
920888
Py_DECREF(tid_py);
921-
Py_DECREF(awaited_by_for_thread);
922889
set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create AwaitedInfo");
923890
return -1;
924891
}
925892

926-
PyStructSequence_SetItem(result_item, 0, tid_py); // steals ref
927-
PyStructSequence_SetItem(result_item, 1, awaited_by_for_thread); // steals ref
893+
PyStructSequence_SetItem(result_item, 0, tid_py);
894+
PyStructSequence_SetItem(result_item, 1, Py_NewRef(task_list));
928895
if (PyList_Append(result, result_item)) {
929896
Py_DECREF(result_item);
930897
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to append awaited_by result item");
931898
return -1;
932899
}
933900
Py_DECREF(result_item);
934901

935-
if (append_awaited_by_for_thread(unwinder, head_addr, awaited_by_for_thread))
936-
{
902+
return 0;
903+
}
904+
905+
int
906+
append_awaited_by(
907+
RemoteUnwinderObject *unwinder,
908+
unsigned long tid,
909+
uintptr_t head_addr,
910+
PyObject *result)
911+
{
912+
PyObject *task_list = PyList_New(0);
913+
if (task_list == NULL) {
914+
set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create awaited_by thread list");
915+
return -1;
916+
}
917+
918+
if (append_awaited_info(unwinder, tid, task_list, result) < 0) {
919+
Py_DECREF(task_list);
920+
return -1;
921+
}
922+
923+
int err = append_awaited_by_for_thread(unwinder, head_addr, task_list);
924+
Py_DECREF(task_list);
925+
if (err < 0) {
937926
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to append awaited_by for thread");
938927
return -1;
939928
}
@@ -1038,32 +1027,9 @@ process_thread_for_async_stack_trace(
10381027
return -1;
10391028
}
10401029

1041-
// Create AwaitedInfo structure for this thread
1042-
PyObject *tid_py = PyLong_FromUnsignedLong(tid);
1043-
if (tid_py == NULL) {
1044-
Py_DECREF(task_list);
1045-
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create thread ID");
1046-
return -1;
1047-
}
1048-
1049-
RemoteDebuggingState *state = RemoteDebugging_GetStateFromObject((PyObject*)unwinder);
1050-
PyObject *awaited_info = PyStructSequence_New(state->AwaitedInfo_Type);
1051-
if (awaited_info == NULL) {
1052-
Py_DECREF(tid_py);
1053-
Py_DECREF(task_list);
1054-
set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create AwaitedInfo");
1055-
return -1;
1056-
}
1057-
1058-
PyStructSequence_SetItem(awaited_info, 0, tid_py); // steals ref
1059-
PyStructSequence_SetItem(awaited_info, 1, task_list); // steals ref
1060-
1061-
if (PyList_Append(result, awaited_info)) {
1062-
Py_DECREF(awaited_info);
1063-
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to append AwaitedInfo to result");
1064-
return -1;
1065-
}
1066-
Py_DECREF(awaited_info);
1030+
int err = append_awaited_info(unwinder, tid, task_list, result);
1031+
Py_DECREF(task_list);
1032+
return err;
10671033
}
10681034

10691035
return 0;

0 commit comments

Comments
 (0)