From 8b80dea9ddf338ddb4828a54d197fcfae4206ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl> Date: Tue, 8 Dec 2020 17:42:41 +0100 Subject: [PATCH] `Iterator`'s destructor will emit a warning if you forget to close it explicitly. --- README.md | 1 + setup.py | 2 +- tempsdb/chunks.pyx | 4 ++-- tempsdb/iterators.pyx | 13 +++++++++++-- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index dc9036d..c9e8b13 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ You will need to have both snakehouse and satella installed. * iterating and writing at the same time from multiple threads made safe * added `TimeSeries.disable_mmap` +* `Iterator`'s destructor will emit a warning if you forget to close it explicitly. ## v0.4.2 diff --git a/setup.py b/setup.py index dfae4b4..eb9c3a0 100644 --- a/setup.py +++ b/setup.py @@ -25,7 +25,7 @@ if 'CI' in os.environ: setup(name='tempsdb', - version='0.4.3_a3', + version='0.4.3_a4', packages=['tempsdb'], install_requires=['satella>=2.14.21', 'ujson'], ext_modules=build([Multibuild('tempsdb', find_pyx('tempsdb')), ], diff --git a/tempsdb/chunks.pyx b/tempsdb/chunks.pyx index fba7706..417c756 100644 --- a/tempsdb/chunks.pyx +++ b/tempsdb/chunks.pyx @@ -112,7 +112,7 @@ cdef class Chunk: if use_descriptor_access: self.file_lock_object = threading.Lock() - self.mmap = AlternativeMMap(self.file) + self.mmap = AlternativeMMap(self.file, self.file_lock_object) else: try: self.mmap = mmap.mmap(self.file.fileno(), 0) @@ -368,5 +368,5 @@ cpdef Chunk create_chunk(TimeSeries parent, str path, unsigned long long timesta footer[-4:] = b'\x01\x00\x00\x00' # 1 in little endian file.write(footer) file.close() - return Chunk(parent, path, page_size, ) + return Chunk(parent, path, page_size, use_descriptor_access=descriptor_based_access) diff --git a/tempsdb/iterators.pyx b/tempsdb/iterators.pyx index fb86406..4b82ba8 100644 --- a/tempsdb/iterators.pyx +++ b/tempsdb/iterators.pyx @@ -1,4 +1,6 @@ import typing as tp +import warnings + from .chunks cimport Chunk from .series cimport TimeSeries import collections @@ -14,12 +16,17 @@ cdef class Iterator: >>> for timestamp, value in it: >>> ... - It will close itself automatically. + It will close itself automatically via destructor, if you forget to call close. At most basic this implements an iterator interface, iterating over tp.Tuple[int, bytes] - timestamp and data When you're done call :meth:`~tempsdb.iterators.Iterator.close` to release the resources. + + .. versionadded:: 0.4.3 + + A warning will be emitted in the case that destructor has to call + :meth:`~tempsdb.iterators.Iterator.close`. """ def __init__(self, parent: TimeSeries, start: int, stop: int, chunks: tp.List[Chunk]): @@ -41,7 +48,9 @@ cdef class Iterator: self.close() def __del__(self): - self.close() + if not self.closed: + warnings.warn('You forgot to close an Iterator. Please close them explicitly!') + self.close() cpdef void close(self): """ -- GitLab