2
0
mirror of https://github.com/openvswitch/ovs synced 2025-10-19 14:37:21 +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

@@ -15,6 +15,7 @@
import errno
import os
import socket
import sys
import six
@@ -223,6 +224,11 @@ class Stream(object):
return 0
try:
# Python 3 has separate types for strings and bytes. We must have
# bytes here.
if (sys.version_info[0] >= 3
and not isinstance(buf, six.binary_type)):
buf = six.binary_type(buf, 'utf-8')
return self.socket.send(buf)
except socket.error as e:
return -ovs.socket_util.get_exception_errno(e)