diff --git a/python/src/deltachat/__init__.py b/python/src/deltachat/__init__.py index ea8786454..4a8f21bf9 100644 --- a/python/src/deltachat/__init__.py +++ b/python/src/deltachat/__init__.py @@ -34,13 +34,14 @@ def py_dc_callback(ctx, evt, data1, data2): if data1 and event_sig_types & 1: data1 = ffi.string(ffi.cast('char*', data1)).decode("utf8") if data2 and event_sig_types & 2: + data2 = ffi.string(ffi.cast('char*', data2)).decode("utf8") try: - data2 = ffi.string(ffi.cast('char*', data2)).decode("utf8") + if isinstance(data2, bytes): + data2 = data2.decode("utf8") except UnicodeDecodeError: - # XXX ignoring this error is not quite correct but for now + # XXX ignoring the decode error is not quite correct but for now # i don't want to hunt down encoding problems in the c lib - data2 = ffi.string(ffi.cast('char*', data2)) - + pass try: ret = callback(ctx, evt_name, data1, data2) if ret is None: diff --git a/python/src/deltachat/cutil.py b/python/src/deltachat/cutil.py index 80c1f3abe..aa739484c 100644 --- a/python/src/deltachat/cutil.py +++ b/python/src/deltachat/cutil.py @@ -16,4 +16,4 @@ def iter_array(dc_array_t, constructor): def from_dc_charpointer(obj): - return ffi.string(obj).decode("utf8") + return ffi.string(ffi.gc(obj, lib.dc_str_unref)).decode("utf8") diff --git a/python/src/deltachat/message.py b/python/src/deltachat/message.py index 6e3d05625..745893f40 100644 --- a/python/src/deltachat/message.py +++ b/python/src/deltachat/message.py @@ -142,7 +142,7 @@ class Message(object): import email.parser mime_headers = lib.dc_get_mime_headers(self._dc_context, self.id) if mime_headers: - s = ffi.string(mime_headers) + s = ffi.string(ffi.gc(mime_headers, lib.dc_str_unref)) if isinstance(s, bytes): s = s.decode("ascii") return email.message_from_string(s)