cpython: 1d88d04aade2 (original) (raw)
Mercurial > cpython
changeset 85574:1d88d04aade2
Close #18924: Block naive attempts to change an Enum member. [#18924]
Ethan Furman ethan@stoneleaf.us | |
---|---|
date | Fri, 06 Sep 2013 07:16:48 -0700 |
parents | 587bdb940524 |
children | 3070fdd58645 |
files | Lib/enum.py Lib/test/test_enum.py |
diffstat | 2 files changed, 18 insertions(+), 0 deletions(-)[+] [-] Lib/enum.py 13 Lib/test/test_enum.py 5 |
line wrap: on
line diff
--- a/Lib/enum.py +++ b/Lib/enum.py @@ -263,6 +263,19 @@ class EnumMeta(type): def repr(cls): return "<enum %r>" % cls.name
A simple assignment to the class namespace only changes one of the[](#l1.10)
several possible ways to get an Enum member from the Enum class,[](#l1.11)
resulting in an inconsistent Enumeration.[](#l1.12)
"""[](#l1.14)
member_map = cls.__dict__.get('_member_map_', {})[](#l1.15)
if name in member_map:[](#l1.16)
raise AttributeError('Cannot reassign members.')[](#l1.17)
super().__setattr__(name, value)[](#l1.18)
+ def create(cls, class_name, names=None, *, module=None, type=None): """Convenience method to create a new Enum class.
--- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -152,6 +152,11 @@ class TestEnum(unittest.TestCase): with self.assertRaises(AttributeError): Season.SPRING.value = 2
- def test_changing_member(self):
Season = self.Season[](#l2.8)
with self.assertRaises(AttributeError):[](#l2.9)
Season.WINTER = 'really cold'[](#l2.10)
+ def test_invalid_names(self): with self.assertRaises(ValueError): class Wrong(Enum):