From 355d6ab68bfcbfa1544c813e244b6ec9ece9c34b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Mon, 16 Nov 2026 13:38:02 +0100 Subject: [PATCH] _remote_debugging: Use PyBytesWriter in read_py_bytes() (GH-162110) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use PyBytesWriter to construct remote bytes objects. Remove the separate raw buffer allocation, copy, and cleanup. (cherry picked from commit be3dfe5b1890d43c71d6c58c76b65b0ce33eff57) Co-authored-by: Maurycy Pawłowski-Wieroński --- Lib/test/test_external_inspection.py | 11 ++++++++ Modules/_remote_debugging/object_reading.c | 29 ++++++---------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index 81e37fc73ceef44..7d6a0c890cff142 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -630,6 +630,17 @@ async def main(): stacks = self._extract_coroutine_funcnames(asyncio.run(main())) self.assertEqual(stacks["consumer"], ["consumer"]) + @skip_if_not_supported + def test_self_trace_with_empty_linetable(self): + def sample(): + return RemoteUnwinder(os.getpid()).get_stack_trace() + + sample.__code__ = sample.__code__.replace(co_linetable=b"") + frame = self._find_frame_in_trace( + sample(), lambda f: f.funcname == sample.__qualname__ + ) + self.assertEqual(frame.location, (-1, -1, -1, -1)) + @skip_if_not_supported @unittest.skipIf( sys.platform == "linux" and not PROCESS_VM_READV_SUPPORTED, diff --git a/Modules/_remote_debugging/object_reading.c b/Modules/_remote_debugging/object_reading.c index 6f138f3de3503b6..ed1f07c9143e0fc 100644 --- a/Modules/_remote_debugging/object_reading.c +++ b/Modules/_remote_debugging/object_reading.c @@ -185,9 +185,6 @@ read_py_bytes( uintptr_t address, Py_ssize_t max_len ) { - PyObject *result = NULL; - char *buf = NULL; - // Read the entire PyBytesObject at once char bytes_obj[SIZEOF_BYTES_OBJ]; int res = _Py_RemoteDebug_PagedReadRemoteMemory( @@ -198,7 +195,7 @@ read_py_bytes( ); if (res < 0) { set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to read PyBytesObject"); - goto err; + return NULL; } Py_ssize_t len = GET_MEMBER(Py_ssize_t, bytes_obj, unwinder->debug_offsets.bytes_object.ob_size); @@ -209,36 +206,26 @@ read_py_bytes( return NULL; } - buf = (char *)PyMem_RawMalloc(len+1); - if (buf == NULL) { - PyErr_NoMemory(); + PyBytesWriter *writer = PyBytesWriter_Create(len); + if (writer == NULL) { set_exception_cause(unwinder, PyExc_MemoryError, "Failed to allocate buffer for bytes reading"); return NULL; } size_t offset = (size_t)unwinder->debug_offsets.bytes_object.ob_sval; - res = _Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, address + offset, len, buf); + res = _Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, address + offset, + len, PyBytesWriter_GetData(writer)); if (res < 0) { + PyBytesWriter_Discard(writer); set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to read bytes data from remote memory"); - goto err; + return NULL; } - buf[len] = '\0'; - result = PyBytes_FromStringAndSize(buf, len); + PyObject *result = PyBytesWriter_Finish(writer); if (result == NULL) { set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create PyBytes from remote bytes data"); - goto err; } - - PyMem_RawFree(buf); - assert(result != NULL); return result; - -err: - if (buf != NULL) { - PyMem_RawFree(buf); - } - return NULL; } long