Skip to content

Commit

Permalink
Merge pull request #258 from minrk/check-contiguous
Browse files Browse the repository at this point in the history
check that buffers are contiguous
  • Loading branch information
takluyver authored Jul 19, 2017
2 parents 5a8d7f8 + 8416398 commit 086832c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
13 changes: 10 additions & 3 deletions jupyter_client/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,13 +717,20 @@ def send(self, stream, msg_or_type, content=None, parent=None, ident=None,
)
return
buffers = [] if buffers is None else buffers
for buf in buffers:
if not isinstance(buf, memoryview):
for idx, buf in enumerate(buffers):
if isinstance(buf, memoryview):
view = buf
else:
try:
# check to see if buf supports the buffer protocol.
memoryview(buf)
view = memoryview(buf)
except TypeError:
raise TypeError("Buffer objects must support the buffer protocol.")
# memoryview.contiguous is new in 3.3,
# just skip the check on Python 2
if hasattr(view, 'contiguous') and not view.contiguous:
# zmq requires memoryviews to be contiguous
raise ValueError("Buffer %i (%r) is not contiguous" % (idx, buf))

if self.adapt_version:
msg = adapt(msg, self.adapt_version)
Expand Down
9 changes: 8 additions & 1 deletion jupyter_client/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import hmac
import os
import sys
import uuid
from datetime import datetime

Expand Down Expand Up @@ -123,9 +124,15 @@ def test_send(self):
self.assertEqual(new_msg['buffers'],[b'bar'])

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

# buffers must be contiguous
buf = memoryview(os.urandom(16))
if sys.version_info >= (3,3):
with self.assertRaises(ValueError):
self.session.send(A, msg, ident=b'foo', buffers=[buf[::2]])

A.close()
B.close()
ctx.term()
Expand Down

0 comments on commit 086832c

Please sign in to comment.