summaryrefslogtreecommitdiffstats
path: root/Lib/unittest
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-06-11 06:04:06 (GMT)
committerGitHub <noreply@github.com>2024-06-11 06:04:06 (GMT)
commitaba5f2a4d10a7b261d4af03a54a8b1083bd83700 (patch)
tree9a0f5010d55c7493a3ec9f51d44847dbc1e22590 /Lib/unittest
parentffc8e21c80627c46be7a7467ee5427924fdeb885 (diff)
downloadcpython-aba5f2a4d10a7b261d4af03a54a8b1083bd83700.zip
cpython-aba5f2a4d10a7b261d4af03a54a8b1083bd83700.tar.gz
cpython-aba5f2a4d10a7b261d4af03a54a8b1083bd83700.tar.bz2
[3.13] gh-119600: mock: do not access attributes of original when new_callable is set (GH-119601) (#120334)
gh-119600: mock: do not access attributes of original when new_callable is set (GH-119601) In order to patch flask.g e.g. as in GH-84982, that proxies getattr must not be invoked. For that, mock must not try to read from the original object. In some cases that is unavoidable, e.g. when doing autospec. However, patch("flask.g", new_callable=MagicMock) should be entirely safe. (cherry picked from commit 422c4fc855afd18bcc6415902ea1d85a50cb7ce1) Co-authored-by: Robert Collins <robert.collins@cognite.com>
Diffstat (limited to 'Lib/unittest')
-rw-r--r--Lib/unittest/mock.py14
1 files changed, 9 insertions, 5 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index a2634b6..d0f2b8b 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -1508,13 +1508,12 @@ class _patch(object):
if isinstance(original, type):
# If we're patching out a class and there is a spec
inherit = True
- if spec is None and _is_async_obj(original):
- Klass = AsyncMock
- else:
- Klass = MagicMock
- _kwargs = {}
+
+ # Determine the Klass to use
if new_callable is not None:
Klass = new_callable
+ elif spec is None and _is_async_obj(original):
+ Klass = AsyncMock
elif spec is not None or spec_set is not None:
this_spec = spec
if spec_set is not None:
@@ -1527,7 +1526,12 @@ class _patch(object):
Klass = AsyncMock
elif not_callable:
Klass = NonCallableMagicMock
+ else:
+ Klass = MagicMock
+ else:
+ Klass = MagicMock
+ _kwargs = {}
if spec is not None:
_kwargs['spec'] = spec
if spec_set is not None: