Re: Bug#790685: ITP: python-conditional -- conditionally enter a context manager
On Wed, Jul 01, 2015 at 08:14:56AM +0100, Neil Williams wrote:
> If I had upstream code which needed this support, I'd just embed it
> into the project and update the copyright instead of having a package
> for 18 lines in the first place.
Without really wishing to weigh in on whether it should be in Debian
(given an upstream that uses it, I can see it being reasonable for a
Debian maintainer to package it rather than patch away its use), I
recently implemented this elsewhere as the equivalent of:
context = ...
# A no-op context manager.
context = contextlib.contextmanager(lambda: (None for _ in [None]))()
If you can use Python >= 3.3 (in the code in question this is sadly
still some way off), then you can simplify this using
with contextlib.ExitStack() as stack:
The important feature of any of these approaches is to avoid duplicating
the body of the "with" statement.
Colin Watson [firstname.lastname@example.org]