@@ -4449,30 +4449,34 @@ def test__all__(self):
4449
4449
COMPLEX_A = 2j
4450
4450
COMPLEX_B = 3j
4451
4451
4452
- class TestIntEnumConvert (unittest .TestCase ):
4453
- def setUp (self ):
4454
- # Reset the module-level test variables to their original integer
4455
- # values, otherwise the already created enum values get converted
4456
- # instead.
4457
- for suffix in ['A' , 'B' , 'C' , 'D' , 'E' , 'F' ]:
4458
- globals ()[f'CONVERT_TEST_NAME_{ suffix } ' ] = 5
4459
- globals ()[f'CONVERT_STRING_TEST_NAME_{ suffix } ' ] = 5
4452
+ class _ModuleWrapper :
4453
+ """We use this class as a namespace for swapping modules."""
4460
4454
4455
+ def __init__ (self , module ):
4456
+ self .__dict__ .update (module .__dict__ )
4457
+
4458
+ class TestIntEnumConvert (unittest .TestCase ):
4461
4459
def test_convert_value_lookup_priority (self ):
4462
- test_type = enum .IntEnum ._convert_ (
4463
- 'UnittestConvert' ,
4464
- MODULE ,
4465
- filter = lambda x : x .startswith ('CONVERT_TEST_' ))
4460
+ with support .swap_item (
4461
+ sys .modules , MODULE , _ModuleWrapper (sys .modules [MODULE ]),
4462
+ ):
4463
+ test_type = enum .IntEnum ._convert_ (
4464
+ 'UnittestConvert' ,
4465
+ MODULE ,
4466
+ filter = lambda x : x .startswith ('CONVERT_TEST_' ))
4466
4467
# We don't want the reverse lookup value to vary when there are
4467
4468
# multiple possible names for a given value. It should always
4468
4469
# report the first lexigraphical name in that case.
4469
4470
self .assertEqual (test_type (5 ).name , 'CONVERT_TEST_NAME_A' )
4470
4471
4471
4472
def test_convert (self ):
4472
- test_type = enum .IntEnum ._convert_ (
4473
- 'UnittestConvert' ,
4474
- MODULE ,
4475
- filter = lambda x : x .startswith ('CONVERT_TEST_' ))
4473
+ with support .swap_item (
4474
+ sys .modules , MODULE , _ModuleWrapper (sys .modules [MODULE ]),
4475
+ ):
4476
+ test_type = enum .IntEnum ._convert_ (
4477
+ 'UnittestConvert' ,
4478
+ MODULE ,
4479
+ filter = lambda x : x .startswith ('CONVERT_TEST_' ))
4476
4480
# Ensure that test_type has all of the desired names and values.
4477
4481
self .assertEqual (test_type .CONVERT_TEST_NAME_F ,
4478
4482
test_type .CONVERT_TEST_NAME_A )
@@ -4487,11 +4491,16 @@ def test_convert(self):
4487
4491
[], msg = 'Names other than CONVERT_TEST_* found.' )
4488
4492
4489
4493
def test_convert_uncomparable (self ):
4490
- uncomp = enum .Enum ._convert_ (
4491
- 'Uncomparable' ,
4492
- MODULE ,
4493
- filter = lambda x : x .startswith ('UNCOMPARABLE_' ),
4494
- )
4494
+ # We swap a module to some other object with `__dict__`
4495
+ # because otherwise refleak is created.
4496
+ # `_convert_` uses a module side effect that does this. See 30472
4497
+ with support .swap_item (
4498
+ sys .modules , MODULE , _ModuleWrapper (sys .modules [MODULE ]),
4499
+ ):
4500
+ uncomp = enum .Enum ._convert_ (
4501
+ 'Uncomparable' ,
4502
+ MODULE ,
4503
+ filter = lambda x : x .startswith ('UNCOMPARABLE_' ))
4495
4504
4496
4505
# Should be ordered by `name` only:
4497
4506
self .assertEqual (
@@ -4500,11 +4509,13 @@ def test_convert_uncomparable(self):
4500
4509
)
4501
4510
4502
4511
def test_convert_complex (self ):
4503
- uncomp = enum .Enum ._convert_ (
4504
- 'Uncomparable' ,
4505
- MODULE ,
4506
- filter = lambda x : x .startswith ('COMPLEX_' ),
4507
- )
4512
+ with support .swap_item (
4513
+ sys .modules , MODULE , _ModuleWrapper (sys .modules [MODULE ]),
4514
+ ):
4515
+ uncomp = enum .Enum ._convert_ (
4516
+ 'Uncomparable' ,
4517
+ MODULE ,
4518
+ filter = lambda x : x .startswith ('COMPLEX_' ))
4508
4519
4509
4520
# Should be ordered by `name` only:
4510
4521
self .assertEqual (
@@ -4531,10 +4542,13 @@ def test_convert_raise(self):
4531
4542
filter = lambda x : x .startswith ('CONVERT_TEST_' ))
4532
4543
4533
4544
def test_convert_repr_and_str (self ):
4534
- test_type = enum .IntEnum ._convert_ (
4535
- 'UnittestConvert' ,
4536
- MODULE ,
4537
- filter = lambda x : x .startswith ('CONVERT_STRING_TEST_' ))
4545
+ with support .swap_item (
4546
+ sys .modules , MODULE , _ModuleWrapper (sys .modules [MODULE ]),
4547
+ ):
4548
+ test_type = enum .IntEnum ._convert_ (
4549
+ 'UnittestConvert' ,
4550
+ MODULE ,
4551
+ filter = lambda x : x .startswith ('CONVERT_STRING_TEST_' ))
4538
4552
self .assertEqual (repr (test_type .CONVERT_STRING_TEST_NAME_A ), '%s.CONVERT_STRING_TEST_NAME_A' % SHORT_MODULE )
4539
4553
self .assertEqual (str (test_type .CONVERT_STRING_TEST_NAME_A ), 'CONVERT_STRING_TEST_NAME_A' )
4540
4554
self .assertEqual (format (test_type .CONVERT_STRING_TEST_NAME_A ), '5' )
@@ -4544,17 +4558,14 @@ def test_convert_repr_and_str(self):
4544
4558
CONVERT_STR_TEST_1 = 'hello'
4545
4559
4546
4560
class TestStrEnumConvert (unittest .TestCase ):
4547
- def setUp (self ):
4548
- global CONVERT_STR_TEST_1
4549
- global CONVERT_STR_TEST_2
4550
- CONVERT_STR_TEST_2 = 'goodbye'
4551
- CONVERT_STR_TEST_1 = 'hello'
4552
-
4553
4561
def test_convert (self ):
4554
- test_type = enum .StrEnum ._convert_ (
4555
- 'UnittestConvert' ,
4556
- MODULE ,
4557
- filter = lambda x : x .startswith ('CONVERT_STR_' ))
4562
+ with support .swap_item (
4563
+ sys .modules , MODULE , _ModuleWrapper (sys .modules [MODULE ]),
4564
+ ):
4565
+ test_type = enum .StrEnum ._convert_ (
4566
+ 'UnittestConvert' ,
4567
+ MODULE ,
4568
+ filter = lambda x : x .startswith ('CONVERT_STR_' ))
4558
4569
# Ensure that test_type has all of the desired names and values.
4559
4570
self .assertEqual (test_type .CONVERT_STR_TEST_1 , 'hello' )
4560
4571
self .assertEqual (test_type .CONVERT_STR_TEST_2 , 'goodbye' )
@@ -4565,10 +4576,13 @@ def test_convert(self):
4565
4576
[], msg = 'Names other than CONVERT_STR_* found.' )
4566
4577
4567
4578
def test_convert_repr_and_str (self ):
4568
- test_type = enum .StrEnum ._convert_ (
4569
- 'UnittestConvert' ,
4570
- MODULE ,
4571
- filter = lambda x : x .startswith ('CONVERT_STR_' ))
4579
+ with support .swap_item (
4580
+ sys .modules , MODULE , _ModuleWrapper (sys .modules [MODULE ]),
4581
+ ):
4582
+ test_type = enum .StrEnum ._convert_ (
4583
+ 'UnittestConvert' ,
4584
+ MODULE ,
4585
+ filter = lambda x : x .startswith ('CONVERT_STR_' ))
4572
4586
self .assertEqual (repr (test_type .CONVERT_STR_TEST_1 ), '%s.CONVERT_STR_TEST_1' % SHORT_MODULE )
4573
4587
self .assertEqual (str (test_type .CONVERT_STR_TEST_2 ), 'goodbye' )
4574
4588
self .assertEqual (format (test_type .CONVERT_STR_TEST_1 ), 'hello' )
0 commit comments