Skip to content

Commit 35630ee

Browse files
committed
Cleanup in __del__
1 parent dbedfbd commit 35630ee

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

cdblib/compat.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import atexit
2-
31
from itertools import chain, cycle, islice, repeat
42
from mmap import mmap, ACCESS_READ
53
from os import rename
@@ -27,8 +25,6 @@ def __init__(self, cdb, tmp, encoding='utf-8'):
2725
self.encoding = encoding
2826

2927
self._temp_obj = open(self.fntmp, 'wb')
30-
atexit.register(self._cleanup)
31-
3228
self._writer = Writer(self._temp_obj, strict=True)
3329
self.numentries = 0
3430
self._finished = False
@@ -39,6 +35,9 @@ def _cleanup(self):
3935
except Exception:
4036
pass
4137

38+
def __del__(self):
39+
self._cleanup()
40+
4241
def add(self, key, data):
4342
"""Store a record in the database.
4443
"""
@@ -90,7 +89,6 @@ def __init__(self, f, encoding='utf-8'):
9089

9190
self._file_obj = open(self._file_path, mode='rb')
9291
self._mmap_obj = mmap(self._file_obj.fileno(), 0, access=ACCESS_READ)
93-
atexit.register(self._cleanup)
9492
self._reader = Reader(self._mmap_obj)
9593

9694
self._keys = self._get_key_iterator()
@@ -103,6 +101,9 @@ def _cleanup(self):
103101
except Exception:
104102
pass
105103

104+
def __del__(self):
105+
self._cleanup()
106+
106107
def _unique_keys(self, keys):
107108
seen = set()
108109
seen_add = seen.add

docs/compat.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ The `python-cdb` package accepts integer file descriptors as well as file paths
143143
in `init()` and `cdbmake()`. This module does not.
144144

145145
The `cdb` objects (returned by the `init()` function) and the `cdbmake` objects
146-
close their open file objects at interpreter exit.
146+
close their open file objects when they are garbage collected.
147147
You may call the `._cleanup()` method on either one to close the objects
148148
yourself (this method is not avaialble when using the `python-cdb` package).
149149

0 commit comments

Comments
 (0)