]> granicus.if.org Git - python/commitdiff
[2.7] bpo-31692: Add PYTHONSHOWALLOCCOUNT env var (GH-3927)
authorVictor Stinner <victor.stinner@gmail.com>
Tue, 17 Oct 2017 09:25:23 +0000 (02:25 -0700)
committerGitHub <noreply@github.com>
Tue, 17 Oct 2017 09:25:23 +0000 (02:25 -0700)
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

Doc/c-api/typeobj.rst
Doc/using/cmdline.rst
Lib/test/support/__init__.py
Lib/test/test_abc.py
Lib/test/test_gc.py
Lib/test/test_regrtest.py
Lib/test/test_sys.py
Lib/test/test_weakref.py
Misc/NEWS.d/next/Core and Builtins/2017-10-09-11-03-13.bpo-31692.5-bpdk.rst [new file with mode: 0644]
Python/pythonrun.c

index 18edcdd7e5aa4899573c628a069fbf05c3f815b1..f0ccf2ea5fe94c1ac58e6fcbc24ea04e480b632c 100644 (file)
@@ -1101,7 +1101,7 @@ The next fields, up to and including :c:member:`~PyTypeObject.tp_weaklist`, only
 The remaining fields are only defined if the feature test macro
 :const:`COUNT_ALLOCS` is defined, and are for internal use only. They are
 documented here for completeness.  None of these fields are inherited by
-subtypes.
+subtypes. See the :envvar:`PYTHONSHOWALLOCCOUNT` environment variable.
 
 
 .. c:member:: Py_ssize_t PyTypeObject.tp_allocs
index 65988bc32c162d45bdab01fd0a75b9741c6d22ac..c35bb34f93421a718963db5da45588c54b05dda3 100644 (file)
@@ -664,6 +664,13 @@ if Python was configured with the ``--with-pydebug`` build option.
    If set, Python will print memory allocation statistics every time a new
    object arena is created, and on shutdown.
 
+.. envvar:: PYTHONSHOWALLOCCOUNT
+
+   If set and Python was compiled with ``COUNT_ALLOCS`` defined, Python will
+   dump allocations counts into stderr on shutdown.
+
+   .. versionadded:: 2.7.15
+
 .. envvar:: PYTHONSHOWREFCOUNT
 
    If set, Python will print the total reference count when the program
index 25df3ed0c414931a3d97c6e6c19d4d9aad4db739..d14a6620b5d289c9530106eb407e6165b1421154 100644 (file)
@@ -1795,6 +1795,9 @@ def py3k_bytes(b):
         except TypeError:
             return bytes(b)
 
+requires_type_collecting = unittest.skipIf(hasattr(sys, 'getcounts'),
+                        'types are immortal if COUNT_ALLOCS is defined')
+
 def args_from_interpreter_flags():
     """Return a list of command-line arguments reproducing the current
     settings in sys.flags."""
index 6a8c3a132742720c0bb1aef35ecbecb79f30f18d..dbba37cdb6fff8b27caac5649d4ec057f3521241 100644 (file)
@@ -208,6 +208,7 @@ class TestABC(unittest.TestCase):
         C()
         self.assertEqual(B.counter, 1)
 
+    @test_support.requires_type_collecting
     def test_cache_leak(self):
         # See issue #2521.
         class A(object):
index ed01c9802fc1ee5db1fe9dbed4d3ac260719f78d..7e47b2d3a27baef1f86aa59874b5621b05cb3ef6 100644 (file)
@@ -1,5 +1,6 @@
 import unittest
-from test.test_support import verbose, run_unittest, start_threads
+from test.support import (verbose, run_unittest, start_threads,
+                          requires_type_collecting)
 import sys
 import time
 import gc
@@ -90,6 +91,7 @@ class GCTests(unittest.TestCase):
         del a
         self.assertNotEqual(gc.collect(), 0)
 
+    @requires_type_collecting
     def test_newinstance(self):
         class A(object):
             pass
index aae274384c738d0e64a081d9b3b61a3b1e93e9d1..988a72c109962abe36e49a4e0c768c55c78ed39d 100644 (file)
@@ -493,6 +493,7 @@ class ArgsTestCase(BaseTestCase):
             self.assertIn(line2, reflog)
 
     @unittest.skipUnless(Py_DEBUG, 'need a debug build')
+    @support.requires_type_collecting
     def test_huntrleaks(self):
         # test --huntrleaks
         code = textwrap.dedent("""
index 5baaa352c0ba76ba8045bb722de8e453604b1126..9342716272a7167a29c057836a8abecb03ec672b 100644 (file)
@@ -748,7 +748,10 @@ class SizeofTest(unittest.TestCase):
         # tupleiterator
         check(iter(()), size('lP'))
         # type
-        s = vsize('P2P15Pl4PP9PP11PI'   # PyTypeObject
+        fmt = 'P2P15Pl4PP9PP11PI'
+        if hasattr(sys, 'getcounts'):
+            fmt += '3P2P'
+        s = vsize(fmt +                 # PyTypeObject
                   '39P'                 # PyNumberMethods
                   '3P'                  # PyMappingMethods
                   '10P'                 # PySequenceMethods
index 415d5ebbd728bf85cd444cee8c85c83c1b8b0b17..418481dadd8f596d26f91b48b58a5992a0206930 100644 (file)
@@ -601,6 +601,7 @@ class ReferencesTestCase(TestBase):
         del c1, c2, C, D
         gc.collect()
 
+    @test_support.requires_type_collecting
     def test_callback_in_cycle_resurrection(self):
         import gc
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-10-09-11-03-13.bpo-31692.5-bpdk.rst b/Misc/NEWS.d/next/Core and Builtins/2017-10-09-11-03-13.bpo-31692.5-bpdk.rst
new file mode 100644 (file)
index 0000000..f32548c
--- /dev/null
@@ -0,0 +1,4 @@
+Add a new PYTHONSHOWALLOCCOUNT environment variable. When Python is compiled
+with COUNT_ALLOCS, PYTHONSHOWALLOCCOUNT now has to be set to dump allocation
+counts into stderr on shutdown. Moreover, allocations statistics are now dumped
+into stderr rather than stdout.
index 677f6e48111be44900da4afd99059cbafc511b1a..44fe13d2f7d559eda2d49891f9366761d2169b9f 100644 (file)
@@ -488,7 +488,9 @@ Py_Finalize(void)
 
     /* Debugging stuff */
 #ifdef COUNT_ALLOCS
-    dump_counts(stdout);
+    if (Py_GETENV("PYTHONSHOWALLOCCOUNT")) {
+        dump_counts(stderr);
+    }
 #endif
 
     _PyDebug_PrintTotalRefs();