Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Replace macro with static function
  • Loading branch information
sergey-miryanov committed Apr 25, 2025
commit e7183302c4b95f4221bd08c9d91b31a0d24eea12
32 changes: 10 additions & 22 deletions Modules/_testexternalinspection.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,6 @@
# define HAVE_PROCESS_VM_READV 0
#endif

#ifdef CHAIN_EXCEPTIONS
#error "CHAIN_EXCEPTIONS should not be defined"
#endif

#define CHAIN_EXCEPTIONS(Type, Msg) \
do { \
PyObject *exc = PyErr_GetRaisedException(); \
PyErr_SetString((Type), (Msg)); \
_PyErr_ChainExceptions1(exc); \
} while(0)


struct _Py_AsyncioModuleDebugOffsets {
struct _asyncio_task_object {
uint64_t size;
Expand Down Expand Up @@ -316,7 +304,7 @@ parse_task_name(
if ((flags & Py_TPFLAGS_LONG_SUBCLASS)) {
long res = read_py_long(handle, offsets, task_name_addr);
if (res == -1) {
CHAIN_EXCEPTIONS(PyExc_RuntimeError, "Failed to get task name");
chain_exceptions(PyExc_RuntimeError, "Failed to get task name");
return NULL;
}
return PyUnicode_FromFormat("Task-%d", res);
Expand Down Expand Up @@ -1181,13 +1169,13 @@ get_all_awaited_by(PyObject* self, PyObject* args)
struct _Py_DebugOffsets local_debug_offsets;

if (_Py_RemoteDebug_ReadDebugOffsets(handle, &runtime_start_addr, &local_debug_offsets)) {
CHAIN_EXCEPTIONS(PyExc_RuntimeError, "Failed to read debug offsets");
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)) {
CHAIN_EXCEPTIONS(PyExc_RuntimeError, "Failed to read asyncio debug offsets");
chain_exceptions(PyExc_RuntimeError, "Failed to read asyncio debug offsets");
goto result_err;
}

Expand Down Expand Up @@ -1310,7 +1298,7 @@ get_stack_trace(PyObject* self, PyObject* args)
struct _Py_DebugOffsets local_debug_offsets;

if (_Py_RemoteDebug_ReadDebugOffsets(handle, &runtime_start_address, &local_debug_offsets)) {
CHAIN_EXCEPTIONS(PyExc_RuntimeError, "Failed to read debug offsets");
chain_exceptions(PyExc_RuntimeError, "Failed to read debug offsets");
goto result_err;
}

Expand Down Expand Up @@ -1381,13 +1369,13 @@ get_async_stack_trace(PyObject* self, PyObject* args)
struct _Py_DebugOffsets local_debug_offsets;

if (_Py_RemoteDebug_ReadDebugOffsets(handle, &runtime_start_address, &local_debug_offsets)) {
CHAIN_EXCEPTIONS(PyExc_RuntimeError, "Failed to read debug offsets");
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)) {
CHAIN_EXCEPTIONS(PyExc_RuntimeError, "Failed to read asyncio debug offsets");
chain_exceptions(PyExc_RuntimeError, "Failed to read asyncio debug offsets");
goto result_err;
}

Expand All @@ -1409,7 +1397,7 @@ get_async_stack_trace(PyObject* self, PyObject* args)
handle, runtime_start_address, &local_debug_offsets, &local_async_debug,
&running_task_addr)
) {
CHAIN_EXCEPTIONS(PyExc_RuntimeError, "Failed to find running task");
chain_exceptions(PyExc_RuntimeError, "Failed to find running task");
goto result_err;
}

Expand All @@ -1424,7 +1412,7 @@ get_async_stack_trace(PyObject* self, PyObject* args)
running_task_addr + local_async_debug.asyncio_task_object.task_coro,
&running_coro_addr
)) {
CHAIN_EXCEPTIONS(PyExc_RuntimeError, "Failed to read running task coro");
chain_exceptions(PyExc_RuntimeError, "Failed to read running task coro");
goto result_err;
}

Expand Down Expand Up @@ -1454,7 +1442,7 @@ get_async_stack_trace(PyObject* self, PyObject* args)
handle, runtime_start_address, &local_debug_offsets,
&address_of_current_frame)
) {
CHAIN_EXCEPTIONS(PyExc_RuntimeError, "Failed to find running frame");
chain_exceptions(PyExc_RuntimeError, "Failed to find running frame");
goto result_err;
}

Expand All @@ -1470,7 +1458,7 @@ get_async_stack_trace(PyObject* self, PyObject* args)
);

if (res < 0) {
CHAIN_EXCEPTIONS(PyExc_RuntimeError, "Failed to parse async frame object");
chain_exceptions(PyExc_RuntimeError, "Failed to parse async frame object");
goto result_err;
}

Expand Down
17 changes: 11 additions & 6 deletions Python/remote_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ _Py_RemoteDebug_CleanupProcHandle(proc_handle_t *handle) {
handle->pid = 0;
}

// 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);
}

#if defined(__APPLE__) && TARGET_OS_OSX

static uintptr_t
Expand Down Expand Up @@ -688,18 +697,14 @@ _Py_RemoteDebug_GetPyRuntimeAddress(proc_handle_t* handle)
address = search_windows_map_for_section(handle, "PyRuntime", L"python");
if (address == 0) {
// Error out: 'python' substring covers both executable and DLL
PyObject *exc = PyErr_GetRaisedException();
PyErr_SetString(PyExc_RuntimeError, "Failed to find the PyRuntime section in the process.");
_PyErr_ChainExceptions1(exc);
chain_exceptions(PyExc_RuntimeError, "Failed to find the PyRuntime section in the process.");
}
#elif defined(__linux__)
// On Linux, search for 'python' in executable or DLL
address = search_linux_map_for_section(handle, "PyRuntime", "python");
if (address == 0) {
// Error out: 'python' substring covers both executable and DLL
PyObject *exc = PyErr_GetRaisedException();
PyErr_SetString(PyExc_RuntimeError, "Failed to find the PyRuntime section in the process.");
_PyErr_ChainExceptions1(exc);
chain_exceptions(PyExc_RuntimeError, "Failed to find the PyRuntime section in the process.");
}
#elif defined(__APPLE__) && TARGET_OS_OSX
// On macOS, try libpython first, then fall back to python
Expand Down
Loading