summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Liechti <cliechti@gmx.net>2015-08-13 22:54:16 +0200
committerChris Liechti <cliechti@gmx.net>2015-08-13 22:54:16 +0200
commitf99cd5c603d919b83bd1b013b1d1c7fa54fda531 (patch)
tree1324ec1295dafb27bc6b34cb14e2833e35f6817e
parent7af7c75527cfb862052b41f9f31e0fb9a8136005 (diff)
downloadpyserial-git-f99cd5c603d919b83bd1b013b1d1c7fa54fda531.tar.gz
add an iterbytes helper function
-rw-r--r--serial/rfc2217.py8
-rw-r--r--serial/serialutil.py10
-rw-r--r--serial/urlhandler/protocol_loop.py4
3 files changed, 13 insertions, 9 deletions
diff --git a/serial/rfc2217.py b/serial/rfc2217.py
index 8776f09..9c5d68b 100644
--- a/serial/rfc2217.py
+++ b/serial/rfc2217.py
@@ -706,9 +706,7 @@ class Serial(SerialBase):
self.logger.debug("socket error in reader thread: %s" % (e,))
break
if not data: break # lost connection
- #~ for byte in data: # fails for python3 as it returns ints instead of b''
- for x in range(len(data)):
- byte = data[x:x+1]
+ for byte in iterbytes(data):
if mode == M_NORMAL:
# interpret as command or as data
if byte == IAC:
@@ -1031,9 +1029,7 @@ class PortManager(object):
(socket error handling code left as exercise for the reader)
"""
- #~ for byte in data: # XXX fails for python3 as it returns ints instead of bytes
- for x in range(len(data)):
- byte = data[x:x+1]
+ for byte in iterbytes(data):
if self.mode == M_NORMAL:
# interpret as command or as data
if byte == IAC:
diff --git a/serial/serialutil.py b/serial/serialutil.py
index a71bd37..e20196d 100644
--- a/serial/serialutil.py
+++ b/serial/serialutil.py
@@ -22,6 +22,16 @@ except (NameError, AttributeError):
pass
+# "for byte in data" fails for python3 as it returns ints instead of bytes
+def iterbytes(b):
+ """Iterate over bytes, returning bytes instead of ints (python3)"""
+ x = 0
+ a = True
+ while a:
+ a = b[x:x+1]
+ x += 1
+ yield a
+
# all Python versions prior 3.x convert ``str([17])`` to '[17]' instead of '\x11'
# so a simple ``bytes(sequence)`` doesn't work for all versions
def to_bytes(seq):
diff --git a/serial/urlhandler/protocol_loop.py b/serial/urlhandler/protocol_loop.py
index 02f30df..02167e5 100644
--- a/serial/urlhandler/protocol_loop.py
+++ b/serial/urlhandler/protocol_loop.py
@@ -174,9 +174,7 @@ class Serial(SerialBase):
if self._writeTimeout is not None and time_used_to_send > self._writeTimeout:
time.sleep(self._writeTimeout) # must wait so that unit test succeeds
raise writeTimeoutError
- #~ for byte in data: # fails for python3 as it returns ints instead of b''
- for x in range(len(data)):
- byte = data[x:x+1]
+ for byte in iterbytes(data):
self.queue.put(byte, timeout=self._writeTimeout)
return len(data)