Fix deprecation warning in array conversion

This commit is contained in:
Scott Maxwell 2013-12-04 00:43:03 -05:00
parent aa301506f4
commit a15d5ba25d
1 changed files with 18 additions and 4 deletions

View File

@ -49,6 +49,20 @@ class BufferedPipe (object):
self._buffer = array.array('B') self._buffer = array.array('B')
self._closed = False self._closed = False
if PY2:
def _buffer_frombytes(self, data):
self._buffer.fromstring(data)
def _buffer_tobytes(self, limit=None):
return self._buffer[:limit].tostring()
else:
def _buffer_frombytes(self, data):
self._buffer.frombytes(data)
def _buffer_tobytes(self, limit=None):
return self._buffer[:limit].tobytes()
def set_event(self, event): def set_event(self, event):
""" """
Set an event on this buffer. When data is ready to be read (or the Set an event on this buffer. When data is ready to be read (or the
@ -76,7 +90,7 @@ class BufferedPipe (object):
try: try:
if self._event is not None: if self._event is not None:
self._event.set() self._event.set()
self._buffer.fromstring(data) self._buffer_frombytes(b(data))
self._cv.notifyAll() self._cv.notifyAll()
finally: finally:
self._lock.release() self._lock.release()
@ -143,12 +157,12 @@ class BufferedPipe (object):
# something's in the buffer and we have the lock! # something's in the buffer and we have the lock!
if len(self._buffer) <= nbytes: if len(self._buffer) <= nbytes:
out = self._buffer.tostring() out = self._buffer_tobytes()
del self._buffer[:] del self._buffer[:]
if (self._event is not None) and not self._closed: if (self._event is not None) and not self._closed:
self._event.clear() self._event.clear()
else: else:
out = self._buffer[:nbytes].tostring() out = self._buffer_tobytes(nbytes)
del self._buffer[:nbytes] del self._buffer[:nbytes]
finally: finally:
self._lock.release() self._lock.release()
@ -164,7 +178,7 @@ class BufferedPipe (object):
""" """
self._lock.acquire() self._lock.acquire()
try: try:
out = self._buffer.tostring() out = self._buffer_tobytes()
del self._buffer[:] del self._buffer[:]
if (self._event is not None) and not self._closed: if (self._event is not None) and not self._closed:
self._event.clear() self._event.clear()