diff options
author | Justus Winter <4winter@informatik.uni-hamburg.de> | 2012-02-15 22:41:16 +0100 |
---|---|---|
committer | Justus Winter <4winter@informatik.uni-hamburg.de> | 2012-02-15 22:41:16 +0100 |
commit | 36ce7e3c989f6f66a4b250483de3b45e902d68d1 (patch) | |
tree | 6f826cd34ef95bad9d3cda529cab3b8845f9a5be /bindings/python/notmuch | |
parent | b2734519db78fdec76eeafc5fe8f5631a6436cf6 (diff) |
python: implement the context manager protocol for database objects
Signed-off-by: Justus Winter <4winter@informatik.uni-hamburg.de>
Diffstat (limited to 'bindings/python/notmuch')
-rw-r--r-- | bindings/python/notmuch/database.py | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/bindings/python/notmuch/database.py b/bindings/python/notmuch/database.py index d0e38dda..533948c4 100644 --- a/bindings/python/notmuch/database.py +++ b/bindings/python/notmuch/database.py @@ -41,6 +41,10 @@ class Database(object): :exc:`XapianError` as the underlying database has been 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:: Any function in this class can and will throw an @@ -206,6 +210,18 @@ class Database(object): self._close(self._db) 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): """Returns the file path of an open database""" self._assert_db_is_initialized() |