2
0
mirror of https://github.com/openvswitch/ovs synced 2025-10-29 15:28:56 +00:00

python: Deal with str and byte differences.

Python 3 has separate types for strings and bytes.  Python 2 used the
same type for both.  We need to convert strings to bytes before writing
them out to a socket.  We also need to convert data read from the socket
to a string.

Signed-off-by: Russell Bryant <russell@ovn.org>
Acked-by: Ben Pfaff <blp@ovn.org>
This commit is contained in:
Russell Bryant
2015-12-17 09:45:58 -05:00
parent fbafc3c263
commit 0e7c46c440
3 changed files with 18 additions and 0 deletions

View File

@@ -264,6 +264,15 @@ class Connection(object):
while True:
if not self.input:
error, data = self.stream.recv(4096)
# Python 3 has separate types for strings and bytes. We
# received bytes from a socket. We expect it to be string
# data, so we convert it here as soon as possible.
if (data and not error
and not isinstance(data, six.string_types)):
try:
data = data.decode('utf-8')
except UnicodeError:
error = errno.EILSEQ
if error:
if error == errno.EAGAIN:
return error, None