[ovs-dev] [PATCH 6/6] python: Tighten the check if we need encoding

Jakub Sitnicki jkbs at redhat.com
Wed Apr 18 16:01:15 UTC 2018


Check if we are dealing with a Unicode string for Python 2 and we need
to encode it before sending it, just as we do already for Python 3.

Also, do the encoding the same way for Python 2 and 3 and avoid using
negation to make the code simpler.

Signed-off-by: Jakub Sitnicki <jkbs at redhat.com>
---
 python/ovs/stream.py | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/python/ovs/stream.py b/python/ovs/stream.py
index 7b15bcd7d..0daaf199c 100644
--- a/python/ovs/stream.py
+++ b/python/ovs/stream.py
@@ -383,11 +383,9 @@ class Stream(object):
         elif len(buf) == 0:
             return 0
 
-        # Python 3 has separate types for strings and bytes.  We must have
-        # bytes here.
-        if six.PY3 and not isinstance(buf, bytes):
-            buf = bytes(buf, 'utf-8')
-        elif six.PY2:
+        # We must have bytes for sending.
+        if ((six.PY3 and isinstance(buf, str)) or
+            (six.PY2 and isinstance(buf, unicode))):
             buf = buf.encode('utf-8')
 
         if sys.platform == 'win32' and self.socket is None:
-- 
2.14.3



More information about the dev mailing list