Skip to content

Commit e73283a

Browse files
authored
bpo-45668: Fix PGO tests without test extensions (GH-29315)
1 parent 762a4dc commit e73283a

File tree

13 files changed

+60
-28
lines changed

13 files changed

+60
-28
lines changed

Lib/test/datetimetester.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
from datetime import date, datetime
3232
import time as _time
3333

34-
import _testcapi
34+
try:
35+
import _testcapi
36+
except ImportError:
37+
_testcapi = None
3538

3639
# Needed by test_datetime
3740
import _strptime
@@ -5918,6 +5921,7 @@ class IranTest(ZoneInfoTest):
59185921
zonename = 'Asia/Tehran'
59195922

59205923

5924+
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
59215925
class CapiTest(unittest.TestCase):
59225926
def setUp(self):
59235927
# Since the C API is not present in the _Pure tests, skip all tests

Lib/test/string_tests.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import unittest, string, sys, struct
66
from test import support
7+
from test.support import import_helper
78
from collections import UserList
89
import random
910

@@ -1328,17 +1329,17 @@ class X(object): pass
13281329

13291330
@support.cpython_only
13301331
def test_formatting_c_limits(self):
1331-
from _testcapi import PY_SSIZE_T_MAX, INT_MAX, UINT_MAX
1332-
SIZE_MAX = (1 << (PY_SSIZE_T_MAX.bit_length() + 1)) - 1
1332+
_testcapi = import_helper.import_module('_testcapi')
1333+
SIZE_MAX = (1 << (_testcapi.PY_SSIZE_T_MAX.bit_length() + 1)) - 1
13331334
self.checkraises(OverflowError, '%*s', '__mod__',
1334-
(PY_SSIZE_T_MAX + 1, ''))
1335+
(_testcapi.PY_SSIZE_T_MAX + 1, ''))
13351336
self.checkraises(OverflowError, '%.*f', '__mod__',
1336-
(INT_MAX + 1, 1. / 7))
1337+
(_testcapi.INT_MAX + 1, 1. / 7))
13371338
# Issue 15989
13381339
self.checkraises(OverflowError, '%*s', '__mod__',
13391340
(SIZE_MAX + 1, ''))
13401341
self.checkraises(OverflowError, '%.*f', '__mod__',
1341-
(UINT_MAX + 1, 1. / 7))
1342+
(_testcapi.UINT_MAX + 1, 1. / 7))
13421343

13431344
def test_floatformatting(self):
13441345
# float formatting

Lib/test/support/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,10 @@ def requires_lzma(reason='requires lzma'):
444444
return unittest.skipUnless(lzma, reason)
445445

446446
def has_no_debug_ranges():
447-
import _testinternalcapi
447+
try:
448+
import _testinternalcapi
449+
except ImportError:
450+
raise unittest.SkipTest("_testinternalcapi required")
448451
config = _testinternalcapi.get_config()
449452
return bool(config['no_debug_ranges'])
450453

@@ -692,7 +695,10 @@ def calcvobjsize(fmt):
692695
_TPFLAGS_HEAPTYPE = 1<<9
693696

694697
def check_sizeof(test, o, size):
695-
import _testinternalcapi
698+
try:
699+
import _testinternalcapi
700+
except ImportError:
701+
raise unittest.SkipTest("_testinternalcapi required")
696702
result = sys.getsizeof(o)
697703
# add GC header size
698704
if ((type(o) == type) and (o.__flags__ & _TPFLAGS_HEAPTYPE) or\

Lib/test/test_array.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import collections.abc
66
import unittest
77
from test import support
8+
from test.support import import_helper
89
from test.support import os_helper
910
from test.support import _2G
1011
import weakref
@@ -1147,9 +1148,9 @@ def test_initialize_with_unicode(self):
11471148

11481149
@support.cpython_only
11491150
def test_obsolete_write_lock(self):
1150-
from _testcapi import getbuffer_with_null_view
1151+
_testcapi = import_helper.import_module('_testcapi')
11511152
a = array.array('B', b"")
1152-
self.assertRaises(BufferError, getbuffer_with_null_view, a)
1153+
self.assertRaises(BufferError, _testcapi.getbuffer_with_null_view, a)
11531154

11541155
def test_free_after_iterating(self):
11551156
support.check_free_after_iterating(self, iter, array.array,

Lib/test/test_bytes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,8 +1650,8 @@ def delslice():
16501650

16511651
@test.support.cpython_only
16521652
def test_obsolete_write_lock(self):
1653-
from _testcapi import getbuffer_with_null_view
1654-
self.assertRaises(BufferError, getbuffer_with_null_view, bytearray())
1653+
_testcapi = import_helper.import_module('_testcapi')
1654+
self.assertRaises(BufferError, _testcapi.getbuffer_with_null_view, bytearray())
16551655

16561656
def test_iterator_pickling2(self):
16571657
orig = bytearray(b'abc')

Lib/test/test_cmath.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from test.support import requires_IEEE_754, cpython_only
1+
from test.support import requires_IEEE_754, cpython_only, import_helper
22
from test.test_math import parse_testfile, test_file
33
import test.test_math as test_math
44
import unittest
@@ -452,13 +452,13 @@ def test_polar(self):
452452
@cpython_only
453453
def test_polar_errno(self):
454454
# Issue #24489: check a previously set C errno doesn't disturb polar()
455-
from _testcapi import set_errno
455+
_testcapi = import_helper.import_module('_testcapi')
456456
def polar_with_errno_set(z):
457-
set_errno(11)
457+
_testcapi.set_errno(11)
458458
try:
459459
return polar(z)
460460
finally:
461-
set_errno(0)
461+
_testcapi.set_errno(0)
462462
self.check_polar(polar_with_errno_set)
463463

464464
def test_phase(self):

Lib/test/test_codecs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1970,6 +1970,7 @@ def test_basics(self):
19701970
"encoding=%r" % encoding)
19711971

19721972
@support.cpython_only
1973+
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
19731974
def test_basics_capi(self):
19741975
s = "abc123" # all codecs should be able to encode these
19751976
for encoding in all_unicode_encodings:

Lib/test/test_dict.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import unittest
99
import weakref
1010
from test import support
11+
from test.support import import_helper
1112

1213

1314
class DictTest(unittest.TestCase):
@@ -1541,7 +1542,8 @@ class CAPITest(unittest.TestCase):
15411542
# Test _PyDict_GetItem_KnownHash()
15421543
@support.cpython_only
15431544
def test_getitem_knownhash(self):
1544-
from _testcapi import dict_getitem_knownhash
1545+
_testcapi = import_helper.import_module('_testcapi')
1546+
dict_getitem_knownhash = _testcapi.dict_getitem_knownhash
15451547

15461548
d = {'x': 1, 'y': 2, 'z': 3}
15471549
self.assertEqual(dict_getitem_knownhash(d, 'x', hash('x')), 1)

Lib/test/test_embed.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,7 @@ def test_init_use_frozen_modules(self):
14941494
class SetConfigTests(unittest.TestCase):
14951495
def test_set_config(self):
14961496
# bpo-42260: Test _PyInterpreterState_SetConfig()
1497+
import_helper.import_module('_testcapi')
14971498
cmd = [sys.executable, '-I', '-m', 'test._test_embed_set_config']
14981499
proc = subprocess.run(cmd,
14991500
stdout=subprocess.PIPE,

Lib/test/test_float.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import unittest
99

1010
from test import support
11+
from test.support import import_helper
1112
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
1213
INVALID_UNDERSCORE_LITERALS)
1314
from math import isinf, isnan, copysign, ldexp
@@ -714,7 +715,7 @@ def test_float_specials_do_unpack(self):
714715

715716
@support.requires_IEEE_754
716717
def test_serialized_float_rounding(self):
717-
from _testcapi import FLT_MAX
718+
FLT_MAX = import_helper.import_module('_testcapi').FLT_MAX
718719
self.assertEqual(struct.pack("<f", 3.40282356e38), struct.pack("<f", FLT_MAX))
719720
self.assertEqual(struct.pack("<f", -3.40282356e38), struct.pack("<f", -FLT_MAX))
720721

0 commit comments

Comments
 (0)