mirror of
https://git.notmuchmail.org/git/notmuch
synced 2024-11-22 10:58:10 +01:00
python: implement the context manager protocol for database objects
Signed-off-by: Justus Winter <4winter@informatik.uni-hamburg.de>
This commit is contained in:
parent
b2734519db
commit
36ce7e3c98
1 changed files with 16 additions and 0 deletions
|
@ -41,6 +41,10 @@ class Database(object):
|
||||||
:exc:`XapianError` as the underlying database has been
|
:exc:`XapianError` as the underlying database has been
|
||||||
modified. Close and reopen the database to continue working with it.
|
modified. Close and reopen the database to continue working with it.
|
||||||
|
|
||||||
|
:class:`Database` objects implement the context manager protocol
|
||||||
|
so you can use the :keyword:`with` statement to ensure that the
|
||||||
|
database is properly closed.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
Any function in this class can and will throw an
|
Any function in this class can and will throw an
|
||||||
|
@ -206,6 +210,18 @@ class Database(object):
|
||||||
self._close(self._db)
|
self._close(self._db)
|
||||||
self._db = None
|
self._db = None
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
'''
|
||||||
|
Implements the context manager protocol.
|
||||||
|
'''
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_value, traceback):
|
||||||
|
'''
|
||||||
|
Implements the context manager protocol.
|
||||||
|
'''
|
||||||
|
self.close()
|
||||||
|
|
||||||
def get_path(self):
|
def get_path(self):
|
||||||
"""Returns the file path of an open database"""
|
"""Returns the file path of an open database"""
|
||||||
self._assert_db_is_initialized()
|
self._assert_db_is_initialized()
|
||||||
|
|
Loading…
Reference in a new issue