mirror of
https://github.com/python/cpython.git
synced 2025-09-10 18:58:35 +00:00
bpo-45156: Fixes inifite loop on unittest.mock.seal() (GH-28300) (GH-28326)
Fixes infinite loop on unittest.mock.seal() of mocks created by
unittest.create_autospec().
Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
(cherry picked from commit 7f60c9e1c6
)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
parent
a390bb6d66
commit
fd74d2680e
3 changed files with 70 additions and 6 deletions
|
@ -1004,6 +1004,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
|
||||
|
@ -1022,12 +1027,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)
|
||||
|
||||
|
||||
|
@ -2927,6 +2926,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)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue