Skip to content

Commit d449234

Browse files
[3.15] _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. (cherry picked from commit be3dfe5) Co-authored-by: Maurycy Pawłowski-Wieroński <maurycy@maurycy.com>
1 parent b606bdc commit d449234

2 files changed

Lines changed: 19 additions & 21 deletions

File tree

‎Lib/test/test_external_inspection.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,17 @@ async def main():
647647
stacks = self._extract_coroutine_funcnames(asyncio.run(main()))
648648
self.assertEqual(stacks["consumer"], ["consumer"])
649649

650+
@skip_if_not_supported
651+
def test_self_trace_with_empty_linetable(self):
652+
def sample():
653+
return RemoteUnwinder(os.getpid()).get_stack_trace()
654+
655+
sample.__code__ = sample.__code__.replace(co_linetable=b"")
656+
frame = self._find_frame_in_trace(
657+
sample(), lambda f: f.funcname == sample.__qualname__
658+
)
659+
self.assertEqual(frame.location, (-1, -1, -1, -1))
660+
650661
@skip_if_not_supported
651662
@unittest.skipIf(
652663
sys.platform == "linux" and not PROCESS_VM_READV_SUPPORTED,

‎Modules/_remote_debugging/object_reading.c‎

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,6 @@ read_py_bytes(
185185
uintptr_t address,
186186
Py_ssize_t max_len
187187
) {
188-
PyObject *result = NULL;
189-
char *buf = NULL;
190-
191188
// Read the entire PyBytesObject at once
192189
char bytes_obj[SIZEOF_BYTES_OBJ];
193190
int res = _Py_RemoteDebug_PagedReadRemoteMemory(
@@ -198,7 +195,7 @@ read_py_bytes(
198195
);
199196
if (res < 0) {
200197
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to read PyBytesObject");
201-
goto err;
198+
return NULL;
202199
}
203200

204201
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(
209206
return NULL;
210207
}
211208

212-
buf = (char *)PyMem_RawMalloc(len+1);
213-
if (buf == NULL) {
214-
PyErr_NoMemory();
209+
PyBytesWriter *writer = PyBytesWriter_Create(len);
210+
if (writer == NULL) {
215211
set_exception_cause(unwinder, PyExc_MemoryError, "Failed to allocate buffer for bytes reading");
216212
return NULL;
217213
}
218214

219215
size_t offset = (size_t)unwinder->debug_offsets.bytes_object.ob_sval;
220-
res = _Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, address + offset, len, buf);
216+
res = _Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, address + offset,
217+
len, PyBytesWriter_GetData(writer));
221218
if (res < 0) {
219+
PyBytesWriter_Discard(writer);
222220
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to read bytes data from remote memory");
223-
goto err;
221+
return NULL;
224222
}
225-
buf[len] = '\0';
226223

227-
result = PyBytes_FromStringAndSize(buf, len);
224+
PyObject *result = PyBytesWriter_Finish(writer);
228225
if (result == NULL) {
229226
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create PyBytes from remote bytes data");
230-
goto err;
231227
}
232-
233-
PyMem_RawFree(buf);
234-
assert(result != NULL);
235228
return result;
236-
237-
err:
238-
if (buf != NULL) {
239-
PyMem_RawFree(buf);
240-
}
241-
return NULL;
242229
}
243230

244231
long

0 commit comments

Comments
 (0)