Fall back to sys.modules during mock imports (matching bpo-17636 behavior)
This commit is contained in:
parent
9538bc9185
commit
8fc702ab77
|
@ -1227,7 +1227,9 @@ def _dot_lookup(thing, comp, import_path):
|
||||||
return getattr(thing, comp)
|
return getattr(thing, comp)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
__import__(import_path)
|
__import__(import_path)
|
||||||
return getattr(thing, comp)
|
if hasattr(thing, comp):
|
||||||
|
return getattr(thing, comp)
|
||||||
|
return sys.modules[import_path]
|
||||||
|
|
||||||
|
|
||||||
def _importer(target):
|
def _importer(target):
|
||||||
|
|
|
@ -1664,6 +1664,20 @@ class PatchTest(unittest.TestCase):
|
||||||
p1.stop()
|
p1.stop()
|
||||||
self.assertEqual(squizz.squozz, 3)
|
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):
|
def test_patch_propagates_exc_on_exit(self):
|
||||||
class holder:
|
class holder:
|
||||||
exc_info = None, None, None
|
exc_info = None, None, None
|
||||||
|
|
Loading…
Reference in New Issue