gpsd-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gpsd-dev] [PATCH 09/12] Fixes gpscat and test_maidenhead.py for Python


From: Fred Wright
Subject: [gpsd-dev] [PATCH 09/12] Fixes gpscat and test_maidenhead.py for Python 3.
Date: Fri, 8 Apr 2016 10:07:50 -0700

This fixes two issues with gpscat in Python 3:

1) The data accumulation buffer needs to be 'bytes' rather than a
'str' in Python 3.  This is easily accomplished by using bytes() as
the initializer/reinitializer.

2) The hexdump routine (which was also iterating in a rather strange
way) needs to obtain int values for the characters of the buffer in a
2/3-independent manner.  This is easily accomplished by converting it
to a bytearray, which works in both Python 2 and Python 3.  This isn't
exactly the same as the Python 3 'bytes' type, since it's
unnecessarily mutable, but it's an easy fix that doesn't require
version conditionals.

Also, test_maidenhead.py works with Python 3 after updating the
libraries, so the shebang line and comments now reflect that.

TESTED:
Ran them under Python 2.6, 2.7, and 3.2-3.5 (with appropriate builds of
the extensions).
---
 gpscat             | 19 +++++++++++--------
 test_maidenhead.py |  4 +++-
 2 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/gpscat b/gpscat
index e3342f3..53c3378 100755
--- a/gpscat
+++ b/gpscat
@@ -1,10 +1,13 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
 #
 # This file is Copyright (c) 2010 by the GPSD project
 # BSD terms apply: see the file COPYING in the distribution root for details.
 #
 # Display GPS output.  Hexify it if necessary.
 #
+
+# This code runs compatibly under Python 2 and 3.x for x >= 2.
+# Preserve this property!
 from __future__ import print_function, division
 
 import os, sys, termios, socket, select, getopt, curses.ascii
@@ -22,11 +25,11 @@ highhalf_latch = True
 
 def hexdump(st):
     dmp = ""
-    for (_i, ch) in enumerate(st):
-        if curses.ascii.isprint(ord(ch)) or curses.ascii.isspace(ord(ch)):
-            dmp += ch
+    for ch in bytearray(st):  # bytearray gets array of ints in Python 2 and 3
+        if curses.ascii.isprint(ch) or curses.ascii.isspace(ch):
+            dmp += chr(ch)
         else:
-            dmp += "\\x%02x" % ord(ch)
+            dmp += "\\x%02x" % ch
     return dmp
 
 debuglevel = 0
@@ -41,7 +44,7 @@ def printusage():
     sys.stderr.write("usage: gpscat [-s speed] [-p] [-t] [-D debuglevel] 
serial-port\n")
 
 if __name__ == '__main__':
-    buf = ""
+    buf = bytes()
     try:
         try:
             (options, arguments) = getopt.getopt(sys.argv[1:], "hps:tD:V")
@@ -111,7 +114,7 @@ if __name__ == '__main__':
         poller = select.poll()
         poller.register(tty, select.POLLIN)
 
-        buf = ""
+        buf = bytes()
         if not rawmode:
             getter = sniffer.new()
             sniffer.register_report(reporter)
@@ -122,7 +125,7 @@ if __name__ == '__main__':
                 if rawmode:
                     buf += os.read(tty, NMEA_MAX)
                     sys.stdout.write(hexdump(buf))
-                    buf = ""
+                    buf = bytes()
                 else:
                     (length, ptype, packet, counter) = getter.get(tty)
                     seqno += 1
diff --git a/test_maidenhead.py b/test_maidenhead.py
index e8d5459..13b6447 100755
--- a/test_maidenhead.py
+++ b/test_maidenhead.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
 #
 # Test grid locator conversion.
 #
@@ -7,6 +7,8 @@
 # Test conversions generated using
 #       http://f6fvy.free.fr/qthLocator/
 
+# This code runs compatibly under Python 2 and 3.x for x >= 2.
+# Preserve this property!
 from __future__ import print_function
 
 import sys, gps.clienthelpers
-- 
2.8.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]