context manager examples

This commit is contained in:
andygoblins
2020-02-22 11:28:50 -06:00
parent f1f450cedc
commit 08af4ce9bd
2 changed files with 16 additions and 21 deletions

View File

@@ -19,18 +19,13 @@ except GnuCashBackendException as backend_exception:
# create a new file, this requires a file type specification
session = Session("xml://%s" % FILE_2, is_new=True)
session.save()
session.end()
session.destroy()
with Session("xml://%s" % FILE_2, is_new=True) as session:
book = session.book
root = book.get_root_account()
# open the new file, try to open it a second time, detect the lock
session = Session(FILE_2)
try:
session_2 = Session(FILE_2)
except GnuCashBackendException as backend_exception:
assert( ERR_BACKEND_LOCKED in backend_exception.errors )
session.end()
session.destroy()
with Session(FILE_2) as session:
try:
session_2 = Session(FILE_2)
except GnuCashBackendException as backend_exception:
assert( ERR_BACKEND_LOCKED in backend_exception.errors )