python: first pass at updates for dc_open/dc_close removal

This commit is contained in:
dignifiedquire
2020-05-22 23:56:03 +02:00
parent 2b1d4651fb
commit 8569e1c18b
2 changed files with 18 additions and 43 deletions

View File

@@ -42,21 +42,20 @@ class Account(object):
self.add_account_plugin(self)
self.db_path = db_path
if hasattr(db_path, "encode"):
db_path = db_path.encode("utf8")
self._dc_context = ffi.gc(
lib.dc_context_new(ffi.NULL, as_dc_charpointer(os_name)),
lib.dc_context_new(as_dc_charpointer(os_name), db_path, ffi.NULL),
_destroy_dc_context,
)
if self._dc_context == ffi.NULL:
raise ValueError("Could not dc_context_new: {} {}".format(os_name, db_path))
hook = hookspec.Global._get_plugin_manager().hook
self._shutdown_event = Event()
# open database
self.db_path = db_path
if hasattr(db_path, "encode"):
db_path = db_path.encode("utf8")
if not lib.dc_open(self._dc_context, db_path, ffi.NULL):
raise ValueError("Could not dc_open: {}".format(db_path))
self._event_thread = EventThread(self)
self._configkeys = self.get_config("sys.config_keys").split()
atexit.register(self.shutdown)
@@ -622,15 +621,14 @@ class Account(object):
self.stop_scheduler()
self.log("dc_close")
# the dc_close triggers get_next_event to return ffi.NULL
self.log("remove dc_context")
# the dc_context_unref triggers get_next_event to return ffi.NULL
# which in turns makes the event thread finish execution
lib.dc_close(dc_context)
self._dc_context = None
self.log("wait for event thread to finish")
self._event_thread.wait()
self._dc_context = None
atexit.unregister(self.shutdown)
self._shutdown_event.set()
hook = hookspec.Global._get_plugin_manager().hook

View File

@@ -10,8 +10,8 @@ from deltachat.capi import lib
def test_empty_context():
ctx = capi.lib.dc_context_new(capi.ffi.NULL, capi.ffi.NULL)
capi.lib.dc_close(ctx)
ctx = capi.lib.dc_context_new(capi.ffi.NULL, capi.ffi.NULL, capi.ffi.NULL)
capi.lib.dc_context_unref(ctx)
def test_dc_close_events(tmpdir, acfactory):
@@ -32,24 +32,20 @@ def test_dc_close_events(tmpdir, acfactory):
def test_wrong_db(tmpdir):
dc_context = ffi.gc(
lib.dc_context_new(ffi.NULL, ffi.NULL),
lib.dc_context_unref,
)
p = tmpdir.join("hello.db")
# write an invalid database file
p.write("x123" * 10)
assert not lib.dc_open(dc_context, p.strpath.encode("ascii"), ffi.NULL)
assert ffi.NULL == lib.dc_context_new(ffi.NULL, ffi.NULL, p.strpath.encode("ascii"), ffi.NULL)
def test_empty_blobdir(tmpdir):
db_fname = tmpdir.join("hello.db")
# Apparently some client code expects this to be the same as passing NULL.
ctx = ffi.gc(
lib.dc_context_new(ffi.NULL, ffi.NULL),
lib.dc_context_new(ffi.NULL, ffi.NULL, db_fname.strpath.encode("ascii"), b""),
lib.dc_context_unref,
)
db_fname = tmpdir.join("hello.db")
assert lib.dc_open(ctx, db_fname.strpath.encode("ascii"), b"")
assert ctx != ffi.NULL
def test_event_defines():
@@ -107,30 +103,11 @@ def test_get_info_closed():
def test_get_info_open(tmpdir):
db_fname = tmpdir.join("test.db")
ctx = ffi.gc(
lib.dc_context_new(ffi.NULL, ffi.NULL),
lib.dc_context_new(ffi.NULL, ffi.NULL, db_fname.strpath.encode("ascii"), ffi.NULL),
lib.dc_context_unref,
)
db_fname = tmpdir.join("test.db")
lib.dc_open(ctx, db_fname.strpath.encode("ascii"), ffi.NULL)
info = cutil.from_dc_charpointer(lib.dc_get_info(ctx))
assert 'deltachat_core_version' in info
assert 'database_dir' in info
def test_is_open_closed():
ctx = ffi.gc(
lib.dc_context_new(ffi.NULL, ffi.NULL),
lib.dc_context_unref,
)
assert lib.dc_is_open(ctx) == 0
def test_is_open_actually_open(tmpdir):
ctx = ffi.gc(
lib.dc_context_new(ffi.NULL, ffi.NULL),
lib.dc_context_unref,
)
db_fname = tmpdir.join("test.db")
lib.dc_open(ctx, db_fname.strpath.encode("ascii"), ffi.NULL)
assert lib.dc_is_open(ctx) == 1