@@ -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
0 commit comments