Berker Peksag added the comment:
The old tag() example has been replaced with a different example in
https://github.com/python/cpython/commit/bde782bb594edffeabe978abeee2b7082ab9bc2a
(bpo-33468)
--
resolution: -> duplicate
stage: patch review -> resolved
status: open -> closed
cowlinator added the comment:
I would like to second the improved explanation of contextlib.contextmanager,
and additionally point out another problem:
A very important piece of information is missing from the documentation: how
to return data from the
Serhiy Storchaka added the comment:
swap_attr() looks too general. I think something more concrete would be better.
But the documentation already contain other examples for contextmanager.
closing() is good example, and redirect_stdout() would be good example.
--
nosy:
Berker Peksag added the comment:
We also have swap_attr() and swap_item() helpers in
Lib/test/support/__init__.py. I've used a simplified version of swap_attr() in
my patch.
Since this is basically a document improvement I removed Python 2.7 from the
versions field.
--
keywords:
Martin Panter added the comment:
You should probably use try / finally in your context manager to always restore
the attribute. Having said that, I recently wrote a similar context manager,
and then later discovered there is already “unittest.mock.patch” and/or
“unittest.mock.patch.object”
New submission from Terry J. Reedy:
https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager
The current html contextmanager example is 'not recommended' for actual use,
because there are better ways to accomplish the same goal. To me, is also
unsatifactory in that the