summaryrefslogtreecommitdiffstats
path: root/Lib/unittest/mock.py
diff options
context:
space:
mode:
authorNikita Sobolev <mail@sobolevn.me>2021-09-14 10:20:40 (GMT)
committerGitHub <noreply@github.com>2021-09-14 10:20:40 (GMT)
commit7f60c9e1c6e22cc0e846a872c318570926cd3094 (patch)
treec1b19fca9b51e98473e63e0bf43767086011c283 /Lib/unittest/mock.py
parentc99fc4e53a60084df88ac5c69b3b13bc033677e1 (diff)
downloadcpython-7f60c9e1c6e22cc0e846a872c318570926cd3094.zip
cpython-7f60c9e1c6e22cc0e846a872c318570926cd3094.tar.gz
cpython-7f60c9e1c6e22cc0e846a872c318570926cd3094.tar.bz2
bpo-45156: Fixes inifite loop on unittest.mock.seal() (GH-28300)
Fixes infinite loop on unittest.mock.seal() of mocks created by unittest.create_autospec(). Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
Diffstat (limited to 'Lib/unittest/mock.py')
-rw-r--r--Lib/unittest/mock.py13
1 files changed, 7 insertions, 6 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 8193efc..9f99a5a 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -1005,6 +1005,11 @@ class NonCallableMock(Base):
if _new_name in self.__dict__['_spec_asyncs']:
return AsyncMock(**kw)
+ if self._mock_sealed:
+ attribute = f".{kw['name']}" if "name" in kw else "()"
+ mock_name = self._extract_mock_name() + attribute
+ raise AttributeError(mock_name)
+
_type = type(self)
if issubclass(_type, MagicMock) and _new_name in _async_method_magics:
# Any asynchronous magic becomes an AsyncMock
@@ -1023,12 +1028,6 @@ class NonCallableMock(Base):
klass = Mock
else:
klass = _type.__mro__[1]
-
- if self._mock_sealed:
- attribute = "." + kw["name"] if "name" in kw else "()"
- mock_name = self._extract_mock_name() + attribute
- raise AttributeError(mock_name)
-
return klass(**kw)
@@ -2913,6 +2912,8 @@ def seal(mock):
continue
if not isinstance(m, NonCallableMock):
continue
+ if isinstance(m._mock_children.get(attr), _SpecState):
+ continue
if m._mock_new_parent is mock:
seal(m)