Skip to content

Commit 7b4ba62

Browse files
authored
[2.7] bpo-31692: Add PYTHONSHOWALLOCCOUNT env var (GH-3927)
bpo-31692, bpo-19527: * Add a new PYTHONSHOWALLOCCOUNT environment variable, similar to the Python 3 "-X showalloccount" option * When Python is compiled with COUNT_ALLOCS, the new PYTHONSHOWALLOCCOUNT environment variable now has to be set to dump allocation counts into stderr on shutdown. Moreover, allocations statistics are now dumped into stderr rather than stdout. * Add @test.support.requires_type_collecting decorator: skip test if COUNT_ALLOCS is defined * Fix tests for COUNT_ALLOCS: decorate some methods with @requires_type_collecting * test_sys.test_objecttypes(): update object type when COUNT_ALLOCS is defined
1 parent 3c082a7 commit 7b4ba62

File tree

10 files changed

+28
-4
lines changed

10 files changed

+28
-4
lines changed

Doc/c-api/typeobj.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ The next fields, up to and including :c:member:`~PyTypeObject.tp_weaklist`, only
11011101
The remaining fields are only defined if the feature test macro
11021102
:const:`COUNT_ALLOCS` is defined, and are for internal use only. They are
11031103
documented here for completeness. None of these fields are inherited by
1104-
subtypes.
1104+
subtypes. See the :envvar:`PYTHONSHOWALLOCCOUNT` environment variable.
11051105

11061106

11071107
.. c:member:: Py_ssize_t PyTypeObject.tp_allocs

Doc/using/cmdline.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,13 @@ if Python was configured with the ``--with-pydebug`` build option.
664664
If set, Python will print memory allocation statistics every time a new
665665
object arena is created, and on shutdown.
666666

667+
.. envvar:: PYTHONSHOWALLOCCOUNT
668+
669+
If set and Python was compiled with ``COUNT_ALLOCS`` defined, Python will
670+
dump allocations counts into stderr on shutdown.
671+
672+
.. versionadded:: 2.7.15
673+
667674
.. envvar:: PYTHONSHOWREFCOUNT
668675

669676
If set, Python will print the total reference count when the program

Lib/test/support/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,9 @@ def py3k_bytes(b):
17951795
except TypeError:
17961796
return bytes(b)
17971797

1798+
requires_type_collecting = unittest.skipIf(hasattr(sys, 'getcounts'),
1799+
'types are immortal if COUNT_ALLOCS is defined')
1800+
17981801
def args_from_interpreter_flags():
17991802
"""Return a list of command-line arguments reproducing the current
18001803
settings in sys.flags."""

Lib/test/test_abc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ class C(A, B):
208208
C()
209209
self.assertEqual(B.counter, 1)
210210

211+
@test_support.requires_type_collecting
211212
def test_cache_leak(self):
212213
# See issue #2521.
213214
class A(object):

Lib/test/test_gc.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
2-
from test.test_support import verbose, run_unittest, start_threads
2+
from test.support import (verbose, run_unittest, start_threads,
3+
requires_type_collecting)
34
import sys
45
import time
56
import gc
@@ -90,6 +91,7 @@ class A:
9091
del a
9192
self.assertNotEqual(gc.collect(), 0)
9293

94+
@requires_type_collecting
9395
def test_newinstance(self):
9496
class A(object):
9597
pass

Lib/test/test_regrtest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ def check_leak(self, code, what):
493493
self.assertIn(line2, reflog)
494494

495495
@unittest.skipUnless(Py_DEBUG, 'need a debug build')
496+
@support.requires_type_collecting
496497
def test_huntrleaks(self):
497498
# test --huntrleaks
498499
code = textwrap.dedent("""

Lib/test/test_sys.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,10 @@ def delx(self): del self.__x
748748
# tupleiterator
749749
check(iter(()), size('lP'))
750750
# type
751-
s = vsize('P2P15Pl4PP9PP11PI' # PyTypeObject
751+
fmt = 'P2P15Pl4PP9PP11PI'
752+
if hasattr(sys, 'getcounts'):
753+
fmt += '3P2P'
754+
s = vsize(fmt + # PyTypeObject
752755
'39P' # PyNumberMethods
753756
'3P' # PyMappingMethods
754757
'10P' # PySequenceMethods

Lib/test/test_weakref.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ class D:
601601
del c1, c2, C, D
602602
gc.collect()
603603

604+
@test_support.requires_type_collecting
604605
def test_callback_in_cycle_resurrection(self):
605606
import gc
606607

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Add a new PYTHONSHOWALLOCCOUNT environment variable. When Python is compiled
2+
with COUNT_ALLOCS, PYTHONSHOWALLOCCOUNT now has to be set to dump allocation
3+
counts into stderr on shutdown. Moreover, allocations statistics are now dumped
4+
into stderr rather than stdout.

Python/pythonrun.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,9 @@ Py_Finalize(void)
488488

489489
/* Debugging stuff */
490490
#ifdef COUNT_ALLOCS
491-
dump_counts(stdout);
491+
if (Py_GETENV("PYTHONSHOWALLOCCOUNT")) {
492+
dump_counts(stderr);
493+
}
492494
#endif
493495

494496
_PyDebug_PrintTotalRefs();

0 commit comments

Comments
 (0)