Skip to content

Commit a5fb977

Browse files
committed
check that buffers are contiguous
zmq.send will fail on non-contiguous buffers ensures errors due to bad arguments are raise in `Session.send` instead of potentially delayed to the aysnc callback, which is not handled well.
1 parent 6567670 commit a5fb977

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

jupyter_client/session.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -699,13 +699,18 @@ 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):
702+
for idx, buf in enumerate(buffers):
703+
if isinstance(buf, memoryview):
704+
view = buf
705+
else:
704706
try:
705707
# check to see if buf supports the buffer protocol.
706-
memoryview(buf)
708+
view = memoryview(buf)
707709
except TypeError:
708710
raise TypeError("Buffer objects must support the buffer protocol.")
711+
if not view.contiguous:
712+
# zmq requires memoryviews to be contiguous
713+
raise ValueError("Buffer %i (%r) is not contiguous" % (idx, buf))
709714

710715
if self.adapt_version:
711716
msg = adapt(msg, self.adapt_version)

jupyter_client/tests/test_session.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,14 @@ def test_send(self):
122122
self.assertEqual(new_msg['buffers'],[b'bar'])
123123

124124
# buffers must support the buffer protocol
125-
with self.assertRaises(TypeError) as cm:
125+
with self.assertRaises(TypeError):
126126
self.session.send(A, msg, ident=b'foo', buffers=[1])
127127

128+
# buffers must be contiguous
129+
buf = memoryview(os.urandom(16))
130+
with self.assertRaises(ValueError):
131+
self.session.send(A, msg, ident=b'foo', buffers=[buf[::2]])
132+
128133
A.close()
129134
B.close()
130135
ctx.term()

0 commit comments

Comments
 (0)