]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-136285: Improve `pickle` protocol testing in `test_interpreters` (GH-136286...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 6 Jul 2025 08:13:13 +0000 (10:13 +0200)
committerGitHub <noreply@github.com>
Sun, 6 Jul 2025 08:13:13 +0000 (11:13 +0300)
gh-136285: Improve `pickle` protocol testing in `test_interpreters` (GH-136286)
(cherry picked from commit 06e347b84648f3f8e144e8f70671d610da082b77)

Co-authored-by: sobolevn <mail@sobolevn.me>
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 d6a3197d9e0e26d86315452f4dad82ca5c2f9bf3..ee830973f2a36e934688ef930bfaf4f513cec601 100644 (file)
@@ -130,12 +130,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 dfa86ba24dc13489e125afbb7dd672adbb7b384f..5352f7d4da3af0f505a718d95dc02753d37c3ea4 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 6b106fa21aab7df48be9d08e8eb277538fa462dd..815d38d8bd7a4a0f94346cac995b6a126ad7a84c 100644 (file)
@@ -189,9 +189,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.