Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,9 @@ def _dot_lookup(thing, comp, import_path):
return getattr(thing, comp)
except AttributeError:
__import__(import_path)
return getattr(thing, comp)
if hasattr(thing, comp):
return getattr(thing, comp)
return sys.modules[import_path]


def _importer(target):
Expand Down
14 changes: 14 additions & 0 deletions Lib/unittest/test/testmock/testpatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,20 @@ def test_patch_imports_lazily(self):
p1.stop()
self.assertEqual(squizz.squozz, 3)

def test_patch_from_sys_modules(self):
squizz = type(sys)('squizz')
squizz_squozz = type(sys)('squizz.squozz')
squizz_squozz.x = 42

with uncache('squizz'), uncache('squizz.squozz'):
sys.modules['squizz'] = squizz
sys.modules['squizz.squozz'] = squizz_squozz
p1 = patch('squizz.squozz.x', 100)
p1.start()
self.assertEqual(squizz_squozz.x, 100)
p1.stop()
self.assertEqual(squizz_squozz.x, 42)

def test_patch_propagates_exc_on_exit(self):
class holder:
exc_info = None, None, None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock.patch will now fallback to sys.modules if a child package isn't published on its parent