Skip to content
This repository has been archived by the owner on Sep 27, 2021. It is now read-only.

Commit

Permalink
Special-case handling of memoryviews when sending (fixes #14)
Browse files Browse the repository at this point in the history
Thanks to memeplex.
  • Loading branch information
djc committed Dec 15, 2015
1 parent e481f9e commit e0c4734
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions nnpy/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ def connect(self, addr):
assert rc > 0, rc

def send(self, data, flags=0):
data = data.encode() if isinstance(data, str) else data
l = len(data)
buf = ffi.new('char[%i]' % l, data)
rc = nanomsg.nn_send(self.sock, buf, l, flags)
if isinstance(data, memoryview):
buf = ffi.from_buffer(data)
else:
data = data.encode() if isinstance(data, str) else data
buf = ffi.new('char[%i]' % len(data), data)
rc = nanomsg.nn_send(self.sock, buf, len(data), flags)
assert rc > 0, rc

def recv(self, flags=0):
Expand Down

0 comments on commit e0c4734

Please sign in to comment.