From e73283a20fb05b70da2990decefac0e011faec17 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Mon, 1 Nov 2021 12:14:53 +0200 Subject: [PATCH] bpo-45668: Fix PGO tests without test extensions (GH-29315) --- Lib/test/datetimetester.py | 6 ++++- Lib/test/string_tests.py | 11 ++++---- Lib/test/support/__init__.py | 10 ++++++-- Lib/test/test_array.py | 5 ++-- Lib/test/test_bytes.py | 4 +-- Lib/test/test_cmath.py | 8 +++--- Lib/test/test_codecs.py | 1 + Lib/test/test_dict.py | 4 ++- Lib/test/test_embed.py | 1 + Lib/test/test_float.py | 3 ++- Lib/test/test_struct.py | 9 ++++--- Lib/test/test_unicode.py | 25 ++++++++++++++----- .../2021-10-29-17-18-56.bpo-45668.MfAw4i.rst | 1 + 13 files changed, 60 insertions(+), 28 deletions(-) create mode 100644 Misc/NEWS.d/next/Tests/2021-10-29-17-18-56.bpo-45668.MfAw4i.rst diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 9f551d9b974..810478c7db2 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -31,7 +31,10 @@ from datetime import timezone from datetime import date, datetime import time as _time -import _testcapi +try: + import _testcapi +except ImportError: + _testcapi = None # Needed by test_datetime import _strptime @@ -5918,6 +5921,7 @@ class IranTest(ZoneInfoTest): zonename = 'Asia/Tehran' +@unittest.skipIf(_testcapi is None, 'need _testcapi module') class CapiTest(unittest.TestCase): def setUp(self): # Since the C API is not present in the _Pure tests, skip all tests diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index 089701c983a..0d4c7ecf4a0 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -4,6 +4,7 @@ Common tests shared by test_unicode, test_userstring and test_bytes. import unittest, string, sys, struct from test import support +from test.support import import_helper from collections import UserList import random @@ -1328,17 +1329,17 @@ class MixinStrUnicodeUserStringTest: @support.cpython_only def test_formatting_c_limits(self): - from _testcapi import PY_SSIZE_T_MAX, INT_MAX, UINT_MAX - SIZE_MAX = (1 << (PY_SSIZE_T_MAX.bit_length() + 1)) - 1 + _testcapi = import_helper.import_module('_testcapi') + SIZE_MAX = (1 << (_testcapi.PY_SSIZE_T_MAX.bit_length() + 1)) - 1 self.checkraises(OverflowError, '%*s', '__mod__', - (PY_SSIZE_T_MAX + 1, '')) + (_testcapi.PY_SSIZE_T_MAX + 1, '')) self.checkraises(OverflowError, '%.*f', '__mod__', - (INT_MAX + 1, 1. / 7)) + (_testcapi.INT_MAX + 1, 1. / 7)) # Issue 15989 self.checkraises(OverflowError, '%*s', '__mod__', (SIZE_MAX + 1, '')) self.checkraises(OverflowError, '%.*f', '__mod__', - (UINT_MAX + 1, 1. / 7)) + (_testcapi.UINT_MAX + 1, 1. / 7)) def test_floatformatting(self): # float formatting diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index fc3c99ec0ec..6d84a8bea42 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -444,7 +444,10 @@ def requires_lzma(reason='requires lzma'): return unittest.skipUnless(lzma, reason) def has_no_debug_ranges(): - import _testinternalcapi + try: + import _testinternalcapi + except ImportError: + raise unittest.SkipTest("_testinternalcapi required") config = _testinternalcapi.get_config() return bool(config['no_debug_ranges']) @@ -692,7 +695,10 @@ _TPFLAGS_HAVE_GC = 1<<14 _TPFLAGS_HEAPTYPE = 1<<9 def check_sizeof(test, o, size): - import _testinternalcapi + try: + import _testinternalcapi + except ImportError: + raise unittest.SkipTest("_testinternalcapi required") result = sys.getsizeof(o) # add GC header size if ((type(o) == type) and (o.__flags__ & _TPFLAGS_HEAPTYPE) or\ diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index 6a48c1cc975..5b2c107a604 100755 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -5,6 +5,7 @@ import collections.abc import unittest from test import support +from test.support import import_helper from test.support import os_helper from test.support import _2G import weakref @@ -1147,9 +1148,9 @@ class BaseTest: @support.cpython_only def test_obsolete_write_lock(self): - from _testcapi import getbuffer_with_null_view + _testcapi = import_helper.import_module('_testcapi') a = array.array('B', b"") - self.assertRaises(BufferError, getbuffer_with_null_view, a) + self.assertRaises(BufferError, _testcapi.getbuffer_with_null_view, a) def test_free_after_iterating(self): support.check_free_after_iterating(self, iter, array.array, diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index c2fe2cc2542..fd8e1d4bdc4 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -1650,8 +1650,8 @@ class ByteArrayTest(BaseBytesTest, unittest.TestCase): @test.support.cpython_only def test_obsolete_write_lock(self): - from _testcapi import getbuffer_with_null_view - self.assertRaises(BufferError, getbuffer_with_null_view, bytearray()) + _testcapi = import_helper.import_module('_testcapi') + self.assertRaises(BufferError, _testcapi.getbuffer_with_null_view, bytearray()) def test_iterator_pickling2(self): orig = bytearray(b'abc') diff --git a/Lib/test/test_cmath.py b/Lib/test/test_cmath.py index 9bb93935614..4bdec6d2d83 100644 --- a/Lib/test/test_cmath.py +++ b/Lib/test/test_cmath.py @@ -1,4 +1,4 @@ -from test.support import requires_IEEE_754, cpython_only +from test.support import requires_IEEE_754, cpython_only, import_helper from test.test_math import parse_testfile, test_file import test.test_math as test_math import unittest @@ -452,13 +452,13 @@ class CMathTests(unittest.TestCase): @cpython_only def test_polar_errno(self): # Issue #24489: check a previously set C errno doesn't disturb polar() - from _testcapi import set_errno + _testcapi = import_helper.import_module('_testcapi') def polar_with_errno_set(z): - set_errno(11) + _testcapi.set_errno(11) try: return polar(z) finally: - set_errno(0) + _testcapi.set_errno(0) self.check_polar(polar_with_errno_set) def test_phase(self): diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 506b51c428f..f924826db94 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -1970,6 +1970,7 @@ class BasicUnicodeTest(unittest.TestCase, MixInCheckStateHandling): "encoding=%r" % encoding) @support.cpython_only + @unittest.skipIf(_testcapi is None, 'need _testcapi module') def test_basics_capi(self): s = "abc123" # all codecs should be able to encode these for encoding in all_unicode_encodings: diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py index b43c83abd0e..32ffd38f035 100644 --- a/Lib/test/test_dict.py +++ b/Lib/test/test_dict.py @@ -8,6 +8,7 @@ import sys import unittest import weakref from test import support +from test.support import import_helper class DictTest(unittest.TestCase): @@ -1541,7 +1542,8 @@ class CAPITest(unittest.TestCase): # Test _PyDict_GetItem_KnownHash() @support.cpython_only def test_getitem_knownhash(self): - from _testcapi import dict_getitem_knownhash + _testcapi = import_helper.import_module('_testcapi') + dict_getitem_knownhash = _testcapi.dict_getitem_knownhash d = {'x': 1, 'y': 2, 'z': 3} self.assertEqual(dict_getitem_knownhash(d, 'x', hash('x')), 1) diff --git a/Lib/test/test_embed.py b/Lib/test/test_embed.py index 7858f68d971..3a00efa5799 100644 --- a/Lib/test/test_embed.py +++ b/Lib/test/test_embed.py @@ -1494,6 +1494,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase): class SetConfigTests(unittest.TestCase): def test_set_config(self): # bpo-42260: Test _PyInterpreterState_SetConfig() + import_helper.import_module('_testcapi') cmd = [sys.executable, '-I', '-m', 'test._test_embed_set_config'] proc = subprocess.run(cmd, stdout=subprocess.PIPE, diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index aba15839f13..f033736917c 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -8,6 +8,7 @@ import time import unittest from test import support +from test.support import import_helper from test.test_grammar import (VALID_UNDERSCORE_LITERALS, INVALID_UNDERSCORE_LITERALS) from math import isinf, isnan, copysign, ldexp @@ -714,7 +715,7 @@ class IEEEFormatTestCase(unittest.TestCase): @support.requires_IEEE_754 def test_serialized_float_rounding(self): - from _testcapi import FLT_MAX + FLT_MAX = import_helper.import_module('_testcapi').FLT_MAX self.assertEqual(struct.pack("