Skip to content

Commit 8b6fbde

Browse files
committed
Fix pickling bug
1 parent 080e554 commit 8b6fbde

6 files changed

Lines changed: 16 additions & 27 deletions

File tree

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ def test_equality(self):
412412

413413
def test_pickle(self):
414414
interp = interpreters.create()
415-
for protocol in range(2, pickle.HIGHEST_PROTOCOL + 1):
415+
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
416416
with self.subTest(protocol=protocol):
417417
data = pickle.dumps(interp, protocol)
418418
unpickled = pickle.loads(data)

Lib/test/test_interpreters/test_channels.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,13 @@ def test_equality(self):
120120
self.assertNotEqual(ch1, ch2)
121121

122122
def test_pickle(self):
123-
ch, _ = channels.create()
124-
for protocol in range(2, 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)
123+
recv, send = channels.create()
124+
for ch in [recv, send]:
125+
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
126+
with self.subTest(ch=ch, protocol=protocol):
127+
data = pickle.dumps(ch, protocol)
128+
unpickled = pickle.loads(data)
129+
self.assertEqual(unpickled, ch)
129130

130131

131132
class TestSendChannelAttrs(TestBase):
@@ -154,7 +155,7 @@ def test_equality(self):
154155

155156
def test_pickle(self):
156157
_, ch = channels.create()
157-
for protocol in range(2, pickle.HIGHEST_PROTOCOL + 1):
158+
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
158159
with self.subTest(protocol=protocol):
159160
data = pickle.dumps(ch, protocol)
160161
unpickled = pickle.loads(data)

Lib/test/test_interpreters/test_queues.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def test_equality(self):
188188

189189
def test_pickle(self):
190190
queue = queues.create()
191-
for protocol in range(2, pickle.HIGHEST_PROTOCOL + 1):
191+
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
192192
with self.subTest(protocol=protocol):
193193
data = pickle.dumps(queue, protocol)
194194
unpickled = pickle.loads(data)

0 commit comments

Comments
 (0)