@@ -45,6 +45,15 @@ struct _Py_AsyncioModuleDebugOffsets {
45
45
} asyncio_thread_state ;
46
46
};
47
47
48
+ // Helper to chain exceptions and avoid repetitions
49
+ static void
50
+ chain_exceptions (PyObject * exception , const char * string )
51
+ {
52
+ PyObject * exc = PyErr_GetRaisedException ();
53
+ PyErr_SetString (exception , string );
54
+ _PyErr_ChainExceptions1 (exc );
55
+ }
56
+
48
57
// Get the PyAsyncioDebug section address for any platform
49
58
static uintptr_t
50
59
_Py_RemoteDebug_GetAsyncioDebugAddress (proc_handle_t * handle )
@@ -65,7 +74,7 @@ _Py_RemoteDebug_GetAsyncioDebugAddress(proc_handle_t* handle)
65
74
address = search_map_for_section (handle , "AsyncioDebug" , "_asyncio.cpython" );
66
75
}
67
76
#else
68
- address = 0 ;
77
+ Py_UNREACHABLE () ;
69
78
#endif
70
79
71
80
return address ;
@@ -304,7 +313,7 @@ parse_task_name(
304
313
if ((flags & Py_TPFLAGS_LONG_SUBCLASS )) {
305
314
long res = read_py_long (handle , offsets , task_name_addr );
306
315
if (res == -1 ) {
307
- PyErr_SetString (PyExc_RuntimeError , "Failed to get task name" );
316
+ chain_exceptions (PyExc_RuntimeError , "Failed to get task name" );
308
317
return NULL ;
309
318
}
310
319
return PyUnicode_FromFormat ("Task-%d" , res );
@@ -482,9 +491,6 @@ parse_task(
482
491
return -1 ;
483
492
}
484
493
485
- uintptr_t refcnt ;
486
- read_ptr (handle , task_address + sizeof (Py_ssize_t ), & refcnt );
487
-
488
494
PyObject * result = PyList_New (0 );
489
495
if (result == NULL ) {
490
496
return -1 ;
@@ -1159,30 +1165,32 @@ get_all_awaited_by(PyObject* self, PyObject* args)
1159
1165
return 0 ;
1160
1166
}
1161
1167
1168
+ PyObject * result = NULL ;
1169
+
1162
1170
uintptr_t runtime_start_addr = _Py_RemoteDebug_GetPyRuntimeAddress (handle );
1163
1171
if (runtime_start_addr == 0 ) {
1164
1172
if (!PyErr_Occurred ()) {
1165
1173
PyErr_SetString (
1166
1174
PyExc_RuntimeError , "Failed to get .PyRuntime address" );
1167
1175
}
1168
- return NULL ;
1176
+ goto result_err ;
1169
1177
}
1170
1178
struct _Py_DebugOffsets local_debug_offsets ;
1171
1179
1172
1180
if (_Py_RemoteDebug_ReadDebugOffsets (handle , & runtime_start_addr , & local_debug_offsets )) {
1173
- PyErr_SetString (PyExc_RuntimeError , "Failed to read debug offsets" );
1174
- return NULL ;
1181
+ chain_exceptions (PyExc_RuntimeError , "Failed to read debug offsets" );
1182
+ goto result_err ;
1175
1183
}
1176
1184
1177
1185
struct _Py_AsyncioModuleDebugOffsets local_async_debug ;
1178
1186
if (read_async_debug (handle , & local_async_debug )) {
1179
- PyErr_SetString (PyExc_RuntimeError , "Failed to read asyncio debug offsets" );
1180
- return NULL ;
1187
+ chain_exceptions (PyExc_RuntimeError , "Failed to read asyncio debug offsets" );
1188
+ goto result_err ;
1181
1189
}
1182
1190
1183
- PyObject * result = PyList_New (0 );
1191
+ result = PyList_New (0 );
1184
1192
if (result == NULL ) {
1185
- return NULL ;
1193
+ goto result_err ;
1186
1194
}
1187
1195
1188
1196
uint64_t interpreter_state_list_head =
@@ -1259,7 +1267,7 @@ get_all_awaited_by(PyObject* self, PyObject* args)
1259
1267
return result ;
1260
1268
1261
1269
result_err :
1262
- Py_DECREF (result );
1270
+ Py_XDECREF (result );
1263
1271
_Py_RemoteDebug_CleanupProcHandle (handle );
1264
1272
return NULL ;
1265
1273
}
@@ -1299,7 +1307,7 @@ get_stack_trace(PyObject* self, PyObject* args)
1299
1307
struct _Py_DebugOffsets local_debug_offsets ;
1300
1308
1301
1309
if (_Py_RemoteDebug_ReadDebugOffsets (handle , & runtime_start_address , & local_debug_offsets )) {
1302
- PyErr_SetString (PyExc_RuntimeError , "Failed to read debug offsets" );
1310
+ chain_exceptions (PyExc_RuntimeError , "Failed to read debug offsets" );
1303
1311
goto result_err ;
1304
1312
}
1305
1313
@@ -1357,48 +1365,48 @@ get_async_stack_trace(PyObject* self, PyObject* args)
1357
1365
return 0 ;
1358
1366
}
1359
1367
1368
+ PyObject * result = NULL ;
1369
+
1360
1370
uintptr_t runtime_start_address = _Py_RemoteDebug_GetPyRuntimeAddress (handle );
1361
1371
if (runtime_start_address == 0 ) {
1362
1372
if (!PyErr_Occurred ()) {
1363
1373
PyErr_SetString (
1364
1374
PyExc_RuntimeError , "Failed to get .PyRuntime address" );
1365
1375
}
1366
- return NULL ;
1376
+ goto result_err ;
1367
1377
}
1368
1378
struct _Py_DebugOffsets local_debug_offsets ;
1369
1379
1370
1380
if (_Py_RemoteDebug_ReadDebugOffsets (handle , & runtime_start_address , & local_debug_offsets )) {
1371
- PyErr_SetString (PyExc_RuntimeError , "Failed to read debug offsets" );
1372
- return NULL ;
1381
+ chain_exceptions (PyExc_RuntimeError , "Failed to read debug offsets" );
1382
+ goto result_err ;
1373
1383
}
1374
1384
1375
1385
struct _Py_AsyncioModuleDebugOffsets local_async_debug ;
1376
1386
if (read_async_debug (handle , & local_async_debug )) {
1377
- PyErr_SetString (PyExc_RuntimeError , "Failed to read asyncio debug offsets" );
1378
- return NULL ;
1387
+ chain_exceptions (PyExc_RuntimeError , "Failed to read asyncio debug offsets" );
1388
+ goto result_err ;
1379
1389
}
1380
1390
1381
- PyObject * result = PyList_New (1 );
1391
+ result = PyList_New (1 );
1382
1392
if (result == NULL ) {
1383
- return NULL ;
1393
+ goto result_err ;
1384
1394
}
1385
1395
PyObject * calls = PyList_New (0 );
1386
1396
if (calls == NULL ) {
1387
- Py_DECREF (result );
1388
- return NULL ;
1397
+ goto result_err ;
1389
1398
}
1390
1399
if (PyList_SetItem (result , 0 , calls )) { /* steals ref to 'calls' */
1391
- Py_DECREF (result );
1392
1400
Py_DECREF (calls );
1393
- return NULL ;
1401
+ goto result_err ;
1394
1402
}
1395
1403
1396
1404
uintptr_t running_task_addr = (uintptr_t )NULL ;
1397
1405
if (find_running_task (
1398
1406
handle , runtime_start_address , & local_debug_offsets , & local_async_debug ,
1399
1407
& running_task_addr )
1400
1408
) {
1401
- PyErr_SetString (PyExc_RuntimeError , "Failed to find running task" );
1409
+ chain_exceptions (PyExc_RuntimeError , "Failed to find running task" );
1402
1410
goto result_err ;
1403
1411
}
1404
1412
@@ -1413,7 +1421,7 @@ get_async_stack_trace(PyObject* self, PyObject* args)
1413
1421
running_task_addr + local_async_debug .asyncio_task_object .task_coro ,
1414
1422
& running_coro_addr
1415
1423
)) {
1416
- PyErr_SetString (PyExc_RuntimeError , "Failed to read running task coro" );
1424
+ chain_exceptions (PyExc_RuntimeError , "Failed to read running task coro" );
1417
1425
goto result_err ;
1418
1426
}
1419
1427
@@ -1443,7 +1451,7 @@ get_async_stack_trace(PyObject* self, PyObject* args)
1443
1451
handle , runtime_start_address , & local_debug_offsets ,
1444
1452
& address_of_current_frame )
1445
1453
) {
1446
- PyErr_SetString (PyExc_RuntimeError , "Failed to find running frame" );
1454
+ chain_exceptions (PyExc_RuntimeError , "Failed to find running frame" );
1447
1455
goto result_err ;
1448
1456
}
1449
1457
@@ -1459,7 +1467,7 @@ get_async_stack_trace(PyObject* self, PyObject* args)
1459
1467
);
1460
1468
1461
1469
if (res < 0 ) {
1462
- PyErr_SetString (PyExc_RuntimeError , "Failed to parse async frame object" );
1470
+ chain_exceptions (PyExc_RuntimeError , "Failed to parse async frame object" );
1463
1471
goto result_err ;
1464
1472
}
1465
1473
@@ -1501,7 +1509,7 @@ get_async_stack_trace(PyObject* self, PyObject* args)
1501
1509
1502
1510
result_err :
1503
1511
_Py_RemoteDebug_CleanupProcHandle (handle );
1504
- Py_DECREF (result );
1512
+ Py_XDECREF (result );
1505
1513
return NULL ;
1506
1514
}
1507
1515
0 commit comments