sugarlabs / sugar-toolkit-gtk3

Sugar Learning Environment, Activity Toolkit, GTK 3.
GNU Lesser General Public License v2.1
21 stars 80 forks source link

six port: ConfigParser instance has no attribute 'read_string' #438

Closed bhulsken closed 4 years ago

bhulsken commented 4 years ago

read_string not supported, as far as I can see, readfp should work in this case. Seems to work for me at least. Below patch works for me so far.

diff -Naupr sugar-toolkit-gtk3/src/sugar3/bundle/activitybundle.py sugar-toolkit-gtk3-fix/src/sugar3/bundle/activitybundle.py
--- sugar-toolkit-gtk3/src/sugar3/bundle/activitybundle.py      2019-12-31 11:15:36.182323699 +0100
+++ sugar-toolkit-gtk3-fix/src/sugar3/bundle/activitybundle.py  2020-01-01 16:59:26.377075544 +0100
@@ -129,7 +129,7 @@ class ActivityBundle(Bundle):

     def _parse_info(self, info_file):
         cp = ConfigParser()
-        cp.read_string(info_file.read().decode())
+        cp.readfp(info_file)

         section = 'Activity'

@@ -251,7 +251,7 @@ class ActivityBundle(Bundle):
     def _parse_linfo(self, linfo_file):
         cp = ConfigParser()
         try:
-            cp.read_string(linfo_file.read().decode())
+            cp.readfp(linfo_file)
         except ParsingError as e:
             logging.exception('Exception reading linfo file: %s', e)
             return
quozl commented 4 years ago

Thanks. What version of Python? Did you also test installing activity bundles from Browse or Terminal? The change to read_string was made so installing would work, and was tested on Python 3.8. read_string is documented as supported. If you are using Python 2, we will have to wrap this code in a version test.

bhulsken commented 4 years ago

my mistake, forgot to test for python3, indeed this change is needed for python2, but breaks the python3 version (just checked).

according to internet wisdom (see e.g. here https://stackoverflow.com/questions/34447623/wrap-an-open-stream-with-io-textiowrapper) there is no code that would work with python2 and python3, so a version check is needed indeed. Below patch works for me, now actually tested with both python2 and python3 :$

best regards, Bas Hulsken

diff -Naupr sugar-toolkit-gtk3/src/sugar3/bundle/activitybundle.py sugar-toolkit-gtk3-fix/src/sugar3/bundle/activitybundle.py
--- sugar-toolkit-gtk3/src/sugar3/bundle/activitybundle.py      2019-12-31 11:15:36.182323699 +0100
+++ sugar-toolkit-gtk3-fix/src/sugar3/bundle/activitybundle.py  2020-01-02 19:52:02.703951092 +0100
@@ -21,6 +21,7 @@ UNSTABLE.
 """

 from six.moves.configparser import ConfigParser, ParsingError
+import six
 from locale import normalize
 import os
 import shutil
@@ -129,7 +130,10 @@ class ActivityBundle(Bundle):

     def _parse_info(self, info_file):
         cp = ConfigParser()
-        cp.read_string(info_file.read().decode())
+        if six.PY2:
+            cp.readfp(info_file)
+        else:
+            cp.read_string(info_file.read().decode())

         section = 'Activity'

@@ -251,7 +255,10 @@ class ActivityBundle(Bundle):
     def _parse_linfo(self, linfo_file):
         cp = ConfigParser()
         try:
-            cp.read_string(linfo_file.read().decode())
+            if six.PY2:
+                cp.readfp(linfo_file)
+            else:
+                cp.read_string(linfo_file.read().decode())
         except ParsingError as e:
             logging.exception('Exception reading linfo file: %s', e)
             return