]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-136285: Improve `pickle` protocol testing in `test_interpreters` (#136286)
authorsobolevn <mail@sobolevn.me>
Sun, 6 Jul 2025 07:35:30 +0000 (10:35 +0300)
committerGitHub <noreply@github.com>
Sun, 6 Jul 2025 07:35:30 +0000 (07:35 +0000)
Lib/concurrent/interpreters/__init__.py
Lib/concurrent/interpreters/_queues.py
Lib/test/support/channels.py
Lib/test/test_interpreters/test_api.py
Lib/test/test_interpreters/test_channels.py
Lib/test/test_interpreters/test_queues.py
Misc/NEWS.d/next/Library/2025-07-05-09-45-04.gh-issue-136286.N67Amr.rst [new file with mode: 0644]

index 0fd661249a276cb435624ecc86a37496f55534d3..aa46a2b37a48d51112a05a0dc69c80530117286d 100644 (file)
@@ -146,12 +146,8 @@ class Interpreter:
         self._decref()
 
     # for pickling:
-    def __getnewargs__(self):
-        return (self._id,)
-
-    # for pickling:
-    def __getstate__(self):
-        return None
+    def __reduce__(self):
+        return (type(self), (self._id,))
 
     def _decref(self):
         if not self._ownsref:
index 99987f2f6926b0722be2583a79366908ebdb54bf..9c12b2c8c2466420bf505f951321af27943cf3f8 100644 (file)
@@ -129,12 +129,8 @@ class Queue:
         return hash(self._id)
 
     # for pickling:
-    def __getnewargs__(self):
-        return (self._id,)
-
-    # for pickling:
-    def __getstate__(self):
-        return None
+    def __reduce__(self):
+        return (type(self), (self._id,))
 
     def _set_unbound(self, op, items=None):
         assert not hasattr(self, '_unbound')
index b2de24d9d3e53467c5e97c0334a788942320c068..fab1797659b3124162a5023133efbcd791516282 100644 (file)
@@ -105,12 +105,8 @@ class _ChannelEnd:
         return other._id == self._id
 
     # for pickling:
-    def __getnewargs__(self):
-        return (int(self._id),)
-
-    # for pickling:
-    def __getstate__(self):
-        return None
+    def __reduce__(self):
+        return (type(self), (int(self._id),))
 
     @property
     def id(self):
index 0ee4582b5d1568f553ca2101156a21d87efa4239..a34b20beaca7a3f44d4bac3a4d5522c05db289c6 100644 (file)
@@ -412,9 +412,11 @@ class InterpreterObjectTests(TestBase):
 
     def test_pickle(self):
         interp = interpreters.create()
-        data = pickle.dumps(interp)
-        unpickled = pickle.loads(data)
-        self.assertEqual(unpickled, interp)
+        for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
+            with self.subTest(protocol=protocol):
+                data = pickle.dumps(interp, protocol)
+                unpickled = pickle.loads(data)
+                self.assertEqual(unpickled, interp)
 
 
 class TestInterpreterIsRunning(TestBase):
index 109ddf344539adaa6f09ac99cce05ce1366837c7..52827357078b85760438febd2be497e26eb26173 100644 (file)
@@ -121,9 +121,11 @@ class TestRecvChannelAttrs(TestBase):
 
     def test_pickle(self):
         ch, _ = channels.create()
-        data = pickle.dumps(ch)
-        unpickled = pickle.loads(data)
-        self.assertEqual(unpickled, ch)
+        for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
+            with self.subTest(protocol=protocol):
+                data = pickle.dumps(ch, protocol)
+                unpickled = pickle.loads(data)
+                self.assertEqual(unpickled, ch)
 
 
 class TestSendChannelAttrs(TestBase):
@@ -152,9 +154,11 @@ class TestSendChannelAttrs(TestBase):
 
     def test_pickle(self):
         _, ch = channels.create()
-        data = pickle.dumps(ch)
-        unpickled = pickle.loads(data)
-        self.assertEqual(unpickled, ch)
+        for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
+            with self.subTest(protocol=protocol):
+                data = pickle.dumps(ch, protocol)
+                unpickled = pickle.loads(data)
+                self.assertEqual(unpickled, ch)
 
 
 class TestSendRecv(TestBase):
index cb17340f581b0ac02227f37ca7a5493a97b4f26f..5451c6654acb4787db8c9b427e2ef87760164f3d 100644 (file)
@@ -188,9 +188,11 @@ class QueueTests(TestBase):
 
     def test_pickle(self):
         queue = queues.create()
-        data = pickle.dumps(queue)
-        unpickled = pickle.loads(data)
-        self.assertEqual(unpickled, queue)
+        for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
+            with self.subTest(protocol=protocol):
+                data = pickle.dumps(queue, protocol)
+                unpickled = pickle.loads(data)
+                self.assertEqual(unpickled, queue)
 
 
 class TestQueueOps(TestBase):
diff --git a/Misc/NEWS.d/next/Library/2025-07-05-09-45-04.gh-issue-136286.N67Amr.rst b/Misc/NEWS.d/next/Library/2025-07-05-09-45-04.gh-issue-136286.N67Amr.rst
new file mode 100644 (file)
index 0000000..0a0d66a
--- /dev/null
@@ -0,0 +1,2 @@
+Fix pickling failures for protocols 0 and 1 for many objects realted to
+subinterpreters.