Skip to content

[3.14] gh-150484: Fix mock_open __exit__ with contextlib.ExitStack - #151829

Merged
serhiy-storchaka merged 2 commits into
python:3.14from
factnn:fix/mock-open-exit-signature-3.14
Jun 21, 2026
Merged

serhiy-storchaka merged 2 commits into
python:3.14from
factnn:fix/mock-open-exit-signature-3.14

Conversation

@factnn

@factnn factnn commented Jun 21, 2026 •

Copy link
Copy Markdown

Fixes #150484.

mock_open()'s __exit__ raises TypeError when used with contextlib.ExitStack because _exit_side_effect had a fixed 3-arg signature (exctype, excinst, exctb), but ExitStack.__exit__ calls it with 4 args (self, exctype, excinst, exctb). Since the function body only calls handle.close() and doesn't use the arguments, the fix changes the signature to *args.

This is the 3.14 backport of #150521 (opened per request).

factnn added 2 commits June 21, 2026 12:43
  mock_open's _exit_side_effect had a fixed 3-arg signature, but
  contextlib.ExitStack calls __exit__ with 4 args (self + 3 exc info).
  Use *args to accept any number of arguments.

  Fixes python#150484
@factnn
factnn requested a review from cjw296 as a code owner June 21, 2026 04:46
@picnixz picnixz changed the title gh-150484: Fix mock_open __exit__ with contextlib.ExitStack Jun 21, 2026
@picnixz
picnixz requested a review from serhiy-storchaka June 21, 2026 12:42
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.13 only security fixes label Jun 21, 2026
@serhiy-storchaka
serhiy-storchaka merged commit 85fa295 into python:3.14 Jun 21, 2026
50 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @factnn for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Jun 21, 2026

Copy link
Copy Markdown

GH-151861 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 only security fixes label Jun 21, 2026
@serhiy-storchaka

Copy link
Copy Markdown
Member

Thank you @factnn.

@factnn

factnn commented Jun 21, 2026

Copy link
Copy Markdown
Author

My pleasure.

serhiy-storchaka pushed a commit that referenced this pull request Jun 21, 2026
…H-151829) (GH-151861)

  mock_open's _exit_side_effect had a fixed 3-arg signature, but
  contextlib.ExitStack calls __exit__ with 4 args (self + 3 exc info).
  Use *args to accept any number of arguments.
(cherry picked from commit 85fa295)

Co-authored-by: Zang Peiyu <166481866+factnn@users.noreply.github.com>
factnn added a commit to factnn/cpython that referenced this pull request Oct 21, 2026
…Stack

mock_open().__exit__ only misbehaves on 3.13 and 3.14, where it was
fixed directly by pythonGH-151829 and backported as pythonGH-151861. Only the
regression test belongs on main.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants