[ovs-dev] [PATCH 11/11] python: Deal with str and byte differences.
Ben Pfaff
blp at ovn.org
Tue Feb 2 21:24:36 UTC 2016
On Fri, Jan 22, 2016 at 09:32:33PM -0500, Russell Bryant wrote:
> 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 at ovn.org>
> ---
> python/ovs/jsonrpc.py | 10 ++++++++++
> python/ovs/socket_util.py | 3 +++
> python/ovs/stream.py | 6 ++++++
> 3 files changed, 19 insertions(+)
>
> diff --git a/python/ovs/jsonrpc.py b/python/ovs/jsonrpc.py
> index efe0f6c..5177980 100644
> --- a/python/ovs/jsonrpc.py
> +++ b/python/ovs/jsonrpc.py
> @@ -264,6 +264,16 @@ 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:
> + # XXX Illegal byte sequence. Is there a better errno?
> + error = errno.EILSEQ
Isn't EILSEQ perfect for this?
Acked-by: Ben Pfaff <blp at ovn.org>
More information about the dev
mailing list