Skip to content

Commit d57a3e5

Browse files
authored
bpo-29564:_PyMem_DumpTraceback() suggests enabling tracemalloc (pythonGH-10510) (pythonGH-10517)
If tracemalloc is not tracing Python memory allocations, _PyMem_DumpTraceback() now suggests to enable tracemalloc to get the traceback where the memory block has been allocated.
1 parent 55b4771 commit d57a3e5

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

Lib/test/test_capi.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,8 @@ def test_buffer_overflow(self):
468468
r" The block was made by call #[0-9]+ to debug malloc/realloc.\n"
469469
r" Data at p: cb cb cb .*\n"
470470
r"\n"
471+
r"Enable tracemalloc to get the memory block allocation traceback\n"
472+
r"\n"
471473
r"Fatal Python error: bad trailing pad byte")
472474
regex = regex.format(ptr=self.PTR_REGEX)
473475
regex = re.compile(regex, flags=re.DOTALL)
@@ -482,6 +484,8 @@ def test_api_misuse(self):
482484
r" The block was made by call #[0-9]+ to debug malloc/realloc.\n"
483485
r" Data at p: cb cb cb .*\n"
484486
r"\n"
487+
r"Enable tracemalloc to get the memory block allocation traceback\n"
488+
r"\n"
485489
r"Fatal Python error: bad ID: Allocated using API 'm', verified using API 'r'\n")
486490
regex = regex.format(ptr=self.PTR_REGEX)
487491
self.assertRegex(out, regex)

Modules/_tracemalloc.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,12 @@ _PyMem_DumpTraceback(int fd, const void *ptr)
14901490
traceback_t *traceback;
14911491
int i;
14921492

1493+
if (!tracemalloc_config.tracing) {
1494+
PUTS(fd, "Enable tracemalloc to get the memory block "
1495+
"allocation traceback\n\n");
1496+
return;
1497+
}
1498+
14931499
traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, (uintptr_t)ptr);
14941500
if (traceback == NULL)
14951501
return;

0 commit comments

Comments
 (0)