summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/support/interpreters.py15
-rw-r--r--Lib/test/test__xxinterpchannels.py15
-rw-r--r--Lib/test/test_interpreters.py43
3 files changed, 73 insertions, 0 deletions
diff --git a/Lib/test/support/interpreters.py b/Lib/test/support/interpreters.py
index 5aba369..d61724c 100644
--- a/Lib/test/support/interpreters.py
+++ b/Lib/test/support/interpreters.py
@@ -225,6 +225,21 @@ class SendChannel(_ChannelEnd):
# See bpo-32604 and gh-19829.
return _channels.send(self._id, obj)
+ def send_buffer(self, obj):
+ """Send the object's buffer to the channel's receiving end.
+
+ This blocks until the object is received.
+ """
+ _channels.send_buffer(self._id, obj)
+
+ def send_buffer_nowait(self, obj):
+ """Send the object's buffer to the channel's receiving end.
+
+ If the object is immediately received then return True
+ (else False). Otherwise this is the same as send().
+ """
+ return _channels.send_buffer(self._id, obj)
+
def close(self):
_channels.close(self._id, send=True)
diff --git a/Lib/test/test__xxinterpchannels.py b/Lib/test/test__xxinterpchannels.py
index 750cd99..cb69f73 100644
--- a/Lib/test/test__xxinterpchannels.py
+++ b/Lib/test/test__xxinterpchannels.py
@@ -703,6 +703,21 @@ class ChannelTests(TestBase):
channels.recv(cid2)
del cid2
+ def test_send_buffer(self):
+ buf = bytearray(b'spamspamspam')
+ cid = channels.create()
+ channels.send_buffer(cid, buf)
+ obj = channels.recv(cid)
+
+ self.assertIsNot(obj, buf)
+ self.assertIsInstance(obj, memoryview)
+ self.assertEqual(obj, buf)
+
+ buf[4:8] = b'eggs'
+ self.assertEqual(obj, buf)
+ obj[4:8] = b'ham.'
+ self.assertEqual(obj, buf)
+
def test_allowed_types(self):
cid = channels.create()
objects = [
diff --git a/Lib/test/test_interpreters.py b/Lib/test/test_interpreters.py
index f2ef172..fe7b14d 100644
--- a/Lib/test/test_interpreters.py
+++ b/Lib/test/test_interpreters.py
@@ -1067,3 +1067,46 @@ class TestSendRecv(TestBase):
self.assertEqual(obj4, b'spam')
self.assertEqual(obj5, b'eggs')
self.assertIs(obj6, default)
+
+ def test_send_buffer(self):
+ buf = bytearray(b'spamspamspam')
+ obj = None
+ rch, sch = interpreters.create_channel()
+
+ def f():
+ nonlocal obj
+ while True:
+ try:
+ obj = rch.recv()
+ break
+ except interpreters.ChannelEmptyError:
+ time.sleep(0.1)
+ t = threading.Thread(target=f)
+ t.start()
+
+ sch.send_buffer(buf)
+ t.join()
+
+ self.assertIsNot(obj, buf)
+ self.assertIsInstance(obj, memoryview)
+ self.assertEqual(obj, buf)
+
+ buf[4:8] = b'eggs'
+ self.assertEqual(obj, buf)
+ obj[4:8] = b'ham.'
+ self.assertEqual(obj, buf)
+
+ def test_send_buffer_nowait(self):
+ buf = bytearray(b'spamspamspam')
+ rch, sch = interpreters.create_channel()
+ sch.send_buffer_nowait(buf)
+ obj = rch.recv()
+
+ self.assertIsNot(obj, buf)
+ self.assertIsInstance(obj, memoryview)
+ self.assertEqual(obj, buf)
+
+ buf[4:8] = b'eggs'
+ self.assertEqual(obj, buf)
+ obj[4:8] = b'ham.'
+ self.assertEqual(obj, buf)