Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9284053

Browse files
committed
Following issue #13390, fix compilation --without-pymalloc, and make sys.getallocatedblocks() return 0 in that situation.
1 parent 3438fa4 commit 9284053

3 files changed

Lines changed: 21 additions & 8 deletions

File tree

Doc/library/sys.rst

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -396,15 +396,16 @@ always available.
396396
.. function:: getallocatedblocks()
397397

398398
Return the number of memory blocks currently allocated by the interpreter,
399-
regardless of their size. This function is mainly useful for debugging
400-
small memory leaks. Because of the interpreter's internal caches, the
401-
result can vary from call to call; you may have to call
402-
:func:`_clear_type_cache()` to get more predictable results.
399+
regardless of their size. This function is mainly useful for tracking
400+
and debugging memory leaks. Because of the interpreter's internal
401+
caches, the result can vary from call to call; you may have to call
402+
:func:`_clear_type_cache()` and :func:`gc.collect()` to get more
403+
predictable results.
403404

404-
.. versionadded:: 3.4
405+
If a Python build or implementation cannot reasonably compute this
406+
information, :func:`getallocatedblocks()` is allowed to return 0 instead.
405407

406-
.. impl-detail::
407-
Not all Python implementations may be able to return this information.
408+
.. versionadded:: 3.4
408409

409410

410411
.. function:: getcheckinterval()

Lib/test/test_sys.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import operator
88
import codecs
99
import gc
10+
import sysconfig
1011

1112
# count the number of test runs, used to create unique
1213
# strings to intern in test_intern()
@@ -616,9 +617,13 @@ def test_debugmallocstats(self):
616617
"sys.getallocatedblocks unavailable on this build")
617618
def test_getallocatedblocks(self):
618619
# Some sanity checks
620+
with_pymalloc = sysconfig.get_config_var('WITH_PYMALLOC')
619621
a = sys.getallocatedblocks()
620622
self.assertIs(type(a), int)
621-
self.assertGreater(a, 0)
623+
if with_pymalloc:
624+
self.assertGreater(a, 0)
625+
else:
626+
self.assertEqual(a, 0)
622627
try:
623628
# While we could imagine a Python session where the number of
624629
# multiple buffer objects would exceed the sharing of references,

Objects/obmalloc.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,13 @@ PyObject_Free(void *p)
13161316
{
13171317
PyMem_FREE(p);
13181318
}
1319+
1320+
Py_ssize_t
1321+
_Py_GetAllocatedBlocks(void)
1322+
{
1323+
return 0;
1324+
}
1325+
13191326
#endif /* WITH_PYMALLOC */
13201327

13211328
#ifdef PYMALLOC_DEBUG

0 commit comments

Comments
 (0)