[Python-checkins] gh-102549: [Enum] fail enum creation when data type raises in __init__ (GH-103149)
ethanfurman
webhook-mailer at python.org
Fri Mar 31 16:52:58 EDT 2023
https://github.com/python/cpython/commit/2a4d8c0a9e88f45047da640ce5a92b304d2d39b1
commit: 2a4d8c0a9e88f45047da640ce5a92b304d2d39b1
branch: main
author: Ethan Furman <ethan at stoneleaf.us>
committer: ethanfurman <ethan at stoneleaf.us>
date: 2023-03-31T13:52:31-07:00
summary:
gh-102549: [Enum] fail enum creation when data type raises in __init__ (GH-103149)
files:
A Misc/NEWS.d/next/Library/2023-03-27-19-21-51.gh-issue-102549.NQ6Nlv.rst
M Lib/enum.py
M Lib/test/test_enum.py
diff --git a/Lib/enum.py b/Lib/enum.py
index 8c77117ce6ac..4e231e7e8ea7 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -266,23 +266,20 @@ def __set_name__(self, enum_class, member_name):
args = (args, ) # wrap it one more time
if not enum_class._use_args_:
enum_member = enum_class._new_member_(enum_class)
- if not hasattr(enum_member, '_value_'):
+ else:
+ enum_member = enum_class._new_member_(enum_class, *args)
+ if not hasattr(enum_member, '_value_'):
+ if enum_class._member_type_ is object:
+ enum_member._value_ = value
+ else:
try:
enum_member._value_ = enum_class._member_type_(*args)
except Exception as exc:
- enum_member._value_ = value
- else:
- enum_member = enum_class._new_member_(enum_class, *args)
- if not hasattr(enum_member, '_value_'):
- if enum_class._member_type_ is object:
- enum_member._value_ = value
- else:
- try:
- enum_member._value_ = enum_class._member_type_(*args)
- except Exception as exc:
- raise TypeError(
- '_value_ not set in __new__, unable to create it'
- ) from None
+ new_exc = TypeError(
+ '_value_ not set in __new__, unable to create it'
+ )
+ new_exc.__cause__ = exc
+ raise new_exc
value = enum_member._value_
enum_member._name_ = member_name
enum_member.__objclass__ = enum_class
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index bea19542705d..ee5280601be1 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -2916,6 +2916,26 @@ def __new__(cls, c):
self.assertEqual(FlagFromChar.a, 158456325028528675187087900672)
self.assertEqual(FlagFromChar.a|1, 158456325028528675187087900673)
+ def test_init_exception(self):
+ class Base:
+ def __init__(self, x):
+ raise ValueError("I don't like", x)
+ with self.assertRaises(TypeError):
+ class MyEnum(Base, enum.Enum):
+ A = 'a'
+ def __init__(self, y):
+ self.y = y
+ with self.assertRaises(ValueError):
+ class MyEnum(Base, enum.Enum):
+ A = 'a'
+ def __init__(self, y):
+ self.y = y
+ def __new__(cls, value):
+ member = Base.__new__(cls)
+ member._value_ = Base(value)
+ return member
+
+
class TestOrder(unittest.TestCase):
"test usage of the `_order_` attribute"
diff --git a/Misc/NEWS.d/next/Library/2023-03-27-19-21-51.gh-issue-102549.NQ6Nlv.rst b/Misc/NEWS.d/next/Library/2023-03-27-19-21-51.gh-issue-102549.NQ6Nlv.rst
new file mode 100644
index 000000000000..e4def038175b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-03-27-19-21-51.gh-issue-102549.NQ6Nlv.rst
@@ -0,0 +1 @@
+Don't ignore exceptions in member type creation.
More information about the Python-checkins
mailing list