Skip to content

Commit 06e347b

Browse files
authored
pythongh-136285: Improve pickle protocol testing in test_interpreters (python#136286)
1 parent 1953713 commit 06e347b

File tree

7 files changed

+28
-30
lines changed

7 files changed

+28
-30
lines changed

Lib/concurrent/interpreters/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,8 @@ def __del__(self):
146146
self._decref()
147147

148148
# for pickling:
149-
def __getnewargs__(self):
150-
return (self._id,)
151-
152-
# for pickling:
153-
def __getstate__(self):
154-
return None
149+
def __reduce__(self):
150+
return (type(self), (self._id,))
155151

156152
def _decref(self):
157153
if not self._ownsref:

Lib/concurrent/interpreters/_queues.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,8 @@ def __hash__(self):
129129
return hash(self._id)
130130

131131
# for pickling:
132-
def __getnewargs__(self):
133-
return (self._id,)
134-
135-
# for pickling:
136-
def __getstate__(self):
137-
return None
132+
def __reduce__(self):
133+
return (type(self), (self._id,))
138134

139135
def _set_unbound(self, op, items=None):
140136
assert not hasattr(self, '_unbound')

Lib/test/support/channels.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,8 @@ def __eq__(self, other):
105105
return other._id == self._id
106106

107107
# for pickling:
108-
def __getnewargs__(self):
109-
return (int(self._id),)
110-
111-
# for pickling:
112-
def __getstate__(self):
113-
return None
108+
def __reduce__(self):
109+
return (type(self), (int(self._id),))
114110

115111
@property
116112
def id(self):

Lib/test/test_interpreters/test_api.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,11 @@ def test_equality(self):
412412

413413
def test_pickle(self):
414414
interp = interpreters.create()
415-
data = pickle.dumps(interp)
416-
unpickled = pickle.loads(data)
417-
self.assertEqual(unpickled, interp)
415+
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
416+
with self.subTest(protocol=protocol):
417+
data = pickle.dumps(interp, protocol)
418+
unpickled = pickle.loads(data)
419+
self.assertEqual(unpickled, interp)
418420

419421

420422
class TestInterpreterIsRunning(TestBase):

Lib/test/test_interpreters/test_channels.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,11 @@ def test_equality(self):
121121

122122
def test_pickle(self):
123123
ch, _ = channels.create()
124-
data = pickle.dumps(ch)
125-
unpickled = pickle.loads(data)
126-
self.assertEqual(unpickled, ch)
124+
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
125+
with self.subTest(protocol=protocol):
126+
data = pickle.dumps(ch, protocol)
127+
unpickled = pickle.loads(data)
128+
self.assertEqual(unpickled, ch)
127129

128130

129131
class TestSendChannelAttrs(TestBase):
@@ -152,9 +154,11 @@ def test_equality(self):
152154

153155
def test_pickle(self):
154156
_, ch = channels.create()
155-
data = pickle.dumps(ch)
156-
unpickled = pickle.loads(data)
157-
self.assertEqual(unpickled, ch)
157+
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
158+
with self.subTest(protocol=protocol):
159+
data = pickle.dumps(ch, protocol)
160+
unpickled = pickle.loads(data)
161+
self.assertEqual(unpickled, ch)
158162

159163

160164
class TestSendRecv(TestBase):

Lib/test/test_interpreters/test_queues.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,11 @@ def test_equality(self):
188188

189189
def test_pickle(self):
190190
queue = queues.create()
191-
data = pickle.dumps(queue)
192-
unpickled = pickle.loads(data)
193-
self.assertEqual(unpickled, queue)
191+
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
192+
with self.subTest(protocol=protocol):
193+
data = pickle.dumps(queue, protocol)
194+
unpickled = pickle.loads(data)
195+
self.assertEqual(unpickled, queue)
194196

195197

196198
class TestQueueOps(TestBase):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix pickling failures for protocols 0 and 1 for many objects realted to
2+
subinterpreters.

0 commit comments

Comments
 (0)