4747# Skip this test if the _testcapi module isn't available.
4848_testcapi = import_helper .import_module ('_testcapi' )
4949
50+ import _testlimitedcapi
5051import _testinternalcapi
5152
5253
@@ -1124,7 +1125,7 @@ def test_heaptype_relative_sizes(self):
11241125 # Test subclassing using "relative" basicsize, see PEP 697
11251126 def check (extra_base_size , extra_size ):
11261127 Base , Sub , instance , data_ptr , data_offset , data_size = (
1127- _testcapi .make_sized_heaptypes (
1128+ _testlimitedcapi .make_sized_heaptypes (
11281129 extra_base_size , - extra_size ))
11291130
11301131 # no alignment shenanigans when inheriting directly
@@ -1152,11 +1153,11 @@ def check(extra_base_size, extra_size):
11521153
11531154 # we don't reserve (requested + alignment) or more data
11541155 self .assertLess (data_size - extra_size ,
1155- _testcapi .ALIGNOF_MAX_ALIGN_T )
1156+ _testlimitedcapi .ALIGNOF_MAX_ALIGN_T )
11561157
11571158 # The offsets/sizes we calculated should be aligned.
1158- self .assertEqual (data_offset % _testcapi .ALIGNOF_MAX_ALIGN_T , 0 )
1159- self .assertEqual (data_size % _testcapi .ALIGNOF_MAX_ALIGN_T , 0 )
1159+ self .assertEqual (data_offset % _testlimitedcapi .ALIGNOF_MAX_ALIGN_T , 0 )
1160+ self .assertEqual (data_size % _testlimitedcapi .ALIGNOF_MAX_ALIGN_T , 0 )
11601161
11611162 sizes = sorted ({0 , 1 , 2 , 3 , 4 , 7 , 8 , 123 ,
11621163 object .__basicsize__ ,
@@ -1182,7 +1183,7 @@ def test_heaptype_inherit_itemsize(self):
11821183 object .__basicsize__ + 1 })
11831184 for extra_size in sizes :
11841185 with self .subTest (extra_size = extra_size ):
1185- Sub = _testcapi .subclass_var_heaptype (
1186+ Sub = _testlimitedcapi .subclass_var_heaptype (
11861187 _testcapi .HeapCCollection , - extra_size , 0 , 0 )
11871188 collection = Sub (1 , 2 , 3 )
11881189 collection .set_data_to_3s ()
@@ -1196,7 +1197,7 @@ def test_heaptype_invalid_inheritance(self):
11961197 with self .assertRaises (SystemError ,
11971198 msg = "Cannot extend variable-size class without "
11981199 + "Py_TPFLAGS_ITEMS_AT_END" ):
1199- _testcapi .subclass_heaptype (int , - 8 , 0 )
1200+ _testlimitedcapi .subclass_heaptype (int , - 8 , 0 )
12001201
12011202 def test_heaptype_relative_members (self ):
12021203 """Test HeapCCollection subclasses work properly"""
@@ -1209,7 +1210,7 @@ def test_heaptype_relative_members(self):
12091210 for offset in sizes :
12101211 with self .subTest (extra_base_size = extra_base_size , extra_size = extra_size , offset = offset ):
12111212 if offset < extra_size :
1212- Sub = _testcapi .make_heaptype_with_member (
1213+ Sub = _testlimitedcapi .make_heaptype_with_member (
12131214 extra_base_size , - extra_size , offset , True )
12141215 Base = Sub .mro ()[1 ]
12151216 instance = Sub ()
@@ -1228,29 +1229,29 @@ def test_heaptype_relative_members(self):
12281229 instance .set_memb_relative (0 )
12291230 else :
12301231 with self .assertRaises (SystemError ):
1231- Sub = _testcapi .make_heaptype_with_member (
1232+ Sub = _testlimitedcapi .make_heaptype_with_member (
12321233 extra_base_size , - extra_size , offset , True )
12331234 with self .assertRaises (SystemError ):
1234- Sub = _testcapi .make_heaptype_with_member (
1235+ Sub = _testlimitedcapi .make_heaptype_with_member (
12351236 extra_base_size , extra_size , offset , True )
12361237 with self .subTest (extra_base_size = extra_base_size , extra_size = extra_size ):
12371238 with self .assertRaises (SystemError ):
1238- Sub = _testcapi .make_heaptype_with_member (
1239+ Sub = _testlimitedcapi .make_heaptype_with_member (
12391240 extra_base_size , - extra_size , - 1 , True )
12401241
12411242 def test_heaptype_relative_members_errors (self ):
12421243 with self .assertRaisesRegex (
12431244 SystemError ,
12441245 r"With Py_RELATIVE_OFFSET, basicsize must be negative" ):
1245- _testcapi .make_heaptype_with_member (0 , 1234 , 0 , True )
1246+ _testlimitedcapi .make_heaptype_with_member (0 , 1234 , 0 , True )
12461247 with self .assertRaisesRegex (
12471248 SystemError , r"Member offset out of range \(0\.\.-basicsize\)" ):
1248- _testcapi .make_heaptype_with_member (0 , - 8 , 1234 , True )
1249+ _testlimitedcapi .make_heaptype_with_member (0 , - 8 , 1234 , True )
12491250 with self .assertRaisesRegex (
12501251 SystemError , r"Member offset out of range \(0\.\.-basicsize\)" ):
1251- _testcapi .make_heaptype_with_member (0 , - 8 , - 1 , True )
1252+ _testlimitedcapi .make_heaptype_with_member (0 , - 8 , - 1 , True )
12521253
1253- Sub = _testcapi .make_heaptype_with_member (0 , - 8 , 0 , True )
1254+ Sub = _testlimitedcapi .make_heaptype_with_member (0 , - 8 , 0 , True )
12541255 instance = Sub ()
12551256 with self .assertRaisesRegex (
12561257 SystemError , r"PyMember_GetOne used with Py_RELATIVE_OFFSET" ):
@@ -2264,10 +2265,19 @@ def test_gilstate_matches_current(self):
22642265 _testcapi .test_current_tstate_matches ()
22652266
22662267
2268+ def get_test_funcs (mod , exclude_prefix = None ):
2269+ funcs = {}
2270+ for name in dir (mod ):
2271+ if not name .startswith ('test_' ):
2272+ continue
2273+ if exclude_prefix is not None and name .startswith (exclude_prefix ):
2274+ continue
2275+ funcs [name ] = getattr (mod , name )
2276+ return funcs
2277+
2278+
22672279class Test_testcapi (unittest .TestCase ):
2268- locals ().update ((name , getattr (_testcapi , name ))
2269- for name in dir (_testcapi )
2270- if name .startswith ('test_' ))
2280+ locals ().update (get_test_funcs (_testcapi ))
22712281
22722282 # Suppress warning from PyUnicode_FromUnicode().
22732283 @warnings_helper .ignore_warnings (category = DeprecationWarning )
@@ -2278,11 +2288,13 @@ def test_version_api_data(self):
22782288 self .assertEqual (_testcapi .Py_Version , sys .hexversion )
22792289
22802290
2291+ class Test_testlimitedcapi (unittest .TestCase ):
2292+ locals ().update (get_test_funcs (_testlimitedcapi ))
2293+
2294+
22812295class Test_testinternalcapi (unittest .TestCase ):
2282- locals ().update ((name , getattr (_testinternalcapi , name ))
2283- for name in dir (_testinternalcapi )
2284- if name .startswith ('test_' )
2285- and not name .startswith ('test_lock_' ))
2296+ locals ().update (get_test_funcs (_testinternalcapi ,
2297+ exclude_prefix = 'test_lock_' ))
22862298
22872299
22882300@threading_helper .requires_working_threading ()
0 commit comments