8000 [3.11] gh-99248: [Enum] fix negative number infinite loop (GH-99256) by miss-islington · Pull Request #99259 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.11] gh-99248: [Enum] fix negative number infinite loop (GH-99256) #99259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ def _break_on_call_reduce(self, proto):
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
Expand Down Expand Up @@ -1856,6 +1859,9 @@ def __call__(self, enumeration):
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:
Expand Down
16 changes: 15 additions & 1 deletion Lib/test/test_enum.py
< 8000 td id="diff-8467f9fbbff81abf26d87a8dbbf0e0c866157971948010e48cc73539251a9e4cL178" data-line-number="178" class="blob-num blob-num-context js-linkable-line-number">
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from enum import Enum, IntEnum, StrEnum, EnumType, Flag, IntFlag, unique, auto
from enum import STRICT, CONFORM, EJECT, KEEP, _simple_enum, _test_simple_enum
from enum import verify, UNIQUE, CONTINUOUS, NAMED_FLAGS, ReprEnum
from enum import member, nonmember
from enum import member, nonmember, _iter_bits_lsb
from io import StringIO
from pickle import dumps, loads, PicklingError, HIGHEST_PROTOCOL
from test import support
Expand Down Expand Up @@ -174,6 +174,10 @@ def test_is_private(self):
for name in self.sunder_names + self.dunder_names + self.random_names:
self.assertFalse(enum._is_private('MyEnum', name), '%r is a private name?')

def test_iter_bits_lsb(self):
self.assertEqual(list(_iter_bits_lsb(7)), [1, 2, 4])
self.assertRaisesRegex(ValueError, '-8 is not a positive integer', list, _iter_bits_lsb(-8))


# for subclassing tests

Expand Down Expand Up @@ -3965,6 +3969,16 @@ class Sillier(IntEnum):
triple = 3
value = 4

def test_negative_alias(self):
@verify(NAMED_FLAGS)
class Color(Flag):
RED = 1
GREEN = 2
BLUE = 4
WHITE = -1
# no error means success


class TestInternals(unittest.TestCase):

sunder_names = '_bad_', '_good_', '_what_ho_'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix negative numbers failing in verify()
0