Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing previews in Journal, and UnicodeWarning in log #16

Closed
quozl opened this issue Aug 14, 2019 · 10 comments
Closed

Missing previews in Journal, and UnicodeWarning in log #16

quozl opened this issue Aug 14, 2019 · 10 comments

Comments

@quozl
Copy link
Contributor

quozl commented Aug 14, 2019

/usr/lib/python2.7/dist-packages/sugar3/datastore/datastore.py:113: \
UnicodeWarning: Unicode unequal comparison failed to convert both \
arguments to Unicode - interpreting them as being unequal
  if key not in self._properties or self._properties[key] != value:
@quozl
Copy link
Contributor Author

quozl commented Aug 15, 2019

Also occurs in Clock activity.

/usr/lib/python2.7/dist-packages/sugar3/datastore/datastore.py:113: \
UnicodeWarning: Unicode unequal comparison failed to convert both \
arguments to Unicode - interpreting them as being unequal
  if key not in self._properties or self._properties[key] != value:

Does not occur in Read if run in Python 3.

@quozl quozl transferred this issue from sugarlabs/read-activity Aug 15, 2019
@quozl
Copy link
Contributor Author

quozl commented Aug 28, 2019

Reproduced by running Read in Python 2. Instrumented code with tracebacks. The warning occurs when the preview metadata is changed for the third time. The sequence of changes to preview are;

1566952256.173391 ERROR root: __setitem__ key='preview' value=''
    in `_initialise_journal_object`, file src/sugar3/actiivty/activity.py
1566952257.608579 ERROR root: __setitem__ key='preview' value=dbus.ByteArray('\x89PNG ...
    in `save`, file src/sugar3/actiivty/activity.py
1566952257.760859 ERROR root: __setitem__ key=dbus.String(u'preview') value=dbus.String(u'\\\\x89PNG\\\\r ...
    in `__object_updated_cb`, file src/sugar3/datastore/datastore.py

The warning happens when the updated signal is delivered after the activity has updated the preview metadata. The updated signal is handled by setting the data again, which is usually harmless because the data came from the activity itself.

The warning happens because the binary data in preview could not be converted from dbus.String; with a UnicodeDecodeError, because the data contains byte sequences which are not Unicode.

All other forms of metadata are textual. Only preview triggers this.

@quozl
Copy link
Contributor Author

quozl commented Aug 28, 2019

The binary metadata for the preview returned by get_properties has a type of dbus.String instead of the expected type dbus.ByteArray. Using dbus-monitor shows that two items of non-string metadata (timestamp and preview) are sent to the datastore, but when they come back from the datastore they have changed type to string. The datastore has stopped being type-transparent.

Looks like this happened in 6f4fcae ("Port to Python 3").

@pro-panda, it was about this time last year, but can you look at those lines and tell me why the metadata is being forced to string?

@quozl quozl transferred this issue from sugarlabs/sugar-toolkit-gtk3 Aug 28, 2019
@quozl quozl added this to Critical Issues in Port to Python 3 via Six Aug 28, 2019
@quozl
Copy link
Contributor Author

quozl commented Aug 28, 2019

May be the cause of missing previews in Journal. Detail view shows traceback;

Traceback (most recent call last):
  File "/usr/lib/python3.7/dist-packages/jarabe/journal/detailview.py", line 63, in _update_view
    self._expanded_entry.set_metadata(self._metadata)
  File "/usr/lib/python3.7/dist-packages/jarabe/journal/expandedentry.py", line 336, in set_metadata
    self._preview_box.add(self._create_preview())
  File "/usr/lib/python3.7/dist-packages/jarabe/journal/expandedentry.py", line 378, in _create_preview
    pixbuf = get_preview_pixbuf(metadata.get('preview', ''))
  File "/usr/lib/python3.7/dist-packages/sugar3/graphics/objectchooser.py", line 87, in get_preview_pixbuf
    png_file = six.StringIO(preview_data)
TypeError: initial_value must be str or None, not bytes

@quozl quozl changed the title UnicodeWarning in log Missing previews in Journal, and UnicodeWarning in log Aug 29, 2019
@mdengler
Copy link

That 6f4fcae ("Port to Python 3") commit is bad in that respect. The metadata was stored as bytes before, and there is a big comment about how it has to be stored as bytes and how, coincidentally, str(bytes_data) didn't interfere, but would in the future. The future has come, and the current port/code is blindly converting everything to unicode. It should not be.

@quozl
Copy link
Contributor Author

quozl commented Aug 30, 2019

Thanks. I agree. I'll look at it in about four hours if nobody else does.

@rhl-bthr
Copy link
Contributor

rhl-bthr commented Aug 30, 2019

From memory, All values are converted to bytes in

PyObject *args = Py_BuildValue ("(y#)", value_buf, file_size);

and ultimately returning strings of format "b'<string>'"

@mdengler
Copy link

From memory, All values are converted to bytes in
[metadatareader.add_property]
and ultimately returning strings of format "b'<string>'"

Seems like there should be no strings (or StringIO) in the python code, then, and rather bytes (or BytesIO).

@martin-langhoff
Copy link

I agree with @mdengler 's comments. Py2 string handling was /mostly/ 8-bit clean, and Py3 string handling is most definitely not. IMHO datastore should not ever try to interpret them -- I'd remove all casts to string.

Unfortunately I don't have a test env handy to run Sugar components (and I'm about to have to dodge a hurricane). I do have some recent/current Fedora systems.

quozl added a commit that referenced this issue Sep 2, 2019
Previews were missing, on-disk metadata for previews was wrong data
type, and UnicodeWarnings were in activity logs;

UnicodeWarning: Unicode unequal comparison failed to convert both \
arguments to Unicode - interpreting them as being unequal
  if key not in self._properties or self._properties[key] != value:

Convert any non-bytes metadata to bytes before writing to disk.  Convert
back to expected data type when reading from disk.  Recognise specific
keys and data types; all other values are dbus.String

Regression introduced by 9e21c0a ("Port to Python 3").

Fixes #16
quozl added a commit to sugarlabs/sugar-toolkit-gtk3 that referenced this issue Sep 2, 2019
Preview metadata in 0.114 and earlier was returned by datastore client
as a dbus.ByteArray.

Regression introduced by aa8a5e7 ("Port from Python 2 to six").

Traceback (most recent call last):
  File "/usr/lib/python3.7/dist-packages/jarabe/journal/expandedentry.py", line 378, in _create_preview
    pixbuf = get_preview_pixbuf(metadata.get('preview', ''))
  File "/usr/lib/python3.7/dist-packages/sugar3/graphics/objectchooser.py", line 85, in get_preview_pixbuf
    preview_data = base64.b64decode(preview_data)
  File "/usr/lib/python3.7/base64.py", line 87, in b64decode
    return binascii.a2b_base64(s)
binascii.Error: Invalid base64-encoded string: number of data characters (237) cannot be 1 more than a multiple of 4

Related to
sugarlabs/sugar-datastore#16
quozl added a commit to sugarlabs/sugar-toolkit-gtk3 that referenced this issue Sep 2, 2019
Preview metadata in 0.114 and earlier was returned by datastore client
as a dbus.ByteArray.

Regression introduced by aa8a5e7 ("Port from Python 2 to six").

Traceback (most recent call last):
  File "/usr/lib/python3.7/dist-packages/jarabe/journal/expandedentry.py", line 378, in _create_preview
    pixbuf = get_preview_pixbuf(metadata.get('preview', ''))
  File "/usr/lib/python3.7/dist-packages/sugar3/graphics/objectchooser.py", line 85, in get_preview_pixbuf
    preview_data = base64.b64decode(preview_data)
  File "/usr/lib/python3.7/base64.py", line 87, in b64decode
    return binascii.a2b_base64(s)
binascii.Error: Invalid base64-encoded string: number of data characters (237) cannot be 1 more than a multiple of 4

Related to
sugarlabs/sugar-datastore#16
@quozl
Copy link
Contributor Author

quozl commented Sep 2, 2019

Think I've fixed it.

#18
sugarlabs/sugar-toolkit-gtk3#428

quozl added a commit to sugarlabs/sugar-toolkit-gtk3 that referenced this issue Sep 3, 2019
Preview metadata in 0.114 and earlier was returned by datastore client
as a dbus.ByteArray.

Regression introduced by aa8a5e7 ("Port from Python 2 to six").

Traceback (most recent call last):
  File "/usr/lib/python3.7/dist-packages/jarabe/journal/expandedentry.py", line 378, in _create_preview
    pixbuf = get_preview_pixbuf(metadata.get('preview', ''))
  File "/usr/lib/python3.7/dist-packages/sugar3/graphics/objectchooser.py", line 85, in get_preview_pixbuf
    preview_data = base64.b64decode(preview_data)
  File "/usr/lib/python3.7/base64.py", line 87, in b64decode
    return binascii.a2b_base64(s)
binascii.Error: Invalid base64-encoded string: number of data characters (237) cannot be 1 more than a multiple of 4

Related to
sugarlabs/sugar-datastore#16
@quozl quozl closed this as completed in 80fa75f Sep 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

4 participants