diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index e1c5b7d2..2aefb8c9 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -14,7 +14,7 @@ from attr import validators as v import deltachat from .capi import ffi, lib -from .cutil import convert_to_bytes_utf8, ffi_unicode, iter_array_and_unref +from .cutil import as_dc_charpointer, from_dc_charpointer, iter_array_and_unref from .types import DC_Context from .chatting import Contact, Chat, Message @@ -62,10 +62,11 @@ class Account(object): :param name: configuration key to lookup (eg "addr" or "mail_pw") :returns: unicode value + :raises: KeyError if no config value was found. """ name = name.encode("utf8") res = lib.dc_get_config(self._dc_context.p, name, b'') - return ffi_unicode(res) + return from_dc_charpointer(res) def is_configured(self): """ determine if the account is configured already. @@ -96,8 +97,8 @@ class Account(object): :param name: display name for this contact (optional) :returns: :class:`Contact` instance. """ - name = convert_to_bytes_utf8(name) - email = convert_to_bytes_utf8(email) + name = as_dc_charpointer(name) + email = as_dc_charpointer(email) contact_id = lib.dc_create_contact(self._dc_context.p, name, email) return Contact(self._dc_context, contact_id) @@ -111,7 +112,7 @@ class Account(object): :returns: list of :class:`Message` objects. """ flags = 0 - query = convert_to_bytes_utf8(query) + query = as_dc_charpointer(query) if only_verified: flags |= lib.DC_GCL_VERIFIED_ONLY if with_self: diff --git a/python/src/deltachat/chatting.py b/python/src/deltachat/chatting.py index 28751f95..77138d15 100644 --- a/python/src/deltachat/chatting.py +++ b/python/src/deltachat/chatting.py @@ -1,6 +1,6 @@ """ chatting related objects: Contact, Chat, Message. """ -from .cutil import convert_to_bytes_utf8, ffi_unicode, iter_array_and_unref +from .cutil import as_dc_charpointer, from_dc_charpointer, iter_array_and_unref from .capi import lib from .types import cached_property, property_with_doc from .types import DC_Context, DC_Contact, DC_Chat, DC_Msg @@ -24,12 +24,12 @@ class Contact(object): @property_with_doc def addr(self): """ normalized e-mail address for this account. """ - return ffi_unicode(lib.dc_contact_get_addr(self._dc_contact.p)) + return from_dc_charpointer(lib.dc_contact_get_addr(self._dc_contact.p)) @property_with_doc def display_name(self): """ display name for this contact. """ - return ffi_unicode(lib.dc_contact_get_display_name(self._dc_contact.p)) + return from_dc_charpointer(lib.dc_contact_get_display_name(self._dc_contact.p)) def is_blocked(self): """ Return True if the contact is blocked. """ @@ -63,7 +63,7 @@ class Chat(object): :param msg: unicode text :returns: the resulting :class:`Message` instance """ - msg = convert_to_bytes_utf8(msg) + msg = as_dc_charpointer(msg) msg_id = lib.dc_send_text_msg(self._dc_context.p, self.id, msg) return Message(self._dc_context, msg_id) @@ -120,7 +120,7 @@ class Message(object): @property_with_doc def text(self): """unicode representation. """ - return ffi_unicode(lib.dc_msg_get_text(self._dc_msg.p)) + return from_dc_charpointer(lib.dc_msg_get_text(self._dc_msg.p)) @property def chat(self): diff --git a/python/src/deltachat/cutil.py b/python/src/deltachat/cutil.py index bf2cf1c6..5249b7a4 100644 --- a/python/src/deltachat/cutil.py +++ b/python/src/deltachat/cutil.py @@ -2,7 +2,7 @@ from .capi import lib from .capi import ffi -def convert_to_bytes_utf8(obj): +def as_dc_charpointer(obj): if obj == ffi.NULL or obj is None: return ffi.NULL if not isinstance(obj, bytes): @@ -18,5 +18,5 @@ def iter_array_and_unref(dc_array_t, constructor): lib.dc_array_unref(dc_array_t) -def ffi_unicode(obj): +def from_dc_charpointer(obj): return ffi.string(obj).decode("utf8") diff --git a/python/tests/test_account.py b/python/tests/test_account.py index 87f4ac71..51aa3933 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -15,6 +15,11 @@ class TestOfflineAccount: with pytest.raises(ValueError): ac1.get_self_contact() + # def test_get_config_fails(self, acfactory): + # ac1 = acfactory.get_offline_account() + # with pytest.raises(KeyError): + # ac1.get_config("123123") + def test_contact_attr(self, acfactory): ac1 = acfactory.get_offline_account() contact1 = ac1.create_contact(email="some1@hello.com", name="some1")