Skip to content

Commit c52e4fa

Browse files
committed
Added UTF-8 support over the wire for python 3
1 parent c76a03f commit c52e4fa

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

pynuodb/encodedsession.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ def putString(self, value):
411411
Appends a String to the message.
412412
@type value str
413413
"""
414+
if systemVersion is '3' and not self.isASCII(value):
415+
value = value.encode('utf-8').decode('latin-1')
414416
length = len(value)
415417
if length < 40:
416418
packed = chr(protocol.UTF8LEN0 + length) + value
@@ -604,11 +606,17 @@ def getString(self):
604606
typeCode = self._getTypeCode()
605607

606608
if typeCode in range(protocol.UTF8LEN0, protocol.UTF8LEN39 + 1):
607-
return self._takeBytes(typeCode - 109)
609+
value = self._takeBytes(typeCode - 109)
610+
if systemVersion is '3' and not self.isASCII(value):
611+
value = value.encode('latin-1').decode('utf-8')
612+
return value
608613

609614
if typeCode in range(protocol.UTF8COUNT1, protocol.UTF8COUNT4 + 1):
610615
strLength = fromByteString(self._takeBytes(typeCode - 68))
611-
return self._takeBytes(strLength)
616+
value = self._takeBytes(strLength)
617+
if systemVersion is '3' and not self.isASCII(value):
618+
value = value.encode('latin-1').decode('utf-8')
619+
return value
612620

613621
raise DataError('Not a string')
614622

@@ -873,3 +881,11 @@ def _takeBytes(self, length):
873881
return self.__input[self.__inpos:self.__inpos + length]
874882
finally:
875883
self.__inpos += length
884+
885+
def isASCII(self, data):
886+
try:
887+
data.encode('ascii')
888+
except UnicodeEncodeError:
889+
return False
890+
else:
891+
return True

0 commit comments

Comments
 (0)