Message321226
There is a flaw in the example in the documentation for contextlib.nullcontext.
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
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 issue34066). 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(). |
|
Date |
User |
Action |
Args |
2018-07-07 17:33:26 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, ncoghlan |
2018-07-07 17:33:25 | serhiy.storchaka | set | messageid: <1530984805.95.0.56676864532.issue34067@psf.upfronthosting.co.za> |
2018-07-07 17:33:25 | serhiy.storchaka | link | issue34067 messages |
2018-07-07 17:33:25 | serhiy.storchaka | create | |
|