summaryrefslogtreecommitdiff
path: root/gps
diff options
context:
space:
mode:
authorFred Wright <fw@fwright.net>2016-12-24 17:00:20 -0800
committerFred Wright <fw@fwright.net>2016-12-25 12:53:04 -0800
commitadcba9ae23b8bce85cf7f3f25e6b30113d7a1e5a (patch)
tree483de8d842a87ae21f0bafb0cc6ff9d7d68284ea /gps
parent04ac607bcf742785f08feef5e3f8ce4a85858a05 (diff)
downloadgpsd-adcba9ae23b8bce85cf7f3f25e6b30113d7a1e5a.tar.gz
Changes fake.py stream type to 'bytes'.
Given that the data returned bye fake.py may include binary data, 'bytes' is a more appropriate type. Unlike the client API, where it's easy to provide both return types, it would be less convenient to do that here, so 'bytes' is preferred as the one choice (and is consistent with network sockets). When sending such data to stdout (or stderr), the straightforward method is to write to sys.stdXXX.buffer rather than sys.stdXXX. That doesn't exist in Python 2, but a helper function is now provided to get the appropriate 'bytes' stream for stdXXX. Since no previous *release* of GPSD is compatible with Python 3, changing this now doesn't break anything based on released code. TESTED: Ran "scons check" on OSX with all supported Python versions.
Diffstat (limited to 'gps')
-rw-r--r--gps/fake.py12
-rw-r--r--gps/misc.py8
2 files changed, 15 insertions, 5 deletions
diff --git a/gps/fake.py b/gps/fake.py
index 830b1e90..5ab636a6 100644
--- a/gps/fake.py
+++ b/gps/fake.py
@@ -31,10 +31,12 @@ identified by the string naming its slave device.
TestSession also has methods to start and end client sessions. Daemon
responses to a client are fed to a hook function which, by default,
-discards them. You can change the hook to sys.stdout.write() to dump
-responses to standard output (this is what the gpsfake executable
-does) or do something more exotic. A client session is identified by a
-small integer that counts the number of client session starts.
+discards them. Note that this data is 'bytes' to accommodate possible
+binary data in Python 3; use polystr() if you need a str. You can
+change the hook to misc.get_bytes_stream(sys.stdout).write to dump
+responses to standard output (this is what the gpsfake executable does)
+or do something more exotic. A client session is identified by a small
+integer that counts the number of client session starts.
There are a couple of convenience methods. TestSession.wait() does nothing,
allowing a specified number of seconds to elapse. TestSession.send()
@@ -700,7 +702,7 @@ class TestSession(object):
while chosen.waiting():
chosen.read()
if chosen.valid & gps.PACKET_SET:
- self.reporter(chosen.response)
+ self.reporter(chosen.bresponse)
if chosen.data["class"] == "DEVICE" and chosen.data["activated"] == 0 and chosen.data["path"] in self.fakegpslist:
self.gps_remove(chosen.data["path"])
self.progress("gpsfake: GPS %s removed (notification)\n" % chosen.data["path"])
diff --git a/gps/misc.py b/gps/misc.py
index f2dd601b..cffe16f0 100644
--- a/gps/misc.py
+++ b/gps/misc.py
@@ -38,6 +38,10 @@ if bytes is str: # In Python 2 these functions can be null transformations
"Dummy stdio wrapper function."
return stream
+ def get_bytes_stream(stream):
+ "Dummy stdio bytes buffer function."
+ return stream
+
else: # Otherwise we do something real
def polystr(o):
@@ -68,6 +72,10 @@ else: # Otherwise we do something real
return io.TextIOWrapper(stream.buffer, encoding=BINARY_ENCODING,
newline="\n", line_buffering=True)
+ def get_bytes_stream(stream):
+ "Standard input/output bytes buffer function"
+ return stream.buffer
+
# some multipliers for interpreting GPS output
# Note: A Texas Foot is ( meters * 3937/1200)