3535
3636if six .PY3 :
3737 _number_types = frozenset ((int , float ))
38+ long = int
3839else :
3940 _number_types = frozenset ((int , long , float ))
4041
@@ -217,7 +218,7 @@ def to_binary(cls, val):
217218 more information. This method differs in that if None is passed in,
218219 the result is the empty string.
219220 """
220- return six . binary_type () if val is None else cls .serialize (val )
221+ return b'' if val is None else cls .serialize (val )
221222
222223 @staticmethod
223224 def deserialize (byts ):
@@ -278,7 +279,8 @@ def apply_parameters(cls, subtypes, names=None):
278279 if cls .num_subtypes != 'UNKNOWN' and len (subtypes ) != cls .num_subtypes :
279280 raise ValueError ("%s types require %d subtypes (%d given)"
280281 % (cls .typename , cls .num_subtypes , len (subtypes )))
281- newname = cls .cass_parameterized_type_with (subtypes ).encode ('utf8' )
282+ # newname = cls.cass_parameterized_type_with(subtypes).encode('utf8')
283+ newname = cls .cass_parameterized_type_with (subtypes )
282284 return type (newname , (cls ,), {'subtypes' : subtypes , 'cassname' : cls .cassname })
283285
284286 @classmethod
@@ -309,10 +311,16 @@ class _UnrecognizedType(_CassandraType):
309311 num_subtypes = 'UNKNOWN'
310312
311313
312- def mkUnrecognizedType (casstypename ):
313- return CassandraTypeType (casstypename .encode ('utf8' ),
314- (_UnrecognizedType ,),
315- {'typename' : "'%s'" % casstypename })
314+ if six .PY3 :
315+ def mkUnrecognizedType (casstypename ):
316+ return CassandraTypeType (casstypename ,
317+ (_UnrecognizedType ,),
318+ {'typename' : "'%s'" % casstypename })
319+ else :
320+ def mkUnrecognizedType (casstypename ):
321+ return CassandraTypeType (casstypename .encode ('utf8' ),
322+ (_UnrecognizedType ,),
323+ {'typename' : "'%s'" % casstypename })
316324
317325
318326class BytesType (_CassandraType ):
@@ -321,11 +329,11 @@ class BytesType(_CassandraType):
321329
322330 @staticmethod
323331 def validate (val ):
324- return buffer (val )
332+ return bytearray (val )
325333
326334 @staticmethod
327335 def serialize (val ):
328- return str (val )
336+ return six . binary_type (val )
329337
330338
331339class DecimalType (_CassandraType ):
@@ -386,9 +394,25 @@ def serialize(truth):
386394 return int8_pack (truth )
387395
388396
389- class AsciiType (_CassandraType ):
390- typename = 'ascii'
391- empty_binary_ok = True
397+ if six .PY2 :
398+ class AsciiType (_CassandraType ):
399+ typename = 'ascii'
400+ empty_binary_ok = True
401+ else :
402+ class AsciiType (_CassandraType ):
403+ typename = 'ascii'
404+ empty_binary_ok = True
405+
406+ @staticmethod
407+ def deserialize (byts ):
408+ return byts .decode ('ascii' )
409+
410+ @staticmethod
411+ def serialize (var ):
412+ try :
413+ return var .encode ('ascii' )
414+ except UnicodeDecodeError :
415+ return var
392416
393417
394418class FloatType (_CassandraType ):
@@ -683,7 +707,7 @@ def serialize_safe(cls, themap):
683707 buf = six .BytesIO ()
684708 buf .write (uint16_pack (len (themap )))
685709 try :
686- items = themap .iteritems ()
710+ items = six .iteritems (themap )
687711 except AttributeError :
688712 raise TypeError ("Got a non-map object for a map value" )
689713 for key , val in items :
0 commit comments