[Python-checkins] gh-93820: Fix copy() regression in enum.Flag (GH-93876)

miss-islington webhook-mailer at python.org
Thu Jun 16 02:42:40 EDT 2022


https://github.com/python/cpython/commit/05b32c1c796d6c80479756ae898f488eac5f4f71
commit: 05b32c1c796d6c80479756ae898f488eac5f4f71
branch: main
author: Christian Heimes <christian at python.org>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-06-15T23:42:36-07:00
summary:

gh-93820: Fix copy() regression in enum.Flag (GH-93876)



GH-26658 introduced a regression in copy / pickle protocol for combined
`enum.Flag`s. `copy.copy(re.A | re.I)` would fail with
`AttributeError: ASCII|IGNORECASE`.

`enum.Flag` now has a `__reduce_ex__()` method that reduces flags by
combined value, not by combined name.

files:
A Misc/NEWS.d/next/Library/2022-06-15-21-20-02.gh-issue-93820.FAMLY8.rst
M Lib/enum.py
M Lib/test/test_enum.py

diff --git a/Lib/enum.py b/Lib/enum.py
index 4261b117d9db2..ee32d5d4e058b 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -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):
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index 25b3e2d818929..28594b010d80d 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -1,3 +1,4 @@
+import copy
 import enum
 import doctest
 import inspect
@@ -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:
 
@@ -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"
 
diff --git a/Misc/NEWS.d/next/Library/2022-06-15-21-20-02.gh-issue-93820.FAMLY8.rst b/Misc/NEWS.d/next/Library/2022-06-15-21-20-02.gh-issue-93820.FAMLY8.rst
new file mode 100644
index 0000000000000..e06d897e7d8e6
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-06-15-21-20-02.gh-issue-93820.FAMLY8.rst
@@ -0,0 +1,2 @@
+Fixed a regression when :func:`copy.copy`-ing :class:`enum.Flag` with
+multiple flag members.



More information about the Python-checkins mailing list