Skip to content

Commit 41eca1c

Browse files
authored
Merge pull request #234 from jasongrout/buffers
Check that buffers in a session send support the buffer protocol.
2 parents 9191c78 + 98aaa2e commit 41eca1c

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

jupyter_client/session.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,14 @@ def send(self, stream, msg_or_type, content=None, parent=None, ident=None,
699699
)
700700
return
701701
buffers = [] if buffers is None else buffers
702+
for buf in buffers:
703+
if not isinstance(buf, memoryview):
704+
try:
705+
# check to see if buf supports the buffer protocol.
706+
memoryview(buf)
707+
except TypeError:
708+
raise TypeError("Buffer objects must support the buffer protocol.")
709+
702710
if self.adapt_version:
703711
msg = adapt(msg, self.adapt_version)
704712
to_send = self.serialize(msg, ident)

jupyter_client/tests/test_session.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ def test_send(self):
121121
self.assertEqual(new_msg['parent_header'],msg['parent_header'])
122122
self.assertEqual(new_msg['buffers'],[b'bar'])
123123

124+
# buffers must support the buffer protocol
125+
with self.assertRaises(TypeError) as cm:
126+
self.session.send(A, msg, ident=b'foo', buffers=[1])
127+
124128
A.close()
125129
B.close()
126130
ctx.term()

0 commit comments

Comments
 (0)