I'm trying to determine if Python's mock.patch
(unittest.mock.patch
in Py3) context manager mutates global state, i.e., if it is thread-safe.
For instance: let's imagine one thread patches function bar
within function foo
with a context manager, and then inside the context manager the interpreter pauses that thread (because of the GIL etc.) and resumes another thread, which runs foo
outside of said context manager. If patch
is thread-safe I would expect that the global state of the functions foo
and bar
are unmodified, and so the second thread will get the normal behavior of foo
. But if patch
modifies global state, the second thread will get the modified behavior of foo
even though it's not inside the context manager.
I referred to the source code but wasn't able to clearly tell just by looking at it.