Author serhiy.storchaka
Recipients ncoghlan, serhiy.storchaka
Date 2018-07-07.17:33:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1530984805.95.0.56676864532.issue34067@psf.upfronthosting.co.za>
In-reply-to
Content
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().
History
Date User Action Args
2018-07-07 17:33:26serhiy.storchakasetrecipients: + serhiy.storchaka, ncoghlan
2018-07-07 17:33:25serhiy.storchakasetmessageid: <1530984805.95.0.56676864532.issue34067@psf.upfronthosting.co.za>
2018-07-07 17:33:25serhiy.storchakalinkissue34067 messages
2018-07-07 17:33:25serhiy.storchakacreate