Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ def _is_descriptor(obj):

def _is_dunder(name):
"""Returns True if a __dunder__ name, False otherwise."""
return (name[:2] == name[-2:] == '__' and
name[2:3] != '_' and
name[-3:-2] != '_' and
len(name) > 4)
return (len(name) > 4 and
name[:2] == name[-2:] == '__' and
name[2] != '_' and
name[-3] != '_')


def _is_sunder(name):
"""Returns True if a _sunder_ name, False otherwise."""
return (name[0] == name[-1] == '_' and
return (len(name) > 2 and
name[0] == name[-1] == '_' and
name[1:2] != '_' and
name[-2:-1] != '_' and
len(name) > 2)
name[-2:-1] != '_')


def _make_class_unpicklable(cls):
"""Make the given class un-picklable."""
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2730,6 +2730,18 @@ def cycle_enum():
self.assertEqual(256, len(seen), 'too many composite members created')


class TestEmptyWeirdString(unittest.TestCase):
def test_empty_string(self):
Yay = Enum('Yay', ('', 'B', 'C'))
item = getattr(Yay, '')
self.assertEqual(item.value, 1)

def test_weird_character(self):
Yay = Enum('Yay', ('!', 'B', 'C'))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a test for the original report where empty string causes an error like Yay = Enum('Yay', ('', 'B', 'C')) in https://bugs.python.org/issue35899#msg334866

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @tirkarthi
Thank you for your feedback, I have just added this test.

item = getattr(Yay, '!')
self.assertEqual(item.value, 1)


class TestUnique(unittest.TestCase):

def test_unique_clean(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Enum supports empty and weird strings for the items. Contributed by Maxwell.