[New-bugs-announce] [issue10049] Add the null context manager to contextlib

Hrvoje Nikšić report at bugs.python.org
Fri Oct 8 13:13:46 CEST 2010

New submission from Hrvoje Nikšić <hniksic at gmail.com>:

I find that I frequently need the "null" (no-op) context manager.  For example, in code such as:

with transaction or contextlib.null():

Since there is no easy expression to create a null context manager, we must resort to workarounds, such as:

if transaction:
    with transaction:
        ... code ...
    ... duplicated code ...

(with the usual options of moving the duplicated code to a function—but still.)

Or by creating ad-hoc null context managers with the help of contextlib.contextmanager:

if transaction is None:
    transaction = contextlib.contextmanager(lambda: iter([None])()
with transaction:

Adding a "null" context manager would be both practical and elegant.  I have attached a patch for contextlib.py

files: fl
messages: 118181
nosy: hniksic
priority: normal
severity: normal
status: open
title: Add the null context manager to contextlib
Added file: http://bugs.python.org/file19158/fl

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list