This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Problem with contextlib.nullcontext
Type: resource usage Stage: needs patch
Components: Versions: Python 3.10, Python 3.9, Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, iritkatriel, miss-islington, ncoghlan, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2018-07-07 17:33 by serhiy.storchaka, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 8158 merged banool, 2018-07-09 13:23
PR 8199 merged miss-islington, 2018-07-09 13:50
Messages (8)
msg321226 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-07-07 17:33
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)
              # 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().
msg321254 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-07-08 04:14
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:

      from functools import partial
      from os import fspath

      def process_file(file_or_path):
              as_fspath = os.fspath(file_or_path)
          except TypeError:
              # If it's a file, caller is responsible for closing it
              make_cm = partial(nullcontext, file_or_path)
              # If it's a path, open file when the context is entered
              make_cm = partial(open, as_fspath)

          with make_cm() as file:
              # 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:

    def update_resource(resource, updates, resource_lock=None):
        if resource_lock is None:
            resource_lock = nullcontext()
        with resource_lock:

(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)
msg321319 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-07-09 13:49
New changeset c287545d62edf1a1ee65727d3c57befa8c99c13a by Nick Coghlan (Daniel Porteous) in branch 'master':
bpo-34067: Include a more easily understood example for nullcontext (GH-8158)
msg321322 - (view) Author: miss-islington (miss-islington) Date: 2018-07-09 14:11
New changeset 1b6e21730417c56748d500a96cafebbaa1bd0120 by Miss Islington (bot) in branch '3.7':
bpo-34067: Include a more easily understood example for nullcontext (GH-8158)
msg321461 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-07-11 14:37
Can we close this issue?
msg321463 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-07-11 14:39
No, the merged PR is not directly related to the original issue.
msg321472 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-07-11 15:03
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.
msg378332 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-10-09 15:02
For the original problem, how about this?

      def process_file(file_or_path):
          if isinstance(file_or_path, str):
              # If string, open file
              cm = open
              # Caller is responsible for closing file
              cm = nullcontext

          with cm(file_or_path) as file:
              # Perform processing on the file
Date User Action Args
2022-04-11 14:59:02adminsetgithub: 78248
2020-11-02 14:13:49iritkatrielsetstage: patch review -> needs patch
2020-11-02 14:13:24iritkatrielsetpull_requests: - pull_request21767
2020-10-20 09:52:24iritkatrielsetversions: + Python 3.9, Python 3.10, - Python 3.7
2020-10-20 09:50:56iritkatrielsetpull_requests: + pull_request21767
2020-10-11 12:26:12vstinnersetnosy: - vstinner
2020-10-09 15:02:32iritkatrielsetnosy: + iritkatriel
messages: + msg378332
2018-07-11 15:03:41ncoghlansetmessages: + msg321472
2018-07-11 14:39:42serhiy.storchakasetmessages: + msg321463
2018-07-11 14:37:48vstinnersetnosy: + vstinner
messages: + msg321461
2018-07-09 14:11:47miss-islingtonsetnosy: + miss-islington
messages: + msg321322
2018-07-09 13:50:41miss-islingtonsetpull_requests: + pull_request7752
2018-07-09 13:49:33ncoghlansetmessages: + msg321319
2018-07-09 13:23:56banoolsetkeywords: + patch
stage: patch review
pull_requests: + pull_request7751
2018-07-08 04:14:22ncoghlansetmessages: + msg321254
2018-07-08 00:45:16eric.smithsetnosy: + eric.smith
2018-07-07 17:33:25serhiy.storchakacreate