Skip to content

Commit 37b173c

Browse files
authored
bpo-38659: Properly re-intialize module variables in test_enum (pythonGH-25516)
Previously TestIntEnumConvert and TestStrEnumConvert would end up converting the module level variables from their regular int form to a `test.test_enum.X` instance after _convert would run. This meant that after a single test ran, the next set of _convert functions would be operating on the enum instances rather than ints. This would cause some tests such as the one involving format to fail when running under a mode that repeatedly runs test such as the refleak finder.
1 parent dc516ef commit 37b173c

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

Lib/test/test_enum.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -3642,6 +3642,14 @@ def test__all__(self):
36423642
CONVERT_STRING_TEST_NAME_F = 5
36433643

36443644
class TestIntEnumConvert(unittest.TestCase):
3645+
def setUp(self):
3646+
# Reset the module-level test variables to their original integer
3647+
# values, otherwise the already created enum values get converted
3648+
# instead.
3649+
for suffix in ['A', 'B', 'C', 'D', 'E', 'F']:
3650+
globals()[f'CONVERT_TEST_NAME_{suffix}'] = 5
3651+
globals()[f'CONVERT_STRING_TEST_NAME_{suffix}'] = 5
3652+
36453653
def test_convert_value_lookup_priority(self):
36463654
test_type = enum.IntEnum._convert_(
36473655
'UnittestConvert',
@@ -3688,8 +3696,6 @@ def test_convert_raise(self):
36883696
filter=lambda x: x.startswith('CONVERT_TEST_'))
36893697

36903698
def test_convert_repr_and_str(self):
3691-
# reset global constants, as previous tests could have converted the
3692-
# integer values to enums
36933699
module = ('test.test_enum', '__main__')[__name__=='__main__']
36943700
test_type = enum.IntEnum._convert_(
36953701
'UnittestConvert',
@@ -3704,6 +3710,11 @@ def test_convert_repr_and_str(self):
37043710
CONVERT_STR_TEST_1 = 'hello'
37053711

37063712
class TestStrEnumConvert(unittest.TestCase):
3713+
def setUp(self):
3714+
global CONVERT_STR_TEST_1
3715+
global CONVERT_STR_TEST_2
3716+
CONVERT_STR_TEST_2 = 'goodbye'
3717+
CONVERT_STR_TEST_1 = 'hello'
37073718

37083719
def test_convert(self):
37093720
test_type = enum.StrEnum._convert_(

0 commit comments

Comments
 (0)