FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-93820: Fix copy() regression in enum.Flag by tiran · Pull Request #93876 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 3 additions & 0 deletions Lib/enum.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,9 @@ class Flag(Enum, boundary=STRICT):
Support for flags
"""

def __reduce_ex__(self, proto):
return self.__class__, (self._value_, )

_numeric_repr_ = repr

def _generate_next_value_(name, start, count, last_values):
Expand Down
28 changes: 28 additions & 0 deletions Lib/test/test_enum.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import enum
import doctest
import inspect
Expand Down Expand Up @@ -734,6 +735,13 @@ def test_format_specs(self):
self.assertFormatIsValue('{:5.2}', TE.third)
self.assertFormatIsValue('{:f}', TE.third)

def test_copy(self):
TE = self.MainEnum
copied = copy.copy(TE)
self.assertEqual(copied, TE)
deep = copy.deepcopy(TE)
self.assertEqual(deep, TE)


class _FlagTests:

Expand Down Expand Up @@ -2654,6 +2662,26 @@ class MyIntFlag(int, Flag):
self.assertTrue(isinstance(MyIntFlag.ONE | MyIntFlag.TWO, MyIntFlag), MyIntFlag.ONE | MyIntFlag.TWO)
self.assertTrue(isinstance(MyIntFlag.ONE | 2, MyIntFlag))

def test_int_flags_copy(self):
class MyIntFlag(IntFlag):
ONE = 1
TWO = 2
FOUR = 4

flags = MyIntFlag.ONE | MyIntFlag.TWO
copied = copy.copy(flags)
deep = copy.deepcopy(flags)
self.assertEqual(copied, flags)
self.assertEqual(deep, flags)

flags = MyIntFlag.ONE | MyIntFlag.TWO | 8
copied = copy.copy(flags)
deep = copy.deepcopy(flags)
self.assertEqual(copied, flags)
self.assertEqual(deep, flags)
self.assertEqual(copied.value, 1 | 2 | 8)


class TestOrder(unittest.TestCase):
"test usage of the `_order_` attribute"

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed a regression when :func:`copy.copy`-ing :class:`enum.Flag` with
multiple flag members.

Back | FazBrowse Home | New Git URL