From b32911b74025c5723834e9eda2f2c72e73e541cf Mon Sep 17 00:00:00 2001 From: Maciej Lach Date: Tue, 12 Jan 2016 14:49:44 +0100 Subject: [PATCH] Allow configuration of the encoding used in deserialization --- qpython/qconnection.py | 6 ++++-- qpython/qreader.py | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/qpython/qconnection.py b/qpython/qconnection.py index 1fbf447..162b357 100644 --- a/qpython/qconnection.py +++ b/qpython/qconnection.py @@ -74,7 +74,7 @@ class QConnection(object): strings are encoded as q strings instead of chars, **Default**: ``False`` ''' - def __init__(self, host, port, username = None, password = None, timeout = None, **options): + def __init__(self, host, port, username = None, password = None, timeout = None, encoding = 'latin-1', **options): self.host = host self.port = port self.username = username @@ -85,6 +85,8 @@ def __init__(self, host, port, username = None, password = None, timeout = None, self.timeout = timeout + self._encoding = encoding + self._options = MetaData(**CONVERSION_OPTIONS.union_dict(**options)) @@ -160,7 +162,7 @@ def is_connected(self): def _initialize(self): '''Performs a IPC protocol handshake.''' credentials = (self.username if self.username else '') + ':' + (self.password if self.password else '') - credentials = credentials.encode('latin-1') + credentials = credentials.encode(self._encoding) self._connection.send(credentials + b'\3\0') response = self._connection.recv(1) diff --git a/qpython/qreader.py b/qpython/qreader.py index bac1e92..bf6b13b 100644 --- a/qpython/qreader.py +++ b/qpython/qreader.py @@ -117,9 +117,10 @@ def __new__(cls, *args, **kwargs): return super(QReader, cls).__new__(cls) - def __init__(self, stream): + def __init__(self, stream, encoding = 'latin-1'): self._stream = stream self._buffer = QReader.BytesBuffer() + self._encoding = encoding def read(self, source = None, **options): @@ -256,7 +257,7 @@ def _read_symbol(self, qtype = QSYMBOL): @parse(QCHAR) def _read_char(self, qtype = QCHAR): - return chr(self._read_atom(QCHAR)).encode('latin-1') + return chr(self._read_atom(QCHAR)).encode(self._encoding) @parse(QGUID)