Skip to content

Commit 3e5cb0f

Browse files
committed
Merge pull request #105 from tvincentNuoDB/master
Added UTF-8 Support over the wire for python 3+
2 parents bd86c14 + c52e4fa commit 3e5cb0f

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

pynuodb/encodedsession.py

Lines changed: 19 additions & 3 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
@@ -445,7 +447,7 @@ def putOpaque(self, value):
445447
"""Appends an Opaque data value to the message."""
446448
data = value.string
447449
length = len(data)
448-
if systemVersion is '3' and type(value) is bytes:
450+
if systemVersion is '3' and type(data) is bytes:
449451
data = data.decode('latin-1')
450452
if length < 40:
451453
packed = chr(protocol.OPAQUELEN0 + length) + data
@@ -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

pynuodb/session.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __str__(self):
6666
class Session(object):
6767

6868
__AUTH_REQ = "<Authorize TargetService=\"%s\" Type=\"SRP\"/>"
69-
__SRP_REQ = "<SRPRequest ClientKey=\"%s\" Cipher=\"RC4\" Username=\"%s\"/>" #Why is this hard coded...
69+
__SRP_REQ = "<SRPRequest ClientKey=\"%s\" Cipher=\"%s\" Username=\"%s\"/>"
7070

7171
__SERVICE_REQ = "<Request Service=\"%s\"%s/>"
7272
__SERVICE_CONN = "<Connect Service=\"%s\"%s/>"
@@ -104,15 +104,14 @@ def port(self):
104104

105105
# NOTE: This routine works only for agents ... see the sql module for a
106106
# still-in-progress example of opening an authorized engine session
107-
def authorize(self, account="domain", password=None):
107+
def authorize(self, account="domain", password=None, cipher='RC4'):
108108
if not password:
109109
raise SessionException("A password is required for authorization")
110110

111111
cp = ClientPassword()
112112
key = cp.genClientKey()
113-
114113
self.send(Session.__AUTH_REQ % self.__service)
115-
response = self.__sendAndReceive(Session.__SRP_REQ % (key, account))
114+
response = self.__sendAndReceive(Session.__SRP_REQ % (key, cipher, account))
116115

117116
root = ElementTree.fromstring(response)
118117
if root.tag != "SRPResponse":

0 commit comments

Comments
 (0)