summaryrefslogtreecommitdiff
path: root/gps
diff options
context:
space:
mode:
authorFred Wright <fw@fwright.net>2016-12-24 16:01:05 -0800
committerFred Wright <fw@fwright.net>2016-12-24 16:17:36 -0800
commit04ac607bcf742785f08feef5e3f8ce4a85858a05 (patch)
tree69575498e142dabad9cb083726c45c06f0a7668d /gps
parent1b0eb63a9646eaf80bec7b77221c5fdb270fd07d (diff)
downloadgpsd-04ac607bcf742785f08feef5e3f8ce4a85858a05.tar.gz
Adds bytes-typed alternative response to Python client.
In Python 2, 'bytes' and 'str' are synonyms, but in Python 3 they are not. Since the client may return binary data in some cases, 'bytes' is a more appropriate data type. Rather than requiring double conversions in this case (the socket returns 'bytes'), this adds an alternative response item which is typed as 'bytes'. Although the accessor method 'data()' has always existed, it's shadowed by a name collision in some cases, resulting in the requirement that the caller access the 'response' item directly (as noted in the documentation). Thus, it's not possible to avoid the conversion to 'str' in the cases where it isn't actually used. This change doesn't bother to add an accessor method for 'bresponse', for consistency with the way the API is normally used, though that should be fixed if this aspect of the API is ever cleaned up. TESTED: Ran "scons check" on OSX with all supported versions of Python.
Diffstat (limited to 'gps')
-rw-r--r--gps/client.py17
1 files changed, 12 insertions, 5 deletions
diff --git a/gps/client.py b/gps/client.py
index 5715dfe7..464ce689 100644
--- a/gps/client.py
+++ b/gps/client.py
@@ -16,7 +16,7 @@ class gpscommon(object):
def __init__(self, host="127.0.0.1", port=GPSD_PORT, verbose=0):
self.sock = None # in case we blow up in connect
- self.linebuffer = ""
+ self.linebuffer = b''
self.verbose = verbose
if host is not None:
self.connect(host, port)
@@ -74,10 +74,10 @@ class gpscommon(object):
"Wait for and read data being streamed from the daemon."
if self.verbose > 1:
sys.stderr.write("poll: reading from daemon...\n")
- eol = self.linebuffer.find('\n')
+ eol = self.linebuffer.find(b'\n')
if eol == -1:
# RTCM3 JSON can be over 4.4k long, so go big
- frag = polystr(self.sock.recv(8192))
+ frag = self.sock.recv(8192)
self.linebuffer += frag
if self.verbose > 1:
sys.stderr.write("poll: read complete.\n")
@@ -86,7 +86,7 @@ class gpscommon(object):
sys.stderr.write("poll: returning -1.\n")
# Read failed
return -1
- eol = self.linebuffer.find('\n')
+ eol = self.linebuffer.find(b'\n')
if eol == -1:
if self.verbose > 1:
sys.stderr.write("poll: returning 0.\n")
@@ -99,7 +99,9 @@ class gpscommon(object):
# We got a line
eol += 1
- self.response = self.linebuffer[:eol]
+ # Provide the response in both 'str' and 'bytes' form
+ self.bresponse = self.linebuffer[:eol]
+ self.response = polystr(self.bresponse)
self.linebuffer = self.linebuffer[eol:]
# Can happen if daemon terminates while we're reading.
@@ -111,6 +113,11 @@ class gpscommon(object):
# We got a \n-terminated line
return len(self.response)
+ # Note that the 'data' method is sometimes shadowed by a name
+ # collision, rendering it unusable. The documentation recommends
+ # accessing 'response' directly. Consequently, no accessor method
+ # for 'bresponse' is currently provided.
+
def data(self):
"Return the client data buffer."
return self.response