gh-91048: Chain some exceptions in _testexternalinspection.c by sergey-miryanov · Pull Request #132970 · python/cpython
// Helper to chain exceptions and avoid repetitions static void chain_exceptions(PyObject *exception, const char *string) { PyObject *exc = PyErr_GetRaisedException(); PyErr_SetString(exception, string); _PyErr_ChainExceptions1(exc); }
// Get the PyAsyncioDebug section address for any platform static uintptr_t _Py_RemoteDebug_GetAsyncioDebugAddress(proc_handle_t* handle)
return address;
uintptr_t refcnt; read_ptr(handle, task_address + sizeof(Py_ssize_t), &refcnt);
PyObject* result = PyList_New(0); if (result == NULL) { return -1;
PyObject *result = NULL;
uintptr_t runtime_start_addr = _Py_RemoteDebug_GetPyRuntimeAddress(handle); if (runtime_start_addr == 0) { if (!PyErr_Occurred()) { PyErr_SetString( PyExc_RuntimeError, "Failed to get .PyRuntime address"); } return NULL; goto result_err; } struct _Py_DebugOffsets local_debug_offsets;
if (_Py_RemoteDebug_ReadDebugOffsets(handle, &runtime_start_addr, &local_debug_offsets)) { PyErr_SetString(PyExc_RuntimeError, "Failed to read debug offsets"); return NULL; chain_exceptions(PyExc_RuntimeError, "Failed to read debug offsets"); goto result_err; }
struct _Py_AsyncioModuleDebugOffsets local_async_debug; if (read_async_debug(handle, &local_async_debug)) { PyErr_SetString(PyExc_RuntimeError, "Failed to read asyncio debug offsets"); return NULL; chain_exceptions(PyExc_RuntimeError, "Failed to read asyncio debug offsets"); goto result_err; }
PyObject *result = PyList_New(0); result = PyList_New(0); if (result == NULL) { return NULL; goto result_err; }
uint64_t interpreter_state_list_head =
result_err: Py_DECREF(result); Py_XDECREF(result); _Py_RemoteDebug_CleanupProcHandle(handle); return NULL; }
if (_Py_RemoteDebug_ReadDebugOffsets(handle, &runtime_start_address, &local_debug_offsets)) { PyErr_SetString(PyExc_RuntimeError, "Failed to read debug offsets"); chain_exceptions(PyExc_RuntimeError, "Failed to read debug offsets"); goto result_err; }
PyObject *result = NULL;
uintptr_t runtime_start_address = _Py_RemoteDebug_GetPyRuntimeAddress(handle); if (runtime_start_address == 0) { if (!PyErr_Occurred()) { PyErr_SetString( PyExc_RuntimeError, "Failed to get .PyRuntime address"); } return NULL; goto result_err; } struct _Py_DebugOffsets local_debug_offsets;
if (_Py_RemoteDebug_ReadDebugOffsets(handle, &runtime_start_address, &local_debug_offsets)) { PyErr_SetString(PyExc_RuntimeError, "Failed to read debug offsets"); return NULL; chain_exceptions(PyExc_RuntimeError, "Failed to read debug offsets"); goto result_err; }
struct _Py_AsyncioModuleDebugOffsets local_async_debug; if (read_async_debug(handle, &local_async_debug)) { PyErr_SetString(PyExc_RuntimeError, "Failed to read asyncio debug offsets"); return NULL; chain_exceptions(PyExc_RuntimeError, "Failed to read asyncio debug offsets"); goto result_err; }
PyObject* result = PyList_New(1); result = PyList_New(1); if (result == NULL) { return NULL; goto result_err; } PyObject* calls = PyList_New(0); if (calls == NULL) { Py_DECREF(result); return NULL; goto result_err; } if (PyList_SetItem(result, 0, calls)) { /* steals ref to 'calls' */ Py_DECREF(result); Py_DECREF(calls); return NULL; goto result_err; }
uintptr_t running_task_addr = (uintptr_t)NULL; if (find_running_task( handle, runtime_start_address, &local_debug_offsets, &local_async_debug, &running_task_addr) ) { PyErr_SetString(PyExc_RuntimeError, "Failed to find running task"); chain_exceptions(PyExc_RuntimeError, "Failed to find running task"); goto result_err; }
if (res < 0) { PyErr_SetString(PyExc_RuntimeError, "Failed to parse async frame object"); chain_exceptions(PyExc_RuntimeError, "Failed to parse async frame object"); goto result_err; }
result_err: _Py_RemoteDebug_CleanupProcHandle(handle); Py_DECREF(result); Py_XDECREF(result); return NULL; }