Skip to content

Commit c721f57

Browse files
committed
Add global shutdown function
1 parent 2202cff commit c721f57

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

pygeodiff/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
:license: MIT, see LICENSE for more details.
88
"""
99

10-
from .main import GeoDiff
10+
from .main import GeoDiff, shutdown
1111
from .geodifflib import (
1212
GeoDiffLibError,
1313
GeoDiffLibConflictError,
@@ -20,6 +20,7 @@
2020

2121
__all__ = [
2222
"GeoDiff",
23+
"shutdown",
2324
"GeoDiffLibError",
2425
"GeoDiffLibConflictError",
2526
"GeoDiffLibUnsupportedChangeError",

pygeodiff/main.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def _lazy_load(self):
5555
self.context = self.clib.create_context()
5656

5757
def shutdown(self):
58-
if self.context is not None:
58+
if self.context is not None and self.clib.lib is not None:
5959
self.clib.destroy_context(self.context)
6060
self.context = None
6161

@@ -469,3 +469,12 @@ def create_wkb_from_gpkg_header(self, geometry):
469469
"""
470470
self._lazy_load()
471471
return self.clib.create_wkb_from_gpkg_header(self.context, geometry)
472+
473+
def shutdown():
474+
"""
475+
Unloads global GeoDiffLib instance. This breaks all existing GeoDiff
476+
instances!
477+
"""
478+
if GeoDiff._clib_weakref is not None and GeoDiff._clib_weakref() is not None:
479+
GeoDiff._clib_weakref().shutdown()
480+
GeoDiff._clib_weakref = None

pygeodiff/tests/test_lib_iface.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import unittest
88

9-
from pygeodiff import GeoDiff
9+
from pygeodiff import GeoDiff, GeoDiffLibError, shutdown
1010

1111

1212
class UnitTestsLibLazyloading(unittest.TestCase):
@@ -41,3 +41,12 @@ def test_multiple(self):
4141
self.assertIsNotNone(GeoDiff._clib_weakref())
4242
del geodiff2
4343
self.assertIsNone(GeoDiff._clib_weakref())
44+
45+
def test_global_shutdown(self):
46+
geodiff = GeoDiff()
47+
geodiff.version()
48+
self.assertIsNotNone(GeoDiff._clib_weakref)
49+
self.assertIsNotNone(GeoDiff._clib_weakref())
50+
shutdown()
51+
self.assertIsNone(GeoDiff._clib_weakref)
52+
self.assertRaises(AttributeError, geodiff.version)

0 commit comments

Comments
 (0)