From dab01b9ba04c0481ab9e9fc81114cf5e5844ed9d Mon Sep 17 00:00:00 2001 From: Brennan D Baraban <375@holbertonschool.com> Date: Fri, 15 Feb 2019 23:03:47 -0800 Subject: [PATCH 1/6] bpo-35899: Fix Enum handling of empty and weird strings 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 --- Lib/enum.py | 15 ++++++++------- Lib/test/test_enum.py | 13 +++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Lib/enum.py b/Lib/enum.py index a958ed8748afea..6a3348c8d9f430 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -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.""" diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 99fc85074b703b..892a162976514b 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -2730,6 +2730,19 @@ def cycle_enum(): self.assertEqual(256, len(seen), 'too many composite members created') +class TestEmptyAndWeirdString(unittest.TestCase): + + def test_empty_string(self): + empty_abc = Enum('empty_abc', ('', 'B', 'C')) + item = getattr(empty_abc, '') + self.assertEqual(item.value, 1) + + def test_weird_character_string(self): + weird_abc = Enum('weird_abc', ('!', 'B', 'C')) + item = getattr(weird_abc, '!') + self.assertEqual(item.value, 1) + + class TestUnique(unittest.TestCase): def test_unique_clean(self): From 6a32331fe248a502343f74c6ce20a04b9cad54b6 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" Date: Sat, 16 Feb 2019 07:11:03 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2019-02-16-07-11-02.bpo-35899.cjfn5a.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2019-02-16-07-11-02.bpo-35899.cjfn5a.rst 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..93e3937b4849bd --- /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 "weird" characters (ie. '!') without crashing. Original patch contributed by Maxwell. Assisted by Stéphane Wirtel. \ No newline at end of file From 021d03b2b806a915ef2e869b0dd8696dffda6ead Mon Sep 17 00:00:00 2001 From: Brennan D Baraban <375@holbertonschool.com> Date: Fri, 22 Feb 2019 15:18:18 -0800 Subject: [PATCH 3/6] bpo-35899: Prevent enum from handling empty strings --- Lib/enum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/enum.py b/Lib/enum.py index 6a3348c8d9f430..6ef17c7f6dc846 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -151,7 +151,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))) From 5fa3c16b758821ea2ce6adee2b27029f19766b86 Mon Sep 17 00:00:00 2001 From: Brennan D Baraban <375@holbertonschool.com> Date: Fri, 22 Feb 2019 15:19:14 -0800 Subject: [PATCH 4/6] bpo-35899: Fix empty string test and add tests for non-latin chars and numbers --- Lib/test/test_enum.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 892a162976514b..ff64fbaa0d3b9d 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -2730,16 +2730,20 @@ def cycle_enum(): self.assertEqual(256, len(seen), 'too many composite members created') -class TestEmptyAndWeirdString(unittest.TestCase): +class TestEmptyAndNonLatinStrings(unittest.TestCase): def test_empty_string(self): - empty_abc = Enum('empty_abc', ('', 'B', 'C')) - item = getattr(empty_abc, '') + with self.assertRaises(ValueError): + empty_abc = Enum('empty_abc', ('', 'B', 'C')) + + def test_non_latin_character_string(self): + greek_abc = Enum('greek_abc', ('α', 'B', 'C')) + item = getattr(greek_abc, 'α') self.assertEqual(item.value, 1) - def test_weird_character_string(self): - weird_abc = Enum('weird_abc', ('!', 'B', 'C')) - item = getattr(weird_abc, '!') + def test_non_latin_number_string(self): + hebrew_123 = Enum('hebrew_123', ('א', '2', '3')) + item = getattr(hebrew_123, 'א') self.assertEqual(item.value, 1) From 2ef1f740aea30829e60fbc271a5b2d58d6743f6f Mon Sep 17 00:00:00 2001 From: Brennan D Baraban <375@holbertonschool.com> Date: Fri, 22 Feb 2019 15:48:30 -0800 Subject: [PATCH 5/6] bpo-35899: Use unicode numbers instead of string literals for TestEmptyAndNonLatinStrings --- Lib/test/test_enum.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index ff64fbaa0d3b9d..770decf2f4bfe7 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -2737,13 +2737,13 @@ def test_empty_string(self): empty_abc = Enum('empty_abc', ('', 'B', 'C')) def test_non_latin_character_string(self): - greek_abc = Enum('greek_abc', ('α', 'B', 'C')) - item = getattr(greek_abc, 'α') + 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', ('א', '2', '3')) - item = getattr(hebrew_123, 'א') + hebrew_123 = Enum('hebrew_123', ('\u05D0', '2', '3')) + item = getattr(hebrew_123, '\u05D0') self.assertEqual(item.value, 1) From fd68b9848f989b9eb9f2acf1dffab65a4eed00ba Mon Sep 17 00:00:00 2001 From: Brennan D Baraban <375@holbertonschool.com> Date: Sun, 3 Mar 2019 13:49:12 -0800 Subject: [PATCH 6/6] bpo-35899: Update news blurb to reflect specification of non-Latin character handling --- .../next/Library/2019-02-16-07-11-02.bpo-35899.cjfn5a.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 93e3937b4849bd..73d4fa17b33d7c 100644 --- 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 @@ -1 +1 @@ -Enum has been fixed to correctly handle empty strings and strings with "weird" characters (ie. '!') without crashing. Original patch contributed by Maxwell. Assisted by Stéphane Wirtel. \ No newline at end of file +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.