diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-05-27 13:12:08 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-27 13:12:08 -0700 |
commit | b5ecbf02e4dbdea6d1c9a6d7189137f76e70c073 (patch) | |
tree | 7391dc043cb567fcc4753dd31bf6cdc03a99226a /Lib/enum.py | |
parent | bpo-13097: ctypes: limit callback to 1024 arguments (GH-19914) (diff) | |
download | cpython-b5ecbf02e4dbdea6d1c9a6d7189137f76e70c073.tar.gz cpython-b5ecbf02e4dbdea6d1c9a6d7189137f76e70c073.tar.bz2 cpython-b5ecbf02e4dbdea6d1c9a6d7189137f76e70c073.zip |
bpo-40025: Require _generate_next_value_ to be defined before members(GH-19763)
require `_generate_next_value_` to be defined before members
Diffstat (limited to 'Lib/enum.py')
-rw-r--r-- | Lib/enum.py | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/Lib/enum.py b/Lib/enum.py index 108d389d948..14cc00e7839 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -60,6 +60,7 @@ class _EnumDict(dict): self._member_names = [] self._last_values = [] self._ignore = [] + self._auto_called = False def __setitem__(self, key, value): """Changes anything not dundered or not a descriptor. @@ -77,6 +78,9 @@ class _EnumDict(dict): ): raise ValueError('_names_ are reserved for future Enum use') if key == '_generate_next_value_': + # check if members already defined as auto() + if self._auto_called: + raise TypeError("_generate_next_value_ must be defined before members") setattr(self, '_generate_next_value', value) elif key == '_ignore_': if isinstance(value, str): @@ -100,6 +104,7 @@ class _EnumDict(dict): # enum overwriting a descriptor? raise TypeError('%r already defined as: %r' % (key, self[key])) if isinstance(value, auto): + self._auto_called = True if value.value == _auto_null: value.value = self._generate_next_value(key, 1, len(self._member_names), self._last_values[:]) value = value.value |