From 01d989e7187bc340506e6039adfb35e28117acae Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Thu, 5 Mar 2020 17:47:03 +0100 Subject: [PATCH] objectstore: remove context manager for new method Turn `ObjectStore.new` into a plain method, since `Object` itself can be used as a context manager, which is now directly returned, instead of internally wrapped in a `with` statement and then yielded. Thus for callers of the method nothing changes and the behavior of `with objectstore.new() as x` is exactly the same. --- osbuild/objectstore.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/osbuild/objectstore.py b/osbuild/objectstore.py index c9a85cc4..05013f6c 100644 --- a/osbuild/objectstore.py +++ b/osbuild/objectstore.py @@ -235,28 +235,27 @@ class ObjectStore: with obj.read() as path: yield path - @contextlib.contextmanager def new(self, base_id=None): """Creates a new temporary `Object`. - This method must be used as a context manager. It returns - a temporary instance of `Object`, which can then be used - for interaction with the store. + It returns a temporary instance of `Object`, the base + optionally set to `base_id`. It can be used to interact + with the store. If changes to the object's content were made (by calling `Object.write`), these must manually be committed to the store via `commit()`. """ - with Object(self) as obj: + obj = Object(self) - if base_id: - # if we were given a base id then this is the base - # content for the new object - # NB: `Object` has copy-on-write semantics, so no - # copying of the data takes places at this point - obj.base = base_id + if base_id: + # if we were given a base id then this is the base + # content for the new object + # NB: `Object` has copy-on-write semantics, so no + # copying of the data takes places at this point + obj.base = base_id - yield obj + return obj def commit(self, obj: Object, object_id: str) -> str: """Commits a Object to the object store