You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a flaw in the example in the documentation for contextlib.nullcontext.
defprocess_file(file_or_path):
ifisinstance(file_or_path, str):
# If string, open filecm=open(file_or_path)
else:
# Caller is responsible for closing filecm=nullcontext(file_or_path)
withcmasfile:
# Perform processing on the file
The execution can be interrupted by Ctrl-C between opening a file and entering a 'with' block. There is the same problem with the simple "with open(path) as file:", but it can be easily solved (see bpo-34066). It is harder to do for this example, because there are more opcodes between calling open() and entering 'with'. I afraid that this problem can't be solved in general case. Maybe contextlib.nullcontext has a flaw similar to the flaw in contextlib.nested().
That's certainly similar to the problems with contextlib.nested, but I don't think it's as inherently flawed as nested was. What I'd suggest we change the existing example to is this:
fromfunctoolsimportpartialfromosimportfspathdefprocess_file(file_or_path):
try:
as_fspath=os.fspath(file_or_path)
exceptTypeError:
# If it's a file, caller is responsible for closing itmake_cm=partial(nullcontext, file_or_path)
else:
# If it's a path, open file when the context is enteredmake_cm=partial(open, as_fspath)
withmake_cm() asfile:
# Perform processing on the file
Optionally, we could also present a cleaner example where a pre-created context manager is passed in and we're just coping with the fact it may be None:
(I'll also note that ExitStack is *far* from being immune to the Ctrl-C problem, as it's implemented in Python itself, which allows its __exit__ method to be interrupted, as well as for interrupt to occur between a resource being created or acquired, and it being registered with the stack)
New changeset c287545 by Nick Coghlan (Daniel Porteous) in branch 'master': bpo-34067: Include a more easily understood example for nullcontext (GH-8158) c287545
Right, the merged PR adds a simpler example that handles "ignore exceptions or not" by selecting between suppress() and nullcontext().
The documentation still needs updating to improve or remove the file-based example, and point out that for resources that get acquired on construction, you either need to delay construction to the start of the with statement, or else use ExitStack instead of nullcontext.
Activity
serhiy-storchaka commentedon Jul 7, 2018
There is a flaw in the example in the documentation for contextlib.nullcontext.
The execution can be interrupted by Ctrl-C between opening a file and entering a 'with' block. There is the same problem with the simple "with open(path) as file:", but it can be easily solved (see bpo-34066). It is harder to do for this example, because there are more opcodes between calling open() and entering 'with'. I afraid that this problem can't be solved in general case. Maybe contextlib.nullcontext has a flaw similar to the flaw in contextlib.nested().
ncoghlan commentedon Jul 8, 2018
That's certainly similar to the problems with contextlib.nested, but I don't think it's as inherently flawed as nested was. What I'd suggest we change the existing example to is this:
Optionally, we could also present a cleaner example where a pre-created context manager is passed in and we're just coping with the fact it may be None:
(I'll also note that ExitStack is *far* from being immune to the Ctrl-C problem, as it's implemented in Python itself, which allows its __exit__ method to be interrupted, as well as for interrupt to occur between a resource being created or acquired, and it being registered with the stack)
ncoghlan commentedon Jul 9, 2018
New changeset c287545 by Nick Coghlan (Daniel Porteous) in branch 'master':
bpo-34067: Include a more easily understood example for nullcontext (GH-8158)
c287545
miss-islington commentedon Jul 9, 2018
New changeset 1b6e217 by Miss Islington (bot) in branch '3.7':
bpo-34067: Include a more easily understood example for nullcontext (GH-8158)
1b6e217
vstinner commentedon Jul 11, 2018
Can we close this issue?
serhiy-storchaka commentedon Jul 11, 2018
No, the merged PR is not directly related to the original issue.
ncoghlan commentedon Jul 11, 2018
Right, the merged PR adds a simpler example that handles "ignore exceptions or not" by selecting between suppress() and nullcontext().
The documentation still needs updating to improve or remove the file-based example, and point out that for resources that get acquired on construction, you either need to delay construction to the start of the with statement, or else use ExitStack instead of nullcontext.
gpshead commentedon Apr 24, 2023
triage: removed the docs and easy tags as the remaining questions weren't right to call clear or "easy" for a new person in a sprint. :)