GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-45156: fixes inifite loop on `mock.seal()` by sobolevn · Pull Request #28300 · python/cpython

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.

Sign up for GitHub

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
vstinner merged 7 commits into python:main from sobolevn:issue-45156
Sep 14, 2021

Uh oh!

There was an error while loading. Please reload this page.

Merged

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

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
7 commits
Select commit Hold shift + click to select a range
70f77aa
bpo-45156: fixes inifite loop on `mock.seal()`
sobolevn Sep 12, 2021
8d7824b
Simplifies test case
sobolevn Sep 12, 2021
3c87eea
Update Lib/unittest/mock.py
sobolevn Sep 13, 2021
1b70cd4
Update Misc/NEWS.d/next/Tests/2021-09-13-00-28-17.bpo-45156.8oomV3.rst
sobolevn Sep 13, 2021
8238aec
Update Misc/NEWS.d/next/Tests/2021-09-13-00-28-17.bpo-45156.8oomV3.rst
sobolevn Sep 13, 2021
9fb0b33
Fixes typo
sobolevn Sep 13, 2021
b156248
Fixes typo
sobolevn Sep 13, 2021
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

13 changes: 7 additions & 6 deletions 13 Lib/unittest/mock.py
Show comments
View file
Open in desktop
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 = 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
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
61 changes: 61 additions & 0 deletions 61 Lib/unittest/test/testmock/testsealable.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,67 @@ 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_autospec(self):
# https://bugs.python.org/issue45156
class Foo:
foo = 0
def bar1(self):
return 1
def bar2(self):
return 2

class Baz:
baz = 3
def ban(self):
return 4

for spec_set in (True, False):
with self.subTest(spec_set=spec_set):
foo = mock.create_autospec(Foo, spec_set=spec_set)
foo.bar1.return_value = 'a'
foo.Baz.ban.return_value = 'b'

mock.seal(foo)

self.assertIsInstance(foo.foo, mock.NonCallableMagicMock)
self.assertIsInstance(foo.bar1, mock.MagicMock)
self.assertIsInstance(foo.bar2, mock.MagicMock)
self.assertIsInstance(foo.Baz, mock.MagicMock)
self.assertIsInstance(foo.Baz.baz, mock.NonCallableMagicMock)
self.assertIsInstance(foo.Baz.ban, mock.MagicMock)

self.assertEqual(foo.bar1(), 'a')
foo.bar1.return_value = 'new_a'
self.assertEqual(foo.bar1(), 'new_a')
self.assertEqual(foo.Baz.ban(), 'b')
foo.Baz.ban.return_value = 'new_b'
self.assertEqual(foo.Baz.ban(), 'new_b')

with self.assertRaises(TypeError):
foo.foo()
with self.assertRaises(AttributeError):
foo.bar = 1
with self.assertRaises(AttributeError):
foo.bar2()

foo.bar2.return_value = 'bar2'
self.assertEqual(foo.bar2(), 'bar2')

with self.assertRaises(AttributeError):
foo.missing_attr
with self.assertRaises(AttributeError):
foo.missing_attr = 1
with self.assertRaises(AttributeError):
foo.missing_method()
with self.assertRaises(TypeError):
foo.Baz.baz()
with self.assertRaises(AttributeError):
foo.Baz.missing_attr
with self.assertRaises(AttributeError):
foo.Baz.missing_attr = 1
with self.assertRaises(AttributeError):
foo.Baz.missing_method()


if __name__ == "__main__":
unittest.main()
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Tests/2021-09-13-00-28-17.bpo-45156.8oomV3.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes infinite loop on :func:`unittest.mock.seal` of mocks created by
:func:`~unittest.create_autospec`.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.