[Python-checkins] bpo-34536: raise error for invalid _missing_ results (GH-9147) (GH-9978)

Ethan Furman webhook-mailer at python.org
Fri Oct 19 19:49:33 EDT 2018


https://github.com/python/cpython/commit/0f2fc8bee0b435ee2934751264196db30d16ed8a
commit: 0f2fc8bee0b435ee2934751264196db30d16ed8a
branch: 3.7
author: Victor Stinner <vstinner at redhat.com>
committer: Ethan Furman <ethan at stoneleaf.us>
date: 2018-10-19T16:49:30-07:00
summary:

bpo-34536: raise error for invalid _missing_ results (GH-9147) (GH-9978)

* raise exception if _missing_ returns None or invalid type

files:
A Misc/NEWS.d/next/Library/2018-09-11-15-49-09.bpo-34536.3IPIH5.rst
M Lib/enum.py
M Lib/test/test_enum.py

diff --git a/Lib/enum.py b/Lib/enum.py
index 87f36911144a..e5a80cd609d4 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -540,7 +540,25 @@ def __new__(cls, value):
                 if member._value_ == value:
                     return member
         # still not found -- try _missing_ hook
-        return cls._missing_(value)
+        try:
+            exc = None
+            result = cls._missing_(value)
+        except Exception as e:
+            exc = e
+            result = None
+        if isinstance(result, cls):
+            return result
+        else:
+            ve_exc = ValueError("%r is not a valid %s" % (value, cls.__name__))
+            if result is None and exc is None:
+                raise ve_exc
+            elif exc is None:
+                exc = TypeError(
+                        'error in %s._missing_: returned %r instead of None or a valid member'
+                        % (cls.__name__, result)
+                        )
+            exc.__context__ = ve_exc
+            raise exc
 
     def _generate_next_value_(name, start, count, last_values):
         for last_value in reversed(last_values):
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index 60eabbe3d487..b221045328db 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -1717,6 +1717,38 @@ class Dupes(Enum):
             third = auto()
         self.assertEqual([Dupes.first, Dupes.second, Dupes.third], list(Dupes))
 
+    def test_missing(self):
+        class Color(Enum):
+            red = 1
+            green = 2
+            blue = 3
+            @classmethod
+            def _missing_(cls, item):
+                if item == 'three':
+                    return cls.blue
+                elif item == 'bad return':
+                    # trigger internal error
+                    return 5
+                elif item == 'error out':
+                    raise ZeroDivisionError
+                else:
+                    # trigger not found
+                    return None
+        self.assertIs(Color('three'), Color.blue)
+        self.assertRaises(ValueError, Color, 7)
+        try:
+            Color('bad return')
+        except TypeError as exc:
+            self.assertTrue(isinstance(exc.__context__, ValueError))
+        else:
+            raise Exception('Exception not raised.')
+        try:
+            Color('error out')
+        except ZeroDivisionError as exc:
+            self.assertTrue(isinstance(exc.__context__, ValueError))
+        else:
+            raise Exception('Exception not raised.')
+
     def test_multiple_mixin(self):
         class MaxMixin:
             @classproperty
diff --git a/Misc/NEWS.d/next/Library/2018-09-11-15-49-09.bpo-34536.3IPIH5.rst b/Misc/NEWS.d/next/Library/2018-09-11-15-49-09.bpo-34536.3IPIH5.rst
new file mode 100644
index 000000000000..be45eb57cad5
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-09-11-15-49-09.bpo-34536.3IPIH5.rst
@@ -0,0 +1,2 @@
+`Enum._missing_`:  raise `ValueError` if None returned and `TypeError` if
+non-member is returned.



More information about the Python-checkins mailing list