[ovs-dev] [PATCH] python: Update Python version checks.

Russell Bryant russell at ovn.org
Thu Apr 14 21:00:48 UTC 2016


Instead of checking the raw version, use the six.PY2 and six.PY3 helpers
to determine if Python 2 or Python 3 are in use.

In one case, the check was to determine if the Python version was >=
2.6.  We now only support >= 2.7, so this check would always be true.

Signed-off-by: Russell Bryant <russell at ovn.org>
---
 python/ovs/socket_util.py | 8 ++------
 python/ovs/stream.py      | 4 +---
 tests/test-json.py        | 2 +-
 3 files changed, 4 insertions(+), 10 deletions(-)

diff --git a/python/ovs/socket_util.py b/python/ovs/socket_util.py
index 9f46a55..b358b05 100644
--- a/python/ovs/socket_util.py
+++ b/python/ovs/socket_util.py
@@ -17,7 +17,6 @@ import os
 import os.path
 import random
 import socket
-import sys
 
 import six
 from six.moves import range
@@ -85,10 +84,7 @@ def make_unix_socket(style, nonblock, bind_path, connect_path, short=False):
             sock.bind(bind_path)
 
             try:
-                if sys.hexversion >= 0x02060000:
-                    os.fchmod(sock.fileno(), 0o700)
-                else:
-                    os.chmod("/dev/fd/%d" % sock.fileno(), 0o700)
+                os.fchmod(sock.fileno(), 0o700)
             except OSError as e:
                 pass
         if connect_path is not None:
@@ -276,7 +272,7 @@ def write_fully(fd, buf):
     bytes_written = 0
     if len(buf) == 0:
         return 0, 0
-    if sys.version_info[0] >= 3 and not isinstance(buf, six.binary_type):
+    if six.PY3 and not isinstance(buf, six.binary_type):
         buf = six.binary_type(buf, 'utf-8')
     while True:
         try:
diff --git a/python/ovs/stream.py b/python/ovs/stream.py
index bc14836..64d9544 100644
--- a/python/ovs/stream.py
+++ b/python/ovs/stream.py
@@ -15,7 +15,6 @@
 import errno
 import os
 import socket
-import sys
 
 import six
 
@@ -226,8 +225,7 @@ class Stream(object):
         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)):
+            if six.PY3 and not isinstance(buf, six.binary_type):
                 buf = six.binary_type(buf, 'utf-8')
             return self.socket.send(buf)
         except socket.error as e:
diff --git a/tests/test-json.py b/tests/test-json.py
index 6750481..3f0b966 100644
--- a/tests/test-json.py
+++ b/tests/test-json.py
@@ -59,7 +59,7 @@ def main(argv):
     argv0 = argv[0]
 
     # When this is used with Python 3, the program produces no output.
-    if sys.version_info[0] == 2:
+    if six.PY2:
         # Make stdout and stderr UTF-8, even if they are redirected to a file.
         sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
         sys.stderr = codecs.getwriter("utf-8")(sys.stderr)
-- 
2.5.5




More information about the dev mailing list