Blob Blame History Raw
From f1ac68488631ab153dd31b737819fd5cadc3b158 Mon Sep 17 00:00:00 2001
From: Shaan Subbaiah <shaansubbaiah.cs18@bmsce.ac.in>
Date: Sat, 14 Mar 2020 21:13:40 +0530
Subject: [PATCH] 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
https://github.com/sugarlabs/sugar/issues/909

Part of
https://github.com/sugarlabs/sugar/pull/911

Regression introduced
aa18879e9717dfe2d30f249549e9a43d6dd6da4f

Signed-off-by: James Cameron <quozl@laptop.org>
---
 src/jarabe/apisocket.py | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/jarabe/apisocket.py b/src/jarabe/apisocket.py
index bcefedf2e..a15376811 100644
--- a/src/jarabe/apisocket.py
+++ b/src/jarabe/apisocket.py
@@ -122,7 +122,7 @@ def _create_file(self):
 
         self._sequence += 1
         file_path = os.path.join(instance_path, "%d" % self._sequence)
-        file_object = open(file_path, "w")
+        file_object = open(file_path, "wb")
 
         return file_path, file_object
 
@@ -153,7 +153,7 @@ def error_handler(error):
 
     def load(self, request):
         def get_filename_reply_handler(file_name):
-            file_object = open(file_name)
+            file_object = open(file_name, 'rb')
             info["file_object"] = file_object
 
             if "requested_size" in info:
@@ -169,7 +169,7 @@ def error_handler(error):
             self._client.send_error(request, error)
 
         def send_binary(data):
-            self._client.send_binary(chr(stream_id) + data)
+            self._client.send_binary(bytes([stream_id]) + data)
 
         def on_data(data):
             size = struct.unpack("ii", data)[1]
@@ -324,7 +324,7 @@ def _session_started_cb(self, server, session):
 
     def _message_received_cb(self, session, message, client):
         if message.message_type == Message.TYPE_BINARY:
-            stream_id = ord(message.data[0])
+            stream_id = message.data[0]
             stream_monitor = client.stream_monitors[stream_id]
             stream_monitor.on_data(message.data)
             return