diff options
author | Ethan Furman <ethan@stoneleaf.us> | 2013-09-06 07:16:48 -0700 |
---|---|---|
committer | Ethan Furman <ethan@stoneleaf.us> | 2013-09-06 07:16:48 -0700 |
commit | f203f2d51da404fd59cbfbf40201fe30bd9ea79c (patch) | |
tree | cd0221640a9d9445eada6cfcc21128ff72ec68d3 /Lib/enum.py | |
parent | 96d848ace4785077c2852ede46895775854be676 (diff) | |
download | cpython-git-f203f2d51da404fd59cbfbf40201fe30bd9ea79c.tar.gz |
Close #18924: Block naive attempts to change an Enum member.
Diffstat (limited to 'Lib/enum.py')
-rw-r--r-- | Lib/enum.py | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/Lib/enum.py b/Lib/enum.py index 8219005bb6..82058ae924 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -263,6 +263,19 @@ class EnumMeta(type): def __repr__(cls): return "<enum %r>" % cls.__name__ + def __setattr__(cls, name, value): + """Block attempts to reassign Enum members. + + A simple assignment to the class namespace only changes one of the + several possible ways to get an Enum member from the Enum class, + resulting in an inconsistent Enumeration. + + """ + member_map = cls.__dict__.get('_member_map_', {}) + if name in member_map: + raise AttributeError('Cannot reassign members.') + super().__setattr__(name, value) + def _create_(cls, class_name, names=None, *, module=None, type=None): """Convenience method to create a new Enum class. |