mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
gh-99248: [Enum] fix negative number infinite loop (GH-99256)
[Enum] fix negative number infinite loop
- _iter_bits_lsb() now raises a ValueError if a negative number
is passed in
- verify() now skips checking negative numbers for named flags
(cherry picked from commit 0b4ffb08cc
)
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
This commit is contained in:
parent
ca944628ac
commit
4f31171e3f
3 changed files with 23 additions and 2 deletions
|
@ -114,9 +114,12 @@ def _make_class_unpicklable(obj):
|
|||
setattr(obj, '__module__', '<unknown>')
|
||||
|
||||
def _iter_bits_lsb(num):
|
||||
# num must be an integer
|
||||
# num must be a positive integer
|
||||
original = num
|
||||
if isinstance(num, Enum):
|
||||
num = num.value
|
||||
if num < 0:
|
||||
raise ValueError('%r is not a positive integer' % original)
|
||||
while num:
|
||||
b = num & (~num + 1)
|
||||
yield b
|
||||
|
@ -1856,6 +1859,9 @@ class verify:
|
|||
if name in member_names:
|
||||
# not an alias
|
||||
continue
|
||||
if alias.value < 0:
|
||||
# negative numbers are not checked
|
||||
continue
|
||||
values = list(_iter_bits_lsb(alias.value))
|
||||
missed = [v for v in values if v not in member_values]
|
||||
if missed:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue