Skip to content

gh-76785: Minor Improvements to "interpreters" Module #116328

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

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Make interpreters.*Channel instances pickleable.
  • Loading branch information
ericsnowcurrently committed Mar 4, 2024
commit 98a03d2a9756872182a71bf94f24cea7266dd92a
12 changes: 11 additions & 1 deletion Lib/test/support/interpreters/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ class _ChannelEnd:

_end = None

def __init__(self, cid):
def __new__(cls, cid):
self = super().__new__(cls)
if self._end == 'send':
cid = _channels._channel_id(cid, send=True, force=True)
elif self._end == 'recv':
cid = _channels._channel_id(cid, recv=True, force=True)
else:
raise NotImplementedError(self._end)
self._id = cid
return self

def __repr__(self):
return f'{type(self).__name__}(id={int(self._id)})'
Expand All @@ -61,6 +63,14 @@ def __eq__(self, other):
return NotImplemented
return other._id == self._id

# for pickling:
def __getnewargs__(self):
return (int(self._id),)

# for pickling:
def __getstate__(self):
return None

@property
def id(self):
return self._id
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_interpreters/test_channels.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import importlib
import pickle
import threading
from textwrap import dedent
import unittest
Expand Down Expand Up @@ -100,6 +101,12 @@ def test_equality(self):
self.assertEqual(ch1, ch1)
self.assertNotEqual(ch1, ch2)

def test_pickle(self):
ch, _ = channels.create()
data = pickle.dumps(ch)
unpickled = pickle.loads(data)
self.assertEqual(unpickled, ch)


class TestSendChannelAttrs(TestBase):

Expand All @@ -125,6 +132,12 @@ def test_equality(self):
self.assertEqual(ch1, ch1)
self.assertNotEqual(ch1, ch2)

def test_pickle(self):
_, ch = channels.create()
data = pickle.dumps(ch)
unpickled = pickle.loads(data)
self.assertEqual(unpickled, ch)


class TestSendRecv(TestBase):

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy