[PATCH 1/1] Drop @contextmanager from finalized() and buptest

1 view
Skip to first unread message

Rob Browning

unread,
Jun 25, 2022, 12:58:44 PM6/25/22
to bup-...@googlegroups.com
Because it involves a generator, @contextmanager creates managers that
are incompatible with ExitStack.pop_all() because they close during
that call -- exactly what pop_all() is intended to avoid.

So begin dropping @contextmanager in preference to explicit context
management classes/methods.

cf. https://github.com/python/cpython/issues/88458

Signed-off-by: Rob Browning <r...@defaultvalue.org>
---

Proposed. This is in preparation for a broader fix to new file
(index, pack, hlinkdb) permissions.

lib/bup/helpers.py | 20 ++++++++++----------
test/lib/buptest/__init__.py | 1 -
2 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/lib/bup/helpers.py b/lib/bup/helpers.py
index a2ac6be5..a14cb1ee 100644
--- a/lib/bup/helpers.py
+++ b/lib/bup/helpers.py
@@ -2,7 +2,7 @@

from __future__ import absolute_import, division
from collections import namedtuple
-from contextlib import contextmanager
+from contextlib import ExitStack
from ctypes import sizeof, c_void_p
from math import floor
from os import environ
@@ -31,15 +31,15 @@ def nullcontext_if_not(manager):
return manager if manager is not None else nullcontext()


-@contextmanager
-def finalized(enter_result=None, finalize=None):
- assert finalize
- try:
- yield enter_result
- except BaseException as ex:
- with pending_raise(ex):
- finalize(enter_result)
- finalize(enter_result)
+class finalized:
+ def __init__(self, enter_result=None, finalize=None):
+ assert finalize
+ self.finalize = finalize
+ self.enter_result = enter_result
+ def __enter__(self):
+ return self.enter_result
+ def __exit__(self, exc_type, exc_value, traceback):
+ self.finalize(self.enter_result)


sc_page_size = os.sysconf('SC_PAGE_SIZE')
diff --git a/test/lib/buptest/__init__.py b/test/lib/buptest/__init__.py
index e2b5284d..627a8e6f 100644
--- a/test/lib/buptest/__init__.py
+++ b/test/lib/buptest/__init__.py
@@ -1,7 +1,6 @@

from __future__ import absolute_import, print_function
from collections import namedtuple
-from contextlib import contextmanager
from os.path import abspath, basename, dirname, realpath
from pipes import quote
from subprocess import PIPE, Popen
--
2.30.2

Rob Browning

unread,
Jul 1, 2022, 2:40:49 PM7/1/22
to bup-...@googlegroups.com
Rob Browning <r...@defaultvalue.org> writes:

> Because it involves a generator, @contextmanager creates managers that
> are incompatible with ExitStack.pop_all() because they close during
> that call -- exactly what pop_all() is intended to avoid.
>
> So begin dropping @contextmanager in preference to explicit context
> management classes/methods.
>
> cf. https://github.com/python/cpython/issues/88458
>
> Signed-off-by: Rob Browning <r...@defaultvalue.org>

Pushed.

--
Rob Browning
rlb @defaultvalue.org and @debian.org
GPG as of 2011-07-10 E6A9 DA3C C9FD 1FF8 C676 D2C4 C0F0 39E9 ED1B 597A
GPG as of 2002-11-03 14DD 432F AE39 534D B592 F9A0 25C8 D377 8C7E 73A4
Reply all
Reply to author
Forward
0 new messages