https://github.com/python/cpython/commit/be3dfe5b1890d43c71d6c58c76b65b0ce33eff57
commit: be3dfe5b1890d43c71d6c58c76b65b0ce33eff57
branch: main
author: Maurycy Pawłowski-Wieroński <[email protected]>
committer: pablogsal <[email protected]>
date: 2026-11-16T12:38:02Z
summary:

_remote_debugging: Use PyBytesWriter in read_py_bytes() (GH-162110)

Use PyBytesWriter to construct remote bytes objects. Remove the separate raw 
buffer allocation, copy, and cleanup.

files:
M Lib/test/test_external_inspection.py
M Modules/_remote_debugging/object_reading.c

diff --git a/Lib/test/test_external_inspection.py 
b/Lib/test/test_external_inspection.py
index cb9aae3345cff9..5aebe1fb6b18e9 100644
--- a/Lib/test/test_external_inspection.py
+++ b/Lib/test/test_external_inspection.py
@@ -696,6 +696,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 6f138f3de3503b..ed1f07c9143e0f 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

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to