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

Skip to content

Commit bae75cf

Browse files
Issue #19527: Fixed tests with defined COUNT_ALLOCS.
2 parents 7e160ce + a793037 commit bae75cf

11 files changed

Lines changed: 29 additions & 6 deletions

File tree

Lib/test/support/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,6 +2071,9 @@ def strip_python_stderr(stderr):
20712071
stderr = re.sub(br"\[\d+ refs, \d+ blocks\]\r?\n?", b"", stderr).strip()
20722072
return stderr
20732073

2074+
requires_type_collecting = unittest.skipIf(hasattr(sys, 'getcounts'),
2075+
'types are immortal if COUNT_ALLOCS is defined')
2076+
20742077
def args_from_interpreter_flags():
20752078
"""Return a list of command-line arguments reproducing the current
20762079
settings in sys.flags and sys.warnoptions."""

Lib/test/test_gc.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
from test.support import (verbose, refcount_test, run_unittest,
3-
strip_python_stderr, cpython_only, start_threads,
4-
temp_dir)
3+
strip_python_stderr, cpython_only, start_threads,
4+
temp_dir, requires_type_collecting)
55
from test.support.script_helper import assert_python_ok, make_script
66

77
import sys
@@ -118,6 +118,7 @@ class A:
118118
del a
119119
self.assertNotEqual(gc.collect(), 0)
120120

121+
@requires_type_collecting
121122
def test_newinstance(self):
122123
class A(object):
123124
pass
@@ -678,6 +679,7 @@ def run_command(code):
678679
stderr = run_command(code % "gc.DEBUG_SAVEALL")
679680
self.assertNotIn(b"uncollectable objects at shutdown", stderr)
680681

682+
@requires_type_collecting
681683
def test_gc_main_module_at_shutdown(self):
682684
# Create a reference cycle through the __main__ module and check
683685
# it gets collected at interpreter shutdown.
@@ -691,6 +693,7 @@ def __del__(self):
691693
rc, out, err = assert_python_ok('-c', code)
692694
self.assertEqual(out.strip(), b'__del__ called')
693695

696+
@requires_type_collecting
694697
def test_gc_ordinary_module_at_shutdown(self):
695698
# Same as above, but with a non-__main__ module.
696699
with temp_dir() as script_dir:

Lib/test/test_io.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3165,6 +3165,7 @@ def __del__(self):
31653165
""".format(iomod=iomod, kwargs=kwargs)
31663166
return assert_python_ok("-c", code)
31673167

3168+
@support.requires_type_collecting
31683169
def test_create_at_shutdown_without_encoding(self):
31693170
rc, out, err = self._check_create_at_shutdown()
31703171
if err:
@@ -3174,6 +3175,7 @@ def test_create_at_shutdown_without_encoding(self):
31743175
else:
31753176
self.assertEqual("ok", out.decode().strip())
31763177

3178+
@support.requires_type_collecting
31773179
def test_create_at_shutdown_with_encoding(self):
31783180
rc, out, err = self._check_create_at_shutdown(encoding='utf-8',
31793181
errors='strict')

Lib/test/test_logging.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3389,6 +3389,7 @@ class MyLogger(logging.Logger):
33893389
logging.setLoggerClass(logging.Logger)
33903390
self.assertEqual(logging.getLoggerClass(), logging.Logger)
33913391

3392+
@support.requires_type_collecting
33923393
def test_logging_at_shutdown(self):
33933394
# Issue #20037
33943395
code = """if 1:

Lib/test/test_module.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Test the module type
22
import unittest
33
import weakref
4-
from test.support import gc_collect
4+
from test.support import gc_collect, requires_type_collecting
55
from test.support.script_helper import assert_python_ok
66

77
import sys
@@ -101,6 +101,7 @@ def f():
101101
gc_collect()
102102
self.assertEqual(f().__dict__["bar"], 4)
103103

104+
@requires_type_collecting
104105
def test_clear_dict_in_ref_cycle(self):
105106
destroyed = []
106107
m = ModuleType("foo")
@@ -214,6 +215,7 @@ def test_module_repr_source(self):
214215
self.assertEqual(r[-len(ends_with):], ends_with,
215216
'{!r} does not end with {!r}'.format(r, ends_with))
216217

218+
@requires_type_collecting
217219
def test_module_finalization_at_shutdown(self):
218220
# Module globals and builtins should still be available during shutdown
219221
rc, out, err = assert_python_ok("-c", "from test import final_a")

Lib/test/test_regrtest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,9 @@ def test_leak(self):
763763

764764
with open(filename) as fp:
765765
reflog = fp.read()
766+
if hasattr(sys, 'getcounts'):
767+
# Types are immportal if COUNT_ALLOCS is defined
768+
reflog = reflog.splitlines(True)[-1]
766769
self.assertEqual(reflog, line2)
767770

768771
def test_list_tests(self):

Lib/test/test_sys.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,7 @@ def test_getallocatedblocks(self):
803803
c = sys.getallocatedblocks()
804804
self.assertIn(c, range(b - 50, b + 50))
805805

806+
@test.support.requires_type_collecting
806807
def test_is_finalizing(self):
807808
self.assertIs(sys.is_finalizing(), False)
808809
# Don't use the atexit module because _Py_Finalizing is only set
@@ -1083,9 +1084,12 @@ def delx(self): del self.__x
10831084
check((1,2,3), vsize('') + 3*self.P)
10841085
# type
10851086
# static type: PyTypeObject
1086-
s = vsize('P2n15Pl4Pn9Pn11PIP')
1087+
fmt = 'P2n15Pl4Pn9Pn11PIP'
1088+
if hasattr(sys, 'getcounts'):
1089+
fmt += '3n2P'
1090+
s = vsize(fmt)
10871091
check(int, s)
1088-
s = vsize('P2n15Pl4Pn9Pn11PIP' # PyTypeObject
1092+
s = vsize(fmt + # PyTypeObject
10891093
'3P' # PyAsyncMethods
10901094
'36P' # PyNumberMethods
10911095
'3P' # PyMappingMethods

Lib/test/test_threading.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"""
44

55
import test.support
6-
from test.support import verbose, import_module, cpython_only
6+
from test.support import (verbose, import_module, cpython_only,
7+
requires_type_collecting)
78
from test.support.script_helper import assert_python_ok, assert_python_failure
89

910
import random
@@ -987,6 +988,7 @@ def run():
987988
self.assertIn("ZeroDivisionError", err)
988989
self.assertNotIn("Unhandled exception", err)
989990

991+
@requires_type_collecting
990992
def test_print_exception_stderr_is_none_1(self):
991993
script = r"""if True:
992994
import sys

Lib/test/test_traceback.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ def do_test(firstlines, message, charset, lineno):
178178
# Issue #18960: coding spec should have no effect
179179
do_test("x=0\n# coding: GBK\n", "h\xe9 ho", 'utf-8', 5)
180180

181+
@support.requires_type_collecting
181182
def test_print_traceback_at_exit(self):
182183
# Issue #22599: Ensure that it is possible to use the traceback module
183184
# to display an exception at Python exit

Lib/test/test_warnings/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,7 @@ def test_issue_8766(self):
10351035

10361036

10371037
class FinalizationTest(unittest.TestCase):
1038+
@support.requires_type_collecting
10381039
def test_finalization(self):
10391040
# Issue #19421: warnings.warn() should not crash
10401041
# during Python finalization

0 commit comments

Comments
 (0)