New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-10049: Add a "no-op" (null) context manager to contextlib #4464
Changes from all commits
3cc01ab
ca4d97e
a194b9c
23cc840
9373422
7e6f26e
dec5304
89ffdef
9947ded
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,6 +137,28 @@ Functions and classes provided: | |
``page.close()`` will be called when the :keyword:`with` block is exited. | ||
|
||
|
||
.. _simplifying-support-for-single-optional-context-managers: | ||
|
||
.. function:: nullcontext(enter_result=None) | ||
|
||
Return a context manager that returns enter_result from ``__enter__``, but | ||
otherwise does nothing. It is intended to be used as a stand-in for an | ||
optional context manager, for example:: | ||
|
||
def process_file(file_or_path): | ||
if isinstance(file_or_path, str): | ||
# If string, open file | ||
cm = open(file_or_path) | ||
else: | ||
# Caller is responsible for closing file | ||
cm = nullcontext(file_or_path) | ||
|
||
with cm as file: | ||
# Perform processing on the file | ||
|
||
.. versionadded:: 3.7 | ||
|
||
|
||
.. function:: suppress(*exceptions) | ||
|
||
Return a context manager that suppresses any of the specified exceptions | ||
|
@@ -433,24 +455,6 @@ statements to manage arbitrary resources that don't natively support the | |
context management protocol. | ||
|
||
|
||
Simplifying support for single optional context managers | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if the section heading should be kept but the text changed to direct to nullcontext, so that people following links can see the new helper. Also, if this is the only place that explained that ExitStack can work as a no-op, I think a line about that should be kept. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it definitely should be kept. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree we still need the hyperlink target, but given the clear example in the function's documentation, I think we can still delete the recipe section. |
||
|
||
In the specific case of a single optional context manager, :class:`ExitStack` | ||
instances can be used as a "do nothing" context manager, allowing a context | ||
manager to easily be omitted without affecting the overall structure of | ||
the source code:: | ||
|
||
def debug_trace(details): | ||
if __debug__: | ||
return TraceContext(details) | ||
# Don't do anything special with the context in release mode | ||
return ExitStack() | ||
|
||
with debug_trace(): | ||
# Suite is traced in debug mode, but runs normally otherwise | ||
|
||
|
||
Catching exceptions from ``__enter__`` methods | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Added *nullcontext* no-op context manager to contextlib. This provides a | ||
simpler and faster alternative to ExitStack() when handling optional context | ||
managers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Declare an explicit hyperlink anchor here with a blank line before the function definition: