Skip to content

Commit f1ac684

Browse files
shaansubbaiahquozl
authored andcommitted
Port to Python 3 - API Socket
When Gears loads a saved journal object, no gears are shown, and Sugar shell.log contains; Traceback (most recent call last): File "jarabe/apisocket.py", line 327, in _message_received_cb stream_id = ord(message.data[0]) TypeError: ord() expected string of length 1, but int found Comparing the type of data between Sugar Live Build with Sugar 0.117 running Python 3 and OLPC OS 18.04 running Python 2: (1) Python 3, stream_id type is int (2) Python 2, stream_id type is str ` Type of streamid: <type 'str'> ` ` Ord(stream_id) type: <type 'int'> ` stream_id is supposed to be an Int, ord() is not required in Python 3 as it was in Python 2, as indexing a bytes buffer yields a string. In Python 3 it yields an integer. Also, for data loaded at DatastoreAPI.save.on_data : (1) Python 3, data type is bytes ` DatastoreAPI.save.on_data(data), data of type: <class 'bytes'> ` ` data[1:].decode('utf-8') of type:<class 'str'> ` (2) Python 2, data type is str ` DatastoreAPI.save.on_data(data), data of type: <type 'str'> ` write() function requires parameter of type str because file is text. In Python 2 the bytes type is the same as the str type. So write() had no issues with Python 2. The commit fixes these and data from the Journal loads properly. Read and write data in binary, so as to avoid conversion to text. Activity data can be saved and resumed, no issues logged. Related to #909 Part of #911 Regression introduced aa18879 Signed-off-by: James Cameron <[email protected]>
1 parent 03c4643 commit f1ac684

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/jarabe/apisocket.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def _create_file(self):
122122

123123
self._sequence += 1
124124
file_path = os.path.join(instance_path, "%d" % self._sequence)
125-
file_object = open(file_path, "w")
125+
file_object = open(file_path, "wb")
126126

127127
return file_path, file_object
128128

@@ -153,7 +153,7 @@ def error_handler(error):
153153

154154
def load(self, request):
155155
def get_filename_reply_handler(file_name):
156-
file_object = open(file_name)
156+
file_object = open(file_name, 'rb')
157157
info["file_object"] = file_object
158158

159159
if "requested_size" in info:
@@ -169,7 +169,7 @@ def error_handler(error):
169169
self._client.send_error(request, error)
170170

171171
def send_binary(data):
172-
self._client.send_binary(chr(stream_id) + data)
172+
self._client.send_binary(bytes([stream_id]) + data)
173173

174174
def on_data(data):
175175
size = struct.unpack("ii", data)[1]
@@ -324,7 +324,7 @@ def _session_started_cb(self, server, session):
324324

325325
def _message_received_cb(self, session, message, client):
326326
if message.message_type == Message.TYPE_BINARY:
327-
stream_id = ord(message.data[0])
327+
stream_id = message.data[0]
328328
stream_monitor = client.stream_monitors[stream_id]
329329
stream_monitor.on_data(message.data)
330330
return

0 commit comments

Comments
 (0)