2121 Socket.IO protocol related functions
2222"""
2323import logging
24+ import sys
2425
2526
2627logger = logging .getLogger ('tornadio2.proto' )
@@ -40,6 +41,14 @@ def default(self, o):
4041 return super (DecimalEncoder , self ).default (o )
4142 json_decimal_args = {"cls" : DecimalEncoder }
4243
44+
45+ if sys .version_info [0 ] == 2 :
46+ text_type = unicode
47+ string_types = (str , unicode )
48+ else :
49+ text_type = str
50+ string_types = (str ,)
51+
4352# Packet ids
4453DISCONNECT = '0'
4554CONNECT = '1'
@@ -106,15 +115,15 @@ def message(endpoint, msg, message_id=None, force_json=False):
106115 'message_id' : message_id or u'' }
107116
108117 # Trying to send a dict over the wire ?
109- if not isinstance (msg , ( unicode , str ) ) and isinstance (msg , (dict , object )):
118+ if not isinstance (msg , string_types ) and isinstance (msg , (dict , object )):
110119 packed_data .update ({'kind' : JSON ,
111120 'msg' : json .dumps (msg , ** json_decimal_args )})
112121
113122 # for all other classes, including objects. Call str(obj)
114123 # and respect forced JSON if requested
115124 else :
116125 packed_data .update ({'kind' : MESSAGE if not force_json else JSON ,
117- 'msg' : msg if isinstance (msg , unicode ) else str (msg ).decode ('utf-8' )})
126+ 'msg' : msg if isinstance (msg , text_type ) else str (msg ).decode ('utf-8' )})
118127
119128 return packed_message_tpl % packed_data
120129
@@ -224,7 +233,7 @@ def decode_frames(data):
224233
225234 """
226235 # Single message - nothing to decode here
227- assert isinstance (data , unicode ), 'frame is not unicode'
236+ assert isinstance (data , text_type ), 'frame is not unicode'
228237
229238 if not data .startswith (FRAME_SEPARATOR ):
230239 return [data ]
0 commit comments