@@ -4449,30 +4449,34 @@ def test__all__(self):
44494449COMPLEX_A = 2j
44504450COMPLEX_B = 3j
44514451
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."""
44604454
4455+ def __init__ (self , module ):
4456+ self .__dict__ .update (module .__dict__ )
4457+
4458+ class TestIntEnumConvert (unittest .TestCase ):
44614459 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_' ))
44664467 # We don't want the reverse lookup value to vary when there are
44674468 # multiple possible names for a given value. It should always
44684469 # report the first lexigraphical name in that case.
44694470 self .assertEqual (test_type (5 ).name , 'CONVERT_TEST_NAME_A' )
44704471
44714472 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_' ))
44764480 # Ensure that test_type has all of the desired names and values.
44774481 self .assertEqual (test_type .CONVERT_TEST_NAME_F ,
44784482 test_type .CONVERT_TEST_NAME_A )
@@ -4487,11 +4491,16 @@ def test_convert(self):
44874491 [], msg = 'Names other than CONVERT_TEST_* found.' )
44884492
44894493 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_' ))
44954504
44964505 # Should be ordered by `name` only:
44974506 self .assertEqual (
@@ -4500,11 +4509,13 @@ def test_convert_uncomparable(self):
45004509 )
45014510
45024511 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_' ))
45084519
45094520 # Should be ordered by `name` only:
45104521 self .assertEqual (
@@ -4531,10 +4542,13 @@ def test_convert_raise(self):
45314542 filter = lambda x : x .startswith ('CONVERT_TEST_' ))
45324543
45334544 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_' ))
45384552 self .assertEqual (repr (test_type .CONVERT_STRING_TEST_NAME_A ), '%s.CONVERT_STRING_TEST_NAME_A' % SHORT_MODULE )
45394553 self .assertEqual (str (test_type .CONVERT_STRING_TEST_NAME_A ), 'CONVERT_STRING_TEST_NAME_A' )
45404554 self .assertEqual (format (test_type .CONVERT_STRING_TEST_NAME_A ), '5' )
@@ -4544,17 +4558,14 @@ def test_convert_repr_and_str(self):
45444558CONVERT_STR_TEST_1 = 'hello'
45454559
45464560class 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-
45534561 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_' ))
45584569 # Ensure that test_type has all of the desired names and values.
45594570 self .assertEqual (test_type .CONVERT_STR_TEST_1 , 'hello' )
45604571 self .assertEqual (test_type .CONVERT_STR_TEST_2 , 'goodbye' )
@@ -4565,10 +4576,13 @@ def test_convert(self):
45654576 [], msg = 'Names other than CONVERT_STR_* found.' )
45664577
45674578 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_' ))
45724586 self .assertEqual (repr (test_type .CONVERT_STR_TEST_1 ), '%s.CONVERT_STR_TEST_1' % SHORT_MODULE )
45734587 self .assertEqual (str (test_type .CONVERT_STR_TEST_2 ), 'goodbye' )
45744588 self .assertEqual (format (test_type .CONVERT_STR_TEST_1 ), 'hello' )
0 commit comments