From 6d079ff34cc3a6a66a2961d9838238a2b8e70844 Mon Sep 17 00:00:00 2001 From: Brennan D Baraban <34765317+bdbaraban@users.noreply.github.com> Date: Sun, 3 Mar 2019 14:09:11 -0800 Subject: [PATCH] bpo-35899: Fix Enum handling of empty and weird strings (GH-11891) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Maxwell Co-authored-by: Stéphane Wirtel https://bugs.python.org/issue35899 (cherry picked from commit 8b914d2767acba3a9e78f1dacdc2d61dbfd7e304) Co-authored-by: Brennan D Baraban <34765317+bdbaraban@users.noreply.github.com> --- Lib/enum.py | 17 +++++++++-------- Lib/test/test_enum.py | 17 +++++++++++++++++ .../2019-02-16-07-11-02.bpo-35899.cjfn5a.rst | 1 + 3 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2019-02-16-07-11-02.bpo-35899.cjfn5a.rst diff --git a/Lib/enum.py b/Lib/enum.py index 8405fa965d063f..f6a483fd4cb6ea 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -27,18 +27,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.""" @@ -140,7 +141,7 @@ def __new__(metacls, cls, bases, classdict): _order_ = classdict.pop('_order_', None) # check for illegal enum names (any others?) - invalid_names = set(enum_members) & {'mro', } + invalid_names = set(enum_members) & {'mro', ''} if invalid_names: raise ValueError('Invalid enum member name: {0}'.format( ','.join(invalid_names))) diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 3cecee6916262e..c8baedbeb7ec41 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -2396,6 +2396,23 @@ def cycle_enum(): self.assertEqual(256, len(seen), 'too many composite members created') +class TestEmptyAndNonLatinStrings(unittest.TestCase): + + def test_empty_string(self): + with self.assertRaises(ValueError): + empty_abc = Enum('empty_abc', ('', 'B', 'C')) + + def test_non_latin_character_string(self): + greek_abc = Enum('greek_abc', ('\u03B1', 'B', 'C')) + item = getattr(greek_abc, '\u03B1') + self.assertEqual(item.value, 1) + + def test_non_latin_number_string(self): + hebrew_123 = Enum('hebrew_123', ('\u05D0', '2', '3')) + item = getattr(hebrew_123, '\u05D0') + self.assertEqual(item.value, 1) + + class TestUnique(unittest.TestCase): def test_unique_clean(self): diff --git a/Misc/NEWS.d/next/Library/2019-02-16-07-11-02.bpo-35899.cjfn5a.rst b/Misc/NEWS.d/next/Library/2019-02-16-07-11-02.bpo-35899.cjfn5a.rst new file mode 100644 index 00000000000000..73d4fa17b33d7c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-02-16-07-11-02.bpo-35899.cjfn5a.rst @@ -0,0 +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. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy