Skip to content

gh-136285: Improve pickle protocol testing in test_interpreters #136286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions Lib/concurrent/interpreters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,8 @@ def __del__(self):
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:
Expand Down
8 changes: 2 additions & 6 deletions Lib/concurrent/interpreters/_queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,8 @@ def __hash__(self):
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')
Expand Down
8 changes: 2 additions & 6 deletions Lib/test/support/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,8 @@ def __eq__(self, other):
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):
Expand Down
8 changes: 5 additions & 3 deletions Lib/test/test_interpreters/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,11 @@ def test_equality(self):

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):
Expand Down
19 changes: 12 additions & 7 deletions Lib/test/test_interpreters/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,13 @@ def test_equality(self):
self.assertNotEqual(ch1, ch2)

def test_pickle(self):
ch, _ = channels.create()
data = pickle.dumps(ch)
unpickled = pickle.loads(data)
self.assertEqual(unpickled, ch)
recv, send = channels.create()
for ch in [recv, send]:
Comment on lines +123 to +124
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test repeats also the test in TestSendChannelAttrs. I think that only needs to test the receiver channel here.

Suggested change
recv, send = channels.create()
for ch in [recv, send]:
ch, _ = channels.create()

(and de-indent the following code)

for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
with self.subTest(ch=ch, protocol=protocol):
data = pickle.dumps(ch, protocol)
unpickled = pickle.loads(data)
self.assertEqual(unpickled, ch)


class TestSendChannelAttrs(TestBase):
Expand Down Expand Up @@ -152,9 +155,11 @@ def test_equality(self):

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):
Expand Down
8 changes: 5 additions & 3 deletions Lib/test/test_interpreters/test_queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,11 @@ def test_equality(self):

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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix pickling failures for protocols 0 and 1 for many objects realted to
subinterpreters.
Loading