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:
- changing the way
ExitStack calls its stack of __exit__ callbacks
- making
mock_open and open more similar, or
- 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
Bug report
Bug description:
In Python 3.13 onwards:
raises
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 withcontextlib.ExitStackbut that used to be a no-op before the exit handler was added.I'm not sure whether this should be fixed by:
ExitStackcalls its stack of__exit__callbacksmock_openandopenmore similar, ormock_open's exit handler take*args, **kwargssame as the enter handler??Thanks
CPython versions tested on:
3.12, 3.13, 3.14
Operating systems tested on:
Linux
Linked PRs