mirror of
https://github.com/openvswitch/ovs
synced 2025-10-25 15:07:05 +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:
@@ -264,6 +264,15 @@ class Connection(object):
|
|||||||
while True:
|
while True:
|
||||||
if not self.input:
|
if not self.input:
|
||||||
error, data = self.stream.recv(4096)
|
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:
|
||||||
if error == errno.EAGAIN:
|
if error == errno.EAGAIN:
|
||||||
return error, None
|
return error, None
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import random
|
|||||||
import socket
|
import socket
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import six
|
||||||
from six.moves import range
|
from six.moves import range
|
||||||
|
|
||||||
import ovs.fatal_signal
|
import ovs.fatal_signal
|
||||||
@@ -275,6 +276,8 @@ def write_fully(fd, buf):
|
|||||||
bytes_written = 0
|
bytes_written = 0
|
||||||
if len(buf) == 0:
|
if len(buf) == 0:
|
||||||
return 0, 0
|
return 0, 0
|
||||||
|
if sys.version_info[0] >= 3 and not isinstance(buf, six.binary_type):
|
||||||
|
buf = six.binary_type(buf, 'utf-8')
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
retval = os.write(fd, buf)
|
retval = os.write(fd, buf)
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
import errno
|
import errno
|
||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
|
import sys
|
||||||
|
|
||||||
import six
|
import six
|
||||||
|
|
||||||
@@ -223,6 +224,11 @@ class Stream(object):
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
try:
|
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)
|
return self.socket.send(buf)
|
||||||
except socket.error as e:
|
except socket.error as e:
|
||||||
return -ovs.socket_util.get_exception_errno(e)
|
return -ovs.socket_util.get_exception_errno(e)
|
||||||
|
|||||||
Reference in New Issue
Block a user