@@ -2204,6 +2204,7 @@ def test_module_state_shared_in_global(self):
2204
2204
self .assertEqual (main_attr_id , subinterp_attr_id )
2205
2205
2206
2206
2207
+ @requires_subinterpreters
2207
2208
class InterpreterConfigTests (unittest .TestCase ):
2208
2209
2209
2210
supported = {
@@ -2277,11 +2278,11 @@ def check(name, expected):
2277
2278
expected = self .supported [expected ]
2278
2279
args = (name ,) if name else ()
2279
2280
2280
- config1 = _testinternalcapi . new_interp_config (* args )
2281
+ config1 = _interpreters . new_config (* args )
2281
2282
self .assert_ns_equal (config1 , expected )
2282
2283
self .assertIsNot (config1 , expected )
2283
2284
2284
- config2 = _testinternalcapi . new_interp_config (* args )
2285
+ config2 = _interpreters . new_config (* args )
2285
2286
self .assert_ns_equal (config2 , expected )
2286
2287
self .assertIsNot (config2 , expected )
2287
2288
self .assertIsNot (config2 , config1 )
@@ -2298,7 +2299,7 @@ def test_update_from_dict(self):
2298
2299
with self .subTest (f'noop ({ name } )' ):
2299
2300
expected = vanilla
2300
2301
overrides = vars (vanilla )
2301
- config = _testinternalcapi . new_interp_config (name , ** overrides )
2302
+ config = _interpreters . new_config (name , ** overrides )
2302
2303
self .assert_ns_equal (config , expected )
2303
2304
2304
2305
with self .subTest (f'change all ({ name } )' ):
@@ -2308,7 +2309,7 @@ def test_update_from_dict(self):
2308
2309
continue
2309
2310
overrides ['gil' ] = gil
2310
2311
expected = types .SimpleNamespace (** overrides )
2311
- config = _testinternalcapi . new_interp_config (
2312
+ config = _interpreters . new_config (
2312
2313
name , ** overrides )
2313
2314
self .assert_ns_equal (config , expected )
2314
2315
@@ -2324,14 +2325,14 @@ def test_update_from_dict(self):
2324
2325
expected = types .SimpleNamespace (
2325
2326
** dict (vars (vanilla ), ** overrides ),
2326
2327
)
2327
- config = _testinternalcapi . new_interp_config (
2328
+ config = _interpreters . new_config (
2328
2329
name , ** overrides )
2329
2330
self .assert_ns_equal (config , expected )
2330
2331
2331
2332
with self .subTest ('unsupported field' ):
2332
2333
for name in self .supported :
2333
2334
with self .assertRaises (ValueError ):
2334
- _testinternalcapi . new_interp_config (name , spam = True )
2335
+ _interpreters . new_config (name , spam = True )
2335
2336
2336
2337
# Bad values for bool fields.
2337
2338
for field , value in vars (self .supported ['empty' ]).items ():
@@ -2341,19 +2342,18 @@ def test_update_from_dict(self):
2341
2342
for value in [1 , '' , 'spam' , 1.0 , None , object ()]:
2342
2343
with self .subTest (f'unsupported value ({ field } ={ value !r} )' ):
2343
2344
with self .assertRaises (TypeError ):
2344
- _testinternalcapi . new_interp_config (** {field : value })
2345
+ _interpreters . new_config (** {field : value })
2345
2346
2346
2347
# Bad values for .gil.
2347
2348
for value in [True , 1 , 1.0 , None , object ()]:
2348
2349
with self .subTest (f'unsupported value(gil={ value !r} )' ):
2349
2350
with self .assertRaises (TypeError ):
2350
- _testinternalcapi . new_interp_config (gil = value )
2351
+ _interpreters . new_config (gil = value )
2351
2352
for value in ['' , 'spam' ]:
2352
2353
with self .subTest (f'unsupported value (gil={ value !r} )' ):
2353
2354
with self .assertRaises (ValueError ):
2354
- _testinternalcapi . new_interp_config (gil = value )
2355
+ _interpreters . new_config (gil = value )
2355
2356
2356
- @requires_subinterpreters
2357
2357
def test_interp_init (self ):
2358
2358
questionable = [
2359
2359
# strange
@@ -2412,11 +2412,10 @@ def check(config):
2412
2412
with self .subTest (f'valid: { config } ' ):
2413
2413
check (config )
2414
2414
2415
- @requires_subinterpreters
2416
2415
def test_get_config (self ):
2417
2416
@contextlib .contextmanager
2418
2417
def new_interp (config ):
2419
- interpid = _testinternalcapi . new_interpreter (config )
2418
+ interpid = _interpreters . create (config , reqrefs = False )
2420
2419
try :
2421
2420
yield interpid
2422
2421
finally :
@@ -2426,32 +2425,32 @@ def new_interp(config):
2426
2425
pass
2427
2426
2428
2427
with self .subTest ('main' ):
2429
- expected = _testinternalcapi . new_interp_config ('legacy' )
2428
+ expected = _interpreters . new_config ('legacy' )
2430
2429
expected .gil = 'own'
2431
2430
interpid = _interpreters .get_main ()
2432
- config = _testinternalcapi . get_interp_config (interpid )
2431
+ config = _interpreters . get_config (interpid )
2433
2432
self .assert_ns_equal (config , expected )
2434
2433
2435
2434
with self .subTest ('isolated' ):
2436
- expected = _testinternalcapi . new_interp_config ('isolated' )
2435
+ expected = _interpreters . new_config ('isolated' )
2437
2436
with new_interp ('isolated' ) as interpid :
2438
- config = _testinternalcapi . get_interp_config (interpid )
2437
+ config = _interpreters . get_config (interpid )
2439
2438
self .assert_ns_equal (config , expected )
2440
2439
2441
2440
with self .subTest ('legacy' ):
2442
- expected = _testinternalcapi . new_interp_config ('legacy' )
2441
+ expected = _interpreters . new_config ('legacy' )
2443
2442
with new_interp ('legacy' ) as interpid :
2444
- config = _testinternalcapi . get_interp_config (interpid )
2443
+ config = _interpreters . get_config (interpid )
2445
2444
self .assert_ns_equal (config , expected )
2446
2445
2447
2446
with self .subTest ('custom' ):
2448
- orig = _testinternalcapi . new_interp_config (
2447
+ orig = _interpreters . new_config (
2449
2448
'empty' ,
2450
2449
use_main_obmalloc = True ,
2451
2450
gil = 'shared' ,
2452
2451
)
2453
2452
with new_interp (orig ) as interpid :
2454
- config = _testinternalcapi . get_interp_config (interpid )
2453
+ config = _interpreters . get_config (interpid )
2455
2454
self .assert_ns_equal (config , orig )
2456
2455
2457
2456
@@ -2529,14 +2528,19 @@ def test_lookup_destroyed(self):
2529
2528
self .assertFalse (
2530
2529
_testinternalcapi .interpreter_exists (interpid ))
2531
2530
2531
+ def get_refcount_helpers (self ):
2532
+ return (
2533
+ _testinternalcapi .get_interpreter_refcount ,
2534
+ (lambda id : _interpreters .incref (id , implieslink = False )),
2535
+ _interpreters .decref ,
2536
+ )
2537
+
2532
2538
def test_linked_lifecycle_does_not_exist (self ):
2533
2539
exists = _testinternalcapi .interpreter_exists
2534
2540
is_linked = _testinternalcapi .interpreter_refcount_linked
2535
2541
link = _testinternalcapi .link_interpreter_refcount
2536
2542
unlink = _testinternalcapi .unlink_interpreter_refcount
2537
- get_refcount = _testinternalcapi .get_interpreter_refcount
2538
- incref = _testinternalcapi .interpreter_incref
2539
- decref = _testinternalcapi .interpreter_decref
2543
+ get_refcount , incref , decref = self .get_refcount_helpers ()
2540
2544
2541
2545
with self .subTest ('never existed' ):
2542
2546
interpid = _testinternalcapi .unused_interpreter_id ()
@@ -2578,8 +2582,7 @@ def test_linked_lifecycle_initial(self):
2578
2582
get_refcount = _testinternalcapi .get_interpreter_refcount
2579
2583
2580
2584
# A new interpreter will start out not linked, with a refcount of 0.
2581
- interpid = _testinternalcapi .new_interpreter ()
2582
- self .add_interp_cleanup (interpid )
2585
+ interpid = self .new_interpreter ()
2583
2586
linked = is_linked (interpid )
2584
2587
refcount = get_refcount (interpid )
2585
2588
@@ -2589,12 +2592,9 @@ def test_linked_lifecycle_initial(self):
2589
2592
def test_linked_lifecycle_never_linked (self ):
2590
2593
exists = _testinternalcapi .interpreter_exists
2591
2594
is_linked = _testinternalcapi .interpreter_refcount_linked
2592
- get_refcount = _testinternalcapi .get_interpreter_refcount
2593
- incref = _testinternalcapi .interpreter_incref
2594
- decref = _testinternalcapi .interpreter_decref
2595
+ get_refcount , incref , decref = self .get_refcount_helpers ()
2595
2596
2596
- interpid = _testinternalcapi .new_interpreter ()
2597
- self .add_interp_cleanup (interpid )
2597
+ interpid = self .new_interpreter ()
2598
2598
2599
2599
# Incref will not automatically link it.
2600
2600
incref (interpid )
@@ -2618,8 +2618,7 @@ def test_linked_lifecycle_link_unlink(self):
2618
2618
link = _testinternalcapi .link_interpreter_refcount
2619
2619
unlink = _testinternalcapi .unlink_interpreter_refcount
2620
2620
2621
- interpid = _testinternalcapi .new_interpreter ()
2622
- self .add_interp_cleanup (interpid )
2621
+ interpid = self .new_interpreter ()
2623
2622
2624
2623
# Linking at refcount 0 does not destroy the interpreter.
2625
2624
link (interpid )
@@ -2639,12 +2638,9 @@ def test_linked_lifecycle_link_incref_decref(self):
2639
2638
exists = _testinternalcapi .interpreter_exists
2640
2639
is_linked = _testinternalcapi .interpreter_refcount_linked
2641
2640
link = _testinternalcapi .link_interpreter_refcount
2642
- get_refcount = _testinternalcapi .get_interpreter_refcount
2643
- incref = _testinternalcapi .interpreter_incref
2644
- decref = _testinternalcapi .interpreter_decref
2641
+ get_refcount , incref , decref = self .get_refcount_helpers ()
2645
2642
2646
- interpid = _testinternalcapi .new_interpreter ()
2647
- self .add_interp_cleanup (interpid )
2643
+ interpid = self .new_interpreter ()
2648
2644
2649
2645
# Linking it will not change the refcount.
2650
2646
link (interpid )
@@ -2666,11 +2662,9 @@ def test_linked_lifecycle_link_incref_decref(self):
2666
2662
def test_linked_lifecycle_incref_link (self ):
2667
2663
is_linked = _testinternalcapi .interpreter_refcount_linked
2668
2664
link = _testinternalcapi .link_interpreter_refcount
2669
- get_refcount = _testinternalcapi .get_interpreter_refcount
2670
- incref = _testinternalcapi .interpreter_incref
2665
+ get_refcount , incref , _ = self .get_refcount_helpers ()
2671
2666
2672
- interpid = _testinternalcapi .new_interpreter ()
2673
- self .add_interp_cleanup (interpid )
2667
+ interpid = self .new_interpreter ()
2674
2668
2675
2669
incref (interpid )
2676
2670
self .assertEqual (
@@ -2688,12 +2682,9 @@ def test_linked_lifecycle_link_incref_unlink_decref(self):
2688
2682
is_linked = _testinternalcapi .interpreter_refcount_linked
2689
2683
link = _testinternalcapi .link_interpreter_refcount
2690
2684
unlink = _testinternalcapi .unlink_interpreter_refcount
2691
- get_refcount = _testinternalcapi .get_interpreter_refcount
2692
- incref = _testinternalcapi .interpreter_incref
2693
- decref = _testinternalcapi .interpreter_decref
2685
+ get_refcount , incref , decref = self .get_refcount_helpers ()
2694
2686
2695
- interpid = _testinternalcapi .new_interpreter ()
2696
- self .add_interp_cleanup (interpid )
2687
+ interpid = self .new_interpreter ()
2697
2688
2698
2689
link (interpid )
2699
2690
self .assertTrue (
0 commit comments