Fall back to sys.modules during mock imports (matching bpo-17636 behavior)

This commit is contained in:
Dino Viehland 2020-02-04 12:23:40 -08:00
parent 9538bc9185
commit 8fc702ab77
2 changed files with 17 additions and 1 deletions

View File

@ -1227,7 +1227,9 @@ def _dot_lookup(thing, comp, import_path):
return getattr(thing, comp)
except AttributeError:
__import__(import_path)
if hasattr(thing, comp):
return getattr(thing, comp)
return sys.modules[import_path]
def _importer(target):

View File

@ -1664,6 +1664,20 @@ class PatchTest(unittest.TestCase):
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