Skip to content

Commit c956734

Browse files
authored
bpo-44242: [Enum] improve error messages (GH-26669)
1 parent 304dfec commit c956734

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

Lib/enum.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import sys
22
from types import MappingProxyType, DynamicClassAttribute
3+
from operator import or_ as _or_
4+
from functools import reduce
35
from builtins import property as _bltin_property, bin as _bltin_bin
46

57

@@ -97,6 +99,9 @@ def _iter_bits_lsb(num):
9799
yield b
98100
num ^= b
99101

102+
def show_flag_values(value):
103+
return list(_iter_bits_lsb(value))
104+
100105
def bin(num, max_bits=None):
101106
"""
102107
Like built-in bin(), except negative values are represented in
@@ -1601,31 +1606,39 @@ def __call__(self, enumeration):
16011606
else:
16021607
raise Exception('verify: unknown type %r' % enum_type)
16031608
if missing:
1604-
raise ValueError('invalid %s %r: missing values %s' % (
1609+
raise ValueError(('invalid %s %r: missing values %s' % (
16051610
enum_type, cls_name, ', '.join((str(m) for m in missing)))
1606-
)
1611+
)[:256])
1612+
# limit max length to protect against DOS attacks
16071613
elif check is NAMED_FLAGS:
16081614
# examine each alias and check for unnamed flags
16091615
member_names = enumeration._member_names_
16101616
member_values = [m.value for m in enumeration]
1611-
missing = []
1617+
missing_names = []
1618+
missing_value = 0
16121619
for name, alias in enumeration._member_map_.items():
16131620
if name in member_names:
16141621
# not an alias
16151622
continue
16161623
values = list(_iter_bits_lsb(alias.value))
16171624
missed = [v for v in values if v not in member_values]
16181625
if missed:
1619-
plural = ('', 's')[len(missed) > 1]
1620-
a = ('a ', '')[len(missed) > 1]
1621-
missing.append('%r is missing %snamed flag%s for value%s %s' % (
1622-
name, a, plural, plural,
1623-
', '.join(str(v) for v in missed)
1624-
))
1625-
if missing:
1626+
missing_names.append(name)
1627+
missing_value |= reduce(_or_, missed)
1628+
if missing_names:
1629+
if len(missing_names) == 1:
1630+
alias = 'alias %s is missing' % missing_names[0]
1631+
else:
1632+
alias = 'aliases %s and %s are missing' % (
1633+
', '.join(missing_names[:-1]), missing_names[-1]
1634+
)
1635+
if _is_single_bit(missing_value):
1636+
value = 'value 0x%x' % missing_value
1637+
else:
1638+
value = 'combined values of 0x%x' % missing_value
16261639
raise ValueError(
1627-
'invalid Flag %r: %s'
1628-
% (cls_name, '; '.join(missing))
1640+
'invalid Flag %r: %s %s [use `enum.show_flag_values(value)` for details]'
1641+
% (cls_name, alias, value)
16291642
)
16301643
return enumeration
16311644

Lib/test/test_enum.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,9 +2166,6 @@ def __init__(self, *args):
21662166
self._valid = True
21672167
@classmethod
21682168
def _missing_(cls, value):
2169-
# encountered an unknown value!
2170-
# Luckily I'm a LenientStrEnum, so I won't crash just yet.
2171-
# You might want to add a new case though.
21722169
unknown = cls._member_type_.__new__(cls, value)
21732170
unknown._valid = False
21742171
unknown._name_ = value.upper()
@@ -3594,14 +3591,15 @@ class Bizarre(Flag):
35943591
self.assertEqual(Bizarre.d.value, 6)
35953592
with self.assertRaisesRegex(
35963593
ValueError,
3597-
"invalid Flag 'Bizarre': 'b' is missing named flags for values 1, 2; 'd' is missing a named flag for value 2",
3594+
"invalid Flag 'Bizarre': aliases b and d are missing combined values of 0x3 .use `enum.show_flag_values.value.` for details.",
35983595
):
35993596
@verify(NAMED_FLAGS)
36003597
class Bizarre(Flag):
36013598
b = 3
36023599
c = 4
36033600
d = 6
36043601
#
3602+
self.assertEqual(enum.show_flag_values(3), [1, 2])
36053603
class Bizarre(IntFlag):
36063604
b = 3
36073605
c = 4
@@ -3612,13 +3610,13 @@ class Bizarre(IntFlag):
36123610
self.assertEqual(Bizarre.d.value, 6)
36133611
with self.assertRaisesRegex(
36143612
ValueError,
3615-
"invalid Flag 'Bizarre': 'b' is missing named flags for values 1, 2; 'd' is missing a named flag for value 2",
3613+
"invalid Flag 'Bizarre': alias d is missing value 0x2 .use `enum.show_flag_values.value.` for details.",
36163614
):
36173615
@verify(NAMED_FLAGS)
36183616
class Bizarre(IntFlag):
3619-
b = 3
36203617
c = 4
36213618
d = 6
3619+
self.assertEqual(enum.show_flag_values(2), [2])
36223620

36233621
def test_unique_clean(self):
36243622
@verify(UNIQUE)
@@ -3885,7 +3883,7 @@ class Missing:
38853883

38863884
class MiscTestCase(unittest.TestCase):
38873885
def test__all__(self):
3888-
support.check__all__(self, enum, not_exported={'bin'})
3886+
support.check__all__(self, enum, not_exported={'bin', 'show_flag_values'})
38893887

38903888

38913889
# These are unordered here on purpose to ensure that declaration order

0 commit comments

Comments
 (0)