churchyard / rpms / python2

Forked from rpms/python2 6 years ago
Clone
Iryna Shcherbina 6bc3f63
From 7b4ba62e388474e811268322b47f80d464933541 Mon Sep 17 00:00:00 2001
Iryna Shcherbina 6bc3f63
From: Victor Stinner <victor.stinner@gmail.com>
Iryna Shcherbina 6bc3f63
Date: Tue, 17 Oct 2017 02:25:23 -0700
Iryna Shcherbina 6bc3f63
Subject: [PATCH] [2.7] bpo-31692: Add PYTHONSHOWALLOCCOUNT env var (GH-3927)
Iryna Shcherbina 6bc3f63
Iryna Shcherbina 6bc3f63
bpo-31692, bpo-19527:
Iryna Shcherbina 6bc3f63
Iryna Shcherbina 6bc3f63
* Add a new PYTHONSHOWALLOCCOUNT environment variable, similar to
Iryna Shcherbina 6bc3f63
  the Python 3 "-X showalloccount" option
Iryna Shcherbina 6bc3f63
* When Python is compiled with COUNT_ALLOCS, the new
Iryna Shcherbina 6bc3f63
  PYTHONSHOWALLOCCOUNT environment variable now has to be set to dump
Iryna Shcherbina 6bc3f63
  allocation counts into stderr on shutdown. Moreover, allocations
Iryna Shcherbina 6bc3f63
  statistics are now dumped into stderr rather than stdout.
Iryna Shcherbina 6bc3f63
* Add @test.support.requires_type_collecting decorator: skip test if
Iryna Shcherbina 6bc3f63
  COUNT_ALLOCS is defined
Iryna Shcherbina 6bc3f63
* Fix tests for COUNT_ALLOCS: decorate some methods with
Iryna Shcherbina 6bc3f63
  @requires_type_collecting
Iryna Shcherbina 6bc3f63
* test_sys.test_objecttypes(): update object type when COUNT_ALLOCS
Iryna Shcherbina 6bc3f63
  is defined
Iryna Shcherbina 6bc3f63
---
Iryna Shcherbina 6bc3f63
 Doc/c-api/typeobj.rst                                              | 2 +-
Iryna Shcherbina 6bc3f63
 Doc/using/cmdline.rst                                              | 7 +++++++
Iryna Shcherbina 6bc3f63
 Lib/test/support/__init__.py                                       | 3 +++
Iryna Shcherbina 6bc3f63
 Lib/test/test_abc.py                                               | 1 +
Iryna Shcherbina 6bc3f63
 Lib/test/test_gc.py                                                | 4 +++-
Iryna Shcherbina 6bc3f63
 Lib/test/test_regrtest.py                                          | 1 +
Iryna Shcherbina 6bc3f63
 Lib/test/test_sys.py                                               | 5 ++++-
Iryna Shcherbina 6bc3f63
 Lib/test/test_weakref.py                                           | 1 +
Iryna Shcherbina 6bc3f63
 .../Core and Builtins/2017-10-09-11-03-13.bpo-31692.5-bpdk.rst     | 4 ++++
Iryna Shcherbina 6bc3f63
 Python/pythonrun.c                                                 | 4 +++-
Iryna Shcherbina 6bc3f63
 10 files changed, 28 insertions(+), 4 deletions(-)
Iryna Shcherbina 6bc3f63
 create mode 100644 Misc/NEWS.d/next/Core and Builtins/2017-10-09-11-03-13.bpo-31692.5-bpdk.rst
Iryna Shcherbina 6bc3f63
Iryna Shcherbina 6bc3f63
diff --git a/Doc/c-api/typeobj.rst b/Doc/c-api/typeobj.rst
Iryna Shcherbina 6bc3f63
index 18edcdd7e5a..f0ccf2ea5fe 100644
Iryna Shcherbina 6bc3f63
--- a/Doc/c-api/typeobj.rst
Iryna Shcherbina 6bc3f63
+++ b/Doc/c-api/typeobj.rst
Iryna Shcherbina 6bc3f63
@@ -1101,7 +1101,7 @@ The next fields, up to and including :c:member:`~PyTypeObject.tp_weaklist`, only
Iryna Shcherbina 6bc3f63
 The remaining fields are only defined if the feature test macro
Iryna Shcherbina 6bc3f63
 :const:`COUNT_ALLOCS` is defined, and are for internal use only. They are
Iryna Shcherbina 6bc3f63
 documented here for completeness.  None of these fields are inherited by
Iryna Shcherbina 6bc3f63
-subtypes.
Iryna Shcherbina 6bc3f63
+subtypes. See the :envvar:`PYTHONSHOWALLOCCOUNT` environment variable.
Iryna Shcherbina 6bc3f63
 
Iryna Shcherbina 6bc3f63
 
Iryna Shcherbina 6bc3f63
 .. c:member:: Py_ssize_t PyTypeObject.tp_allocs
Iryna Shcherbina 6bc3f63
diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst
Iryna Shcherbina 6bc3f63
index f00f7f6026a..55bc12893d6 100644
Iryna Shcherbina 6bc3f63
--- a/Doc/using/cmdline.rst
Iryna Shcherbina 6bc3f63
+++ b/Doc/using/cmdline.rst
Iryna Shcherbina 6bc3f63
@@ -663,3 +663,10 @@ if Python was configured with the ``--with-pydebug`` build option.
Iryna Shcherbina 6bc3f63
 
Iryna Shcherbina 6bc3f63
    If set, Python will print memory allocation statistics every time a new
Iryna Shcherbina 6bc3f63
    object arena is created, and on shutdown.
Iryna Shcherbina 6bc3f63
+
Iryna Shcherbina 6bc3f63
+.. envvar:: PYTHONSHOWALLOCCOUNT
Iryna Shcherbina 6bc3f63
+
Iryna Shcherbina 6bc3f63
+   If set and Python was compiled with ``COUNT_ALLOCS`` defined, Python will
Iryna Shcherbina 6bc3f63
+   dump allocations counts into stderr on shutdown.
Iryna Shcherbina 6bc3f63
+
Iryna Shcherbina 6bc3f63
+   .. versionadded:: 2.7.15
f948d41
diff --git a/Doc/whatsnew/2.7.rst b/Doc/whatsnew/2.7.rst
f948d41
index 28a8d4b..f0d2428 100644
f948d41
--- a/Doc/whatsnew/2.7.rst
f948d41
+++ b/Doc/whatsnew/2.7.rst
f948d41
@@ -2540,6 +2540,17 @@ exemption allowing new ``-3`` warnings to be added in any Python 2.7
f948d41
 maintenance release.
f948d41
 
f948d41
 
f948d41
+Two new environment variables for debug mode
f948d41
+--------------------------------------------
f948d41
+
f948d41
+When Python is compiled with ``COUNT_ALLOC`` defined, allocation counts are no
f948d41
+longer dumped by default anymore: the :envvar:`PYTHONSHOWALLOCCOUNT` environment
f948d41
+variable must now also be set. Moreover, allocation counts are now dumped into
f948d41
+stderr, rather than stdout. (Contributed by Victor Stinner; :issue:`31692`.)
f948d41
+
f948d41
+.. versionadded:: 2.7.15
f948d41
+
f948d41
+
f948d41
 PEP 434: IDLE Enhancement Exception for All Branches
f948d41
 ----------------------------------------------------
f948d41
 
Iryna Shcherbina 6bc3f63
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
Iryna Shcherbina 6bc3f63
index 25df3ed0c41..d14a6620b5d 100644
Iryna Shcherbina 6bc3f63
--- a/Lib/test/support/__init__.py
Iryna Shcherbina 6bc3f63
+++ b/Lib/test/support/__init__.py
Iryna Shcherbina 6bc3f63
@@ -1795,6 +1795,9 @@ def py3k_bytes(b):
Iryna Shcherbina 6bc3f63
         except TypeError:
Iryna Shcherbina 6bc3f63
             return bytes(b)
Iryna Shcherbina 6bc3f63
 
Iryna Shcherbina 6bc3f63
+requires_type_collecting = unittest.skipIf(hasattr(sys, 'getcounts'),
Iryna Shcherbina 6bc3f63
+                        'types are immortal if COUNT_ALLOCS is defined')
Iryna Shcherbina 6bc3f63
+
Iryna Shcherbina 6bc3f63
 def args_from_interpreter_flags():
Iryna Shcherbina 6bc3f63
     """Return a list of command-line arguments reproducing the current
Iryna Shcherbina 6bc3f63
     settings in sys.flags."""
Iryna Shcherbina 6bc3f63
diff --git a/Lib/test/test_abc.py b/Lib/test/test_abc.py
Iryna Shcherbina 6bc3f63
index 6a8c3a13274..dbba37cdb6f 100644
Iryna Shcherbina 6bc3f63
--- a/Lib/test/test_abc.py
Iryna Shcherbina 6bc3f63
+++ b/Lib/test/test_abc.py
Iryna Shcherbina 6bc3f63
@@ -208,6 +208,7 @@ class C(A, B):
Iryna Shcherbina 6bc3f63
         C()
Iryna Shcherbina 6bc3f63
         self.assertEqual(B.counter, 1)
Iryna Shcherbina 6bc3f63
 
Iryna Shcherbina 6bc3f63
+    @test_support.requires_type_collecting
Iryna Shcherbina 6bc3f63
     def test_cache_leak(self):
Iryna Shcherbina 6bc3f63
         # See issue #2521.
Iryna Shcherbina 6bc3f63
         class A(object):
Iryna Shcherbina 6bc3f63
diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py
Iryna Shcherbina 6bc3f63
index ed01c9802fc..7e47b2d3a27 100644
Iryna Shcherbina 6bc3f63
--- a/Lib/test/test_gc.py
Iryna Shcherbina 6bc3f63
+++ b/Lib/test/test_gc.py
Iryna Shcherbina 6bc3f63
@@ -1,5 +1,6 @@
Iryna Shcherbina 6bc3f63
 import unittest
Iryna Shcherbina 6bc3f63
-from test.test_support import verbose, run_unittest, start_threads, import_module
Iryna Shcherbina 6bc3f63
+from test.support import (verbose, run_unittest, start_threads, import_module,
Iryna Shcherbina 6bc3f63
+                          requires_type_collecting)
Iryna Shcherbina 6bc3f63
 import sys
Iryna Shcherbina 6bc3f63
 import sysconfig
Iryna Shcherbina 6bc3f63
 import time
Iryna Shcherbina 6bc3f63
@@ -90,6 +91,7 @@ class A:
Iryna Shcherbina 6bc3f63
         del a
Iryna Shcherbina 6bc3f63
         self.assertNotEqual(gc.collect(), 0)
Iryna Shcherbina 6bc3f63
 
Iryna Shcherbina 6bc3f63
+    @requires_type_collecting
Iryna Shcherbina 6bc3f63
     def test_newinstance(self):
Iryna Shcherbina 6bc3f63
         class A(object):
Iryna Shcherbina 6bc3f63
             pass
Iryna Shcherbina 6bc3f63
diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py
Iryna Shcherbina 6bc3f63
index aae274384c7..988a72c1099 100644
Iryna Shcherbina 6bc3f63
--- a/Lib/test/test_regrtest.py
Iryna Shcherbina 6bc3f63
+++ b/Lib/test/test_regrtest.py
Iryna Shcherbina 6bc3f63
@@ -493,6 +493,7 @@ def check_leak(self, code, what):
Iryna Shcherbina 6bc3f63
             self.assertIn(line2, reflog)
Iryna Shcherbina 6bc3f63
 
Iryna Shcherbina 6bc3f63
     @unittest.skipUnless(Py_DEBUG, 'need a debug build')
Iryna Shcherbina 6bc3f63
+    @support.requires_type_collecting
Iryna Shcherbina 6bc3f63
     def test_huntrleaks(self):
Iryna Shcherbina 6bc3f63
         # test --huntrleaks
Iryna Shcherbina 6bc3f63
         code = textwrap.dedent("""
Iryna Shcherbina 6bc3f63
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
Iryna Shcherbina 6bc3f63
index 5baaa352c0b..9342716272a 100644
Iryna Shcherbina 6bc3f63
--- a/Lib/test/test_sys.py
Iryna Shcherbina 6bc3f63
+++ b/Lib/test/test_sys.py
Iryna Shcherbina 6bc3f63
@@ -748,7 +748,10 @@ def delx(self): del self.__x
Iryna Shcherbina 6bc3f63
         # tupleiterator
Iryna Shcherbina 6bc3f63
         check(iter(()), size('lP'))
Iryna Shcherbina 6bc3f63
         # type
Iryna Shcherbina 6bc3f63
-        s = vsize('P2P15Pl4PP9PP11PI'   # PyTypeObject
Iryna Shcherbina 6bc3f63
+        fmt = 'P2P15Pl4PP9PP11PI'
Iryna Shcherbina 6bc3f63
+        if hasattr(sys, 'getcounts'):
Iryna Shcherbina 6bc3f63
+            fmt += '3P2P'
Iryna Shcherbina 6bc3f63
+        s = vsize(fmt +                 # PyTypeObject
Iryna Shcherbina 6bc3f63
                   '39P'                 # PyNumberMethods
Iryna Shcherbina 6bc3f63
                   '3P'                  # PyMappingMethods
Iryna Shcherbina 6bc3f63
                   '10P'                 # PySequenceMethods
Iryna Shcherbina 6bc3f63
diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py
Iryna Shcherbina 6bc3f63
index 415d5ebbd72..418481dadd8 100644
Iryna Shcherbina 6bc3f63
--- a/Lib/test/test_weakref.py
Iryna Shcherbina 6bc3f63
+++ b/Lib/test/test_weakref.py
Iryna Shcherbina 6bc3f63
@@ -601,6 +601,7 @@ class D:
Iryna Shcherbina 6bc3f63
         del c1, c2, C, D
Iryna Shcherbina 6bc3f63
         gc.collect()
Iryna Shcherbina 6bc3f63
 
Iryna Shcherbina 6bc3f63
+    @test_support.requires_type_collecting
Iryna Shcherbina 6bc3f63
     def test_callback_in_cycle_resurrection(self):
Iryna Shcherbina 6bc3f63
         import gc
Iryna Shcherbina 6bc3f63
 
Iryna Shcherbina 6bc3f63
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
Iryna Shcherbina 6bc3f63
index 677f6e48111..44fe13d2f7d 100644
Iryna Shcherbina 6bc3f63
--- a/Python/pythonrun.c
Iryna Shcherbina 6bc3f63
+++ b/Python/pythonrun.c
Iryna Shcherbina 6bc3f63
@@ -488,7 +488,9 @@ Py_Finalize(void)
Iryna Shcherbina 6bc3f63
 
Iryna Shcherbina 6bc3f63
     /* Debugging stuff */
Iryna Shcherbina 6bc3f63
 #ifdef COUNT_ALLOCS
Iryna Shcherbina 6bc3f63
-    dump_counts(stdout);
Iryna Shcherbina 6bc3f63
+    if (Py_GETENV("PYTHONSHOWALLOCCOUNT")) {
Iryna Shcherbina 6bc3f63
+        dump_counts(stderr);
Iryna Shcherbina 6bc3f63
+    }
Iryna Shcherbina 6bc3f63
 #endif
Iryna Shcherbina 6bc3f63
 
Iryna Shcherbina 6bc3f63
     PRINT_TOTAL_REFS();