[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
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index 25df3ed..d14a662 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -1795,6 +1795,9 @@
         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."""
diff --git a/Lib/test/test_abc.py b/Lib/test/test_abc.py
index 6a8c3a1..dbba37c 100644
--- a/Lib/test/test_abc.py
+++ b/Lib/test/test_abc.py
@@ -208,6 +208,7 @@
         C()
         self.assertEqual(B.counter, 1)
 
+    @test_support.requires_type_collecting
     def test_cache_leak(self):
         # See issue #2521.
         class A(object):
diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py
index ed01c98..7e47b2d 100644
--- a/Lib/test/test_gc.py
+++ b/Lib/test/test_gc.py
@@ -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 @@
         del a
         self.assertNotEqual(gc.collect(), 0)
 
+    @requires_type_collecting
     def test_newinstance(self):
         class A(object):
             pass
diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py
index aae2743..988a72c 100644
--- a/Lib/test/test_regrtest.py
+++ b/Lib/test/test_regrtest.py
@@ -493,6 +493,7 @@
             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("""
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index 5baaa35..9342716 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -748,7 +748,10 @@
         # 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
diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py
index 415d5eb..418481d 100644
--- a/Lib/test/test_weakref.py
+++ b/Lib/test/test_weakref.py
@@ -601,6 +601,7 @@
         del c1, c2, C, D
         gc.collect()
 
+    @test_support.requires_type_collecting
     def test_callback_in_cycle_resurrection(self):
         import gc