[PATCH] python/osmo-python-tests[master]: Fix python3 compatibility

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Max gerrit-no-reply at lists.osmocom.org
Fri Dec 15 16:07:00 UTC 2017


Hello Pau Espin Pedrol, Neels Hofmeyr,

I'd like you to reexamine a change.  Please visit

    https://gerrit.osmocom.org/5280

to look at the new patch set (#5).

Fix python3 compatibility

Use print() function with proper import.

Change-Id: Ib10dfbec18202245031a634fc3f19677fc952e60
---
M osmopy/__init__.py
M osmopy/obscvty.py
M osmopy/osmoutil.py
M scripts/osmo_ctrl.py
4 files changed, 24 insertions(+), 22 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/python/osmo-python-tests refs/changes/80/5280/5

diff --git a/osmopy/__init__.py b/osmopy/__init__.py
index e3bf016..6150ea4 100644
--- a/osmopy/__init__.py
+++ b/osmopy/__init__.py
@@ -1,4 +1,4 @@
 #!/usr/bin/env python
-__version__ = '0.0.4'
+__version__ = '0.0.5'
 
 __all__ = ['obscvty', 'osmoutil', 'osmo_ipa']
diff --git a/osmopy/obscvty.py b/osmopy/obscvty.py
index 1e05d77..d39d3da 100755
--- a/osmopy/obscvty.py
+++ b/osmopy/obscvty.py
@@ -16,6 +16,7 @@
 #
 # VTY helper code for OpenBSC
 #
+from __future__ import print_function
 import re
 import socket
 import sys, subprocess
@@ -31,12 +32,12 @@
 debug_tcp_sockets = (os.getenv('OSMOPY_DEBUG_TCP_SOCKETS', '0') != '0')
 
 def cmd(what):
-    print '\n> %s' % what
+    print('\n> %s' % what)
     sys.stdout.flush()
     subprocess.call(what, shell=True)
     sys.stdout.flush()
     sys.stderr.flush()
-    print ''
+    print('')
     sys.stdout.flush()
 
 def print_used_tcp_sockets():
@@ -86,16 +87,16 @@
                     raise
                 # possibly the binary hasn't launched yet
                 if debug_tcp_sockets:
-                    print "Connecting socket failed, retrying..."
+                    print("Connecting socket failed, retrying...")
                 time.sleep(.1)
                 continue
             break
 
         if debug_tcp_sockets:
             VTYInteract.all_sockets.append(self.socket)
-            print "Socket: in %d tries, connected to %s:%d %r (%d sockets open)" % (
+            print("Socket: in %d tries, connected to %s:%d %r (%d sockets open)" % (
                     took, self.host, self.port, self.socket,
-                    len(VTYInteract.all_sockets))
+                    len(VTYInteract.all_sockets)))
         self.socket.recv(4096)
 
     def _close_socket(self):
@@ -108,9 +109,9 @@
                 VTYInteract.all_sockets.remove(self.socket)
             except ValueError:
                 pass
-            print "Socket: closing %s:%d %r (%d sockets open)" % (
+            print("Socket: closing %s:%d %r (%d sockets open)" % (
                     self.host, self.port, self.socket,
-                    len(VTYInteract.all_sockets))
+                    len(VTYInteract.all_sockets)))
         self.socket.close()
         self.socket = None
 
@@ -248,7 +249,7 @@
 
         if loud:
             if res != results:
-                print "Rec: %s\nExp: %s" % (res, results)
+                print("Rec: %s\nExp: %s" % (res, results))
 
         return res == results
 
diff --git a/osmopy/osmoutil.py b/osmopy/osmoutil.py
index 8f0369b..54a3456 100755
--- a/osmopy/osmoutil.py
+++ b/osmopy/osmoutil.py
@@ -14,6 +14,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+from __future__ import print_function
 import subprocess
 import os
 import sys
@@ -30,10 +31,10 @@
     global devnull
     if devnull is None:
         if verbose:
-            print "Opening /dev/null"
+            print("Opening /dev/null")
         devnull = open(os.devnull, 'w')
     if verbose:
-        print "Launching: PWD=%s %s" % (os.getcwd(), ' '.join([repr(c) for c in cmd]))
+        print("Launching: PWD=%s %s" % (os.getcwd(), ' '.join([repr(c) for c in cmd])))
     return subprocess.Popen(cmd, stdout=devnull, stderr=devnull)
 
 
@@ -65,9 +66,9 @@
     if proc.poll() is None:
         # termination seems to be slower than that, let's just kill
         proc.kill()
-        print "Killed child process"
+        print("Killed child process")
     elif waited_time > .002:
-        print "Terminating took %.3fs" % waited_time
+        print("Terminating took %.3fs" % waited_time)
     proc.wait()
 
 
@@ -80,9 +81,9 @@
         return importlib.import_module(confname)
     except ImportError as e:
         if p_set:
-            print >> sys.stderr, "osmoappdesc not found in %s" % dirname
+            print("osmoappdesc not found in %s" % dirname, file=sys.stderr)
         else:
-            print >> sys.stderr, "set osmoappdesc location with -p <dir>"
+            print("set osmoappdesc location with -p <dir>", file=sys.stderr)
         sys.exit(1)
 
 
diff --git a/scripts/osmo_ctrl.py b/scripts/osmo_ctrl.py
index 2b8c4be..bec6d1d 100755
--- a/scripts/osmo_ctrl.py
+++ b/scripts/osmo_ctrl.py
@@ -21,7 +21,7 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 """
-
+from __future__ import print_function
 from optparse import OptionParser
 from osmopy.osmo_ipa import Ctrl
 import socket
@@ -30,7 +30,7 @@
 
 def connect(host, port):
         if verbose:
-                print "Connecting to host %s:%i" % (host, port)
+                print("Connecting to host %s:%i" % (host, port))
 
         sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         sck.setblocking(1)
@@ -57,13 +57,13 @@
         """
         try:
                 data = sck.recv(1024, fl)
-        except socket.error as (s_errno, strerror):
+        except socket.error as _:
                 return False
         if len(data) != 0:
                 tail = data
                 while True:
                         (head, tail) = Ctrl().split_combined(tail)
-                        print "Got message:", Ctrl().rem_header(head)
+                        print("Got message:", Ctrl().rem_header(head))
                         if len(tail) == 0:
                                 break
                 return True
@@ -103,18 +103,18 @@
                 if len(args) < 2:
                         parser.error("Set requires var and value arguments")
                 _leftovers(sock, socket.MSG_DONTWAIT)
-                print "Got message:", set_var(sock, args[0], ' '.join(args[1:]))
+                print("Got message:", set_var(sock, args[0], ' '.join(args[1:])))
 
         if options.cmd_get:
                 if len(args) != 1:
                         parser.error("Get requires the var argument")
                 _leftovers(sock, socket.MSG_DONTWAIT)
                 (a, _, _) = do_set_get(sock, args[0])
-                print "Got message:", a
+                print("Got message:", a)
 
         if options.monitor:
                 while True:
                         if not _leftovers(sock, 0):
-                                print "Connection is gone."
+                                print("Connection is gone.")
                                 break
         sock.close()

-- 
To view, visit https://gerrit.osmocom.org/5280
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: Ib10dfbec18202245031a634fc3f19677fc952e60
Gerrit-PatchSet: 5
Gerrit-Project: python/osmo-python-tests
Gerrit-Branch: master
Gerrit-Owner: Max <msuraev at sysmocom.de>
Gerrit-Reviewer: Max <msuraev at sysmocom.de>
Gerrit-Reviewer: Neels Hofmeyr <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: Pau Espin Pedrol <pespin at sysmocom.de>



More information about the gerrit-log mailing list