8000 Issue #119600: mock: do not access attributes of original when new_ca… · rbtcollins/cpython@d940c98 · GitHub
[go: up one dir, main page]

Skip to content

Commit d940c98

Browse files
committed
Issue python#119600: mock: do not access attributes of original when new_callable is set
In order to patch flask.g e.g. as in python#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.
1 parent c3b6dbf commit d940c98

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

Lib/unittest/mock.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,13 +1508,12 @@ def __enter__(self):
15081508
if isinstance(original, type):
15091509
# If we're patching out a class and there is a spec
15101510
inherit = True
1511-
if spec is None and _is_async_obj(original):
1512-
Klass = AsyncMock
1513-
else:
1514-
Klass = MagicMock
1515-
_kwargs = {}
1511+
1512+
# Determine the Klass to use
15161513
if new_callable is not None:
15171514
Klass = new_callable
1515+
elif spec is None and _is_async_obj(original):
1516+
Klass = AsyncMock
15181517
elif spec is not None or spec_set is not None:
15191518
this_spec = spec
15201519
if spec_set is not None:
@@ -1527,7 +1526,12 @@ def __enter__(self):
15271526
Klass = AsyncMock
15281527
elif not_callable:
15291528
Klass = NonCallableMagicMock
1529+
else:
1530+
Klass = MagicMock
1531+
else:
1532+
Klass = MagicMock
15301533

1534+
_kwargs = {}
15311535
if spec is not None:
15321536
_kwargs['spec'] = spec
15331537
if spec_set is not None:

0 commit comments

Comments
 (0)
0