mirror of
https://github.com/python/cpython.git
synced 2025-09-14 20:56:06 +00:00
bpo-44242: [Enum] improve error messages (GH-26669)
(cherry picked from commit c956734d7a
)
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
This commit is contained in:
parent
e6d28a1a6a
commit
0a186b1ec1
2 changed files with 30 additions and 19 deletions
37
Lib/enum.py
37
Lib/enum.py
|
@ -1,5 +1,7 @@
|
|||
import sys
|
||||
from types import MappingProxyType, DynamicClassAttribute
|
||||
from operator import or_ as _or_
|
||||
from functools import reduce
|
||||
from builtins import property as _bltin_property, bin as _bltin_bin
|
||||
|
||||
|
||||
|
@ -97,6 +99,9 @@ def _iter_bits_lsb(num):
|
|||
yield b
|
||||
num ^= b
|
||||
|
||||
def show_flag_values(value):
|
||||
return list(_iter_bits_lsb(value))
|
||||
|
||||
def bin(num, max_bits=None):
|
||||
"""
|
||||
Like built-in bin(), except negative values are represented in
|
||||
|
@ -1601,14 +1606,16 @@ class verify:
|
|||
else:
|
||||
raise Exception('verify: unknown type %r' % enum_type)
|
||||
if missing:
|
||||
raise ValueError('invalid %s %r: missing values %s' % (
|
||||
raise ValueError(('invalid %s %r: missing values %s' % (
|
||||
enum_type, cls_name, ', '.join((str(m) for m in missing)))
|
||||
)
|
||||
)[:256])
|
||||
# limit max length to protect against DOS attacks
|
||||
elif check is NAMED_FLAGS:
|
||||
# examine each alias and check for unnamed flags
|
||||
member_names = enumeration._member_names_
|
||||
member_values = [m.value for m in enumeration]
|
||||
missing = []
|
||||
missing_names = []
|
||||
missing_value = 0
|
||||
for name, alias in enumeration._member_map_.items():
|
||||
if name in member_names:
|
||||
# not an alias
|
||||
|
@ -1616,16 +1623,22 @@ class verify:
|
|||
values = list(_iter_bits_lsb(alias.value))
|
||||
missed = [v for v in values if v not in member_values]
|
||||
if missed:
|
||||
plural = ('', 's')[len(missed) > 1]
|
||||
a = ('a ', '')[len(missed) > 1]
|
||||
missing.append('%r is missing %snamed flag%s for value%s %s' % (
|
||||
name, a, plural, plural,
|
||||
', '.join(str(v) for v in missed)
|
||||
))
|
||||
if missing:
|
||||
missing_names.append(name)
|
||||
missing_value |= reduce(_or_, missed)
|
||||
if missing_names:
|
||||
if len(missing_names) == 1:
|
||||
alias = 'alias %s is missing' % missing_names[0]
|
||||
else:
|
||||
alias = 'aliases %s and %s are missing' % (
|
||||
', '.join(missing_names[:-1]), missing_names[-1]
|
||||
)
|
||||
if _is_single_bit(missing_value):
|
||||
value = 'value 0x%x' % missing_value
|
||||
else:
|
||||
value = 'combined values of 0x%x' % missing_value
|
||||
raise ValueError(
|
||||
'invalid Flag %r: %s'
|
||||
% (cls_name, '; '.join(missing))
|
||||
'invalid Flag %r: %s %s [use `enum.show_flag_values(value)` for details]'
|
||||
% (cls_name, alias, value)
|
||||
)
|
||||
return enumeration
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue