8000 bpo-45156: fixes inifite loop on `mock.seal()` by sobolevn · Pull Request #28300 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-45156: fixes inifite loop on mock.seal() #28300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bpo-45156: fixes inifite loop on mock.seal()
  • Loading branch information
sobolevn committed Sep 12, 2021
commit 70f77aa9c7d5a27b3eb0ddf3a2369dc34f340a05
13 changes: 7 additions & 6 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,11 @@ def _get_child_mock(self, /, **kw):
if _new_name in self.__dict__['_spec_asyncs']:
return AsyncMock(**kw)

if self._mock_sealed:
attribute = "." + 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
Expand All @@ -1023,12 +1028,6 @@ def _get_child_mock(self, /, **kw):
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)


Expand Down Expand Up @@ -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)

Expand Down
63 changes: 63 additions & 0 deletions Lib/unittest/test/testmock/testsealable.py
D5F4
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,69 @@ def test_call_chain_is_maintained(self):
m.test1().test2.test3().test4()
self.assertIn("mock.test1().test2.test3().test4", str(cm.exception))

def test_seal_with_attr_autospec(self):
# https://bugs.python.org/issue45156
class Foo:
foo = 0

for spec_set in (True, False):
with self.subTest(spec_set=spec_set):
foo = mock.create_autospec(Foo, spec_set=spec_set)
mock.seal(foo)

self.assertIsInstance(foo.foo, mock.NonCallableMagicMock)
with self.assertRaises(TypeError):
foo.foo()
with self.assertRaises(AttributeError):
foo.bar = 1
with self.assertRaises(AttributeError):
foo.missing_attr
with self.assertRaises(AttributeError):
foo.missing_method()

def test_seal_with_method_autospec(self):
# https://bugs.python.org/issue45156
class Foo:
def foo(self):
return 1

for spec_set in (True, False):
with self.subTest(spec_set=spec_set):
foo = mock.create_autospec(Foo, spec_set=spec_set)
foo.foo.return_value = 0
mock.seal(foo)

self.assertIsInstance(foo.foo, mock.MagicMock)
self.assertEqual(foo.foo(), 0)
with self.assertRaises(AttributeError):
foo.bar = 1
with self.assertRaises(AttributeError):
foo.missing_attr
with self.assertRaises(AttributeError):
foo.missing_method()

def test_seal_with_nested_class_autospec(self):
# https://bugs.python.org/issue45156
class Foo:
class Baz:
def baz(self):
return 1

for spec_set in (True, False):
with self.subTest(spec_set=spec_set):
foo = mock.create_autospec(Foo, spec_set=spec_set)
foo.Baz.baz.return_value = 0
mock.seal(foo)

self.assertIsInstance(foo.Baz.baz, mock.MagicMock)
self.assertEqual(foo.Baz.baz(), 0)
with self.assertRaises(AttributeError):
foo.bar = 1
with self.assertRaises(AttributeError):
foo.Baz.bar = 1
with self.assertRaises(AttributeError):
foo.Baz.missing_method()


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes infinite loop on ``unittest.seal()`` of mocks created by
``mock.create_autospec()``.
0