Skip to content

Commit 8755f0a

Browse files
bpo-35899: Fix Enum handling of empty and weird strings (GH-11891)
Co-authored-by: Maxwell <[email protected]> Co-authored-by: Stéphane Wirtel <[email protected]> https://bugs.python.org/issue35899 (cherry picked from commit 8b914d2) Co-authored-by: Brennan D Baraban <[email protected]>
1 parent 02351ed commit 8755f0a

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

Lib/enum.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,19 @@ def _is_descriptor(obj):
2525

2626
def _is_dunder(name):
2727
"""Returns True if a __dunder__ name, False otherwise."""
28-
return (name[:2] == name[-2:] == '__' and
29-
name[2:3] != '_' and
30-
name[-3:-2] != '_' and
31-
len(name) > 4)
28+
return (len(name) > 4 and
29+
name[:2] == name[-2:] == '__' and
30+
name[2] != '_' and
31+
name[-3] != '_')
3232

3333

3434
def _is_sunder(name):
3535
"""Returns True if a _sunder_ name, False otherwise."""
36-
return (name[0] == name[-1] == '_' and
36+
return (len(name) > 2 and
37+
name[0] == name[-1] == '_' and
3738
name[1:2] != '_' and
38-
name[-2:-1] != '_' and
39-
len(name) > 2)
39+
name[-2:-1] != '_')
40+
4041

4142
def _make_class_unpicklable(cls):
4243
"""Make the given class un-picklable."""
@@ -156,7 +157,7 @@ def __new__(metacls, cls, bases, classdict):
156157
_order_ = classdict.pop('_order_', None)
157158

158159
# check for illegal enum names (any others?)
159-
invalid_names = set(enum_members) & {'mro', }
160+
invalid_names = set(enum_members) & {'mro', ''}
160161
if invalid_names:
161162
raise ValueError('Invalid enum member name: {0}'.format(
162163
','.join(invalid_names)))

Lib/test/test_enum.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2738,6 +2738,23 @@ def cycle_enum():
27382738
self.assertEqual(256, len(seen), 'too many composite members created')
27392739

27402740

2741+
class TestEmptyAndNonLatinStrings(unittest.TestCase):
2742+
2743+
def test_empty_string(self):
2744+
with self.assertRaises(ValueError):
2745+
empty_abc = Enum('empty_abc', ('', 'B', 'C'))
2746+
2747+
def test_non_latin_character_string(self):
2748+
greek_abc = Enum('greek_abc', ('\u03B1', 'B', 'C'))
2749+
item = getattr(greek_abc, '\u03B1')
2750+
self.assertEqual(item.value, 1)
2751+
2752+
def test_non_latin_number_string(self):
2753+
hebrew_123 = Enum('hebrew_123', ('\u05D0', '2', '3'))
2754+
item = getattr(hebrew_123, '\u05D0')
2755+
self.assertEqual(item.value, 1)
2756+
2757+
27412758
class TestUnique(unittest.TestCase):
27422759

27432760
def test_unique_clean(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Enum has been fixed to correctly handle empty strings and strings with non-Latin characters (ie. 'α', 'א') without crashing. Original patch contributed by Maxwell. Assisted by Stéphane Wirtel.

0 commit comments

Comments
 (0)