Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Only keep the one-bit flag in Flag.__iter__
  • Loading branch information
hongweipeng committed Nov 18, 2020
commit c640f40a14b7fa243aca03c5b8216a3f20faf41c
7 changes: 6 additions & 1 deletion Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ def __contains__(self, other):

def __iter__(self):
members, extra_flags = _decompose(self.__class__, self.value)
return (m for m in members if m._value_ != 0)
return (m for m in members if m._value_ != 0 and _power_of_two(m._value_))
Comment thread
hongweipeng marked this conversation as resolved.
Outdated

def __repr__(self):
cls = self.__class__
Expand Down Expand Up @@ -946,3 +946,8 @@ def _decompose(flag, value):
# we have the breakdown, don't need the value member itself
members.pop(0)
return members, not_covered

Comment thread
hongweipeng marked this conversation as resolved.
Outdated
def _power_of_two(value):
if value < 1:
return False
return not value & (value - 1)
11 changes: 11 additions & 0 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2413,6 +2413,17 @@ def test_member_iter(self):
self.assertEqual(list(Color.BLUE), [Color.BLUE])
self.assertEqual(list(Color.GREEN), [Color.GREEN])

def test_member_iter_one_bit(self):
class Foo(Flag):
NONE = 0
A = 1
B = 2
C = 4
AB = A | B
ABC = A | B | C

self.assertEqual(list(Foo.ABC), [Foo.C, Foo.B, Foo.A])

def test_auto_number(self):
class Color(Flag):
red = auto()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Only keep the one-bit flag for :method:`Flag.__iter__` in :mod:`enum`. Patch by
Weipeng Hong.