FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

mock.mock_open __exit__ called as instance method from contextlib.ExitStack context · Issue #150484 · python/cpython · GitHub

Repository navigation

mock.mock_open __exit__ called as instance method from contextlib.ExitStack context #150484

Description

Bug report

Bug description:

In Python 3.13 onwards:

from contextlib import ExitStack
from unittest import mock
with mock.patch("__main__.open", mock.mock_open()) as m:
    with ExitStack() as exit_stack:
        with exit_stack.enter_context(open("/tmp/test.txt", "w")):
            pass

raises

TypeError: mock_open.<locals>._exit_side_effect() takes 3 positional arguments but 4 were given

The issue seems to come from 3f7c081

It appears that in Python versions 3.12 and earlier, mock_open().__exit__ was always called with 4 args (self, exctype, excinst, exctb) when used with contextlib.ExitStack but that used to be a no-op before the exit handler was added.

I'm not sure whether this should be fixed by:

  1. changing the way ExitStack calls its stack of __exit__ callbacks
  2. making mock_open and open more similar, or
  3. making mock_open's exit handler take *args, **kwargs same as the enter handler??

Thanks

CPython versions tested on:

3.12, 3.13, 3.14

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on May 26, 2026
  2. added a commit that references this issue on May 27, 2026
  3. added a commit that references this issue on May 28, 2026
  4. serhiy-storchaka commented on May 29, 2026

    Member

    This was fixed in 3.15 (most likely in #144386), so only 3.13 and 3.14 need a fix (too late for 3.12, security bugs only).

    Yes, using *args is a correct fix for these versions.

  5. clarker0 commented on Jun 1, 2026

    Author

    3.12 is working fine! sorry for the confusion, I added it to the "tested on" versions list in order to bisect where the issue came from.

    only 3.13 and 3.14 are affected by this issue.

    a fix for 3.14 would be hugely appreciated, thanks 😄

  6. added 2 commits that reference this issue on Jun 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixes3.14bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL