Skip to content

Commit 61a1c71

Browse files
committed
try..except on UTF8Type serialize for pre-encoded strings
Wrap ustr.decode() with a try..except block to handle failure to encode strings of already-encoded unicode.
1 parent b186ecd commit 61a1c71

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

cassandra/cqltypes.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -528,15 +528,11 @@ def deserialize(byts):
528528

529529
@staticmethod
530530
def serialize(ustr):
531-
# ustr.encode('utf8') fails when the string is already encoded
532-
# this is common if your data comes through other database drivers (e.g. odbc, psycopg2, etc.)
533-
if isinstance(ustr, unicode): # check type explicitly. Unicode will encode successfuly.
534-
return ustr.encode('utf8')
535-
# otherwise, our input string is either already encoded or not unicode to begin with.
536-
# since all cassandra strings are utf-8, we can validate that the ustr is already encoded utf-8 by decoding it
537-
else:
538-
ustr.decode('utf-8') # will raise UnicodeDecodeError if not utf8 encoded byte string.
539-
return ustr # definitely valid :)
531+
try:
532+
return ustr.encode('utf-8')
533+
except UnicodeDecodeError:
534+
# already utf-8
535+
return ustr
540536

541537

542538
class VarcharType(UTF8Type):

0 commit comments

Comments
 (0)