summaryrefslogtreecommitdiff
path: root/gpscat
diff options
context:
space:
mode:
authorFred Wright <fw@fwright.net>2016-04-08 10:07:50 -0700
committerEric S. Raymond <esr@thyrsus.com>2016-04-09 04:39:16 -0400
commit79991ff14b9e396e319e108d7615d379163347d9 (patch)
treea2dc27580b51e98e22f2d7f1c403a7a0e0a51388 /gpscat
parentd79ddb4f02f8800fe601a5b7f4b1aaae8990ca7c (diff)
downloadgpsd-79991ff14b9e396e319e108d7615d379163347d9.tar.gz
Fixes gpscat and test_maidenhead.py for Python 3.
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).
Diffstat (limited to 'gpscat')
-rwxr-xr-xgpscat19
1 files changed, 11 insertions, 8 deletions
diff --git a/gpscat b/gpscat
index e3342f3a..53c3378e 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