MCPcopy Index your code
hub / github.com/python/cpython / test_failing_reload

Method test_failing_reload

Lib/test/test_import/__init__.py:581–617  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

579 import test.support.script_helper as x # noqa: F811
580
581 def test_failing_reload(self):
582 # A failing reload should leave the module object in sys.modules.
583 source = TESTFN + os.extsep + "py"
584 with open(source, "w", encoding='utf-8') as f:
585 f.write("a = 1\nb=2\n")
586
587 sys.path.insert(0, os.curdir)
588 try:
589 mod = __import__(TESTFN)
590 self.assertIn(TESTFN, sys.modules)
591 self.assertEqual(mod.a, 1, "module has wrong attribute values")
592 self.assertEqual(mod.b, 2, "module has wrong attribute values")
593
594 # On WinXP, just replacing the .py file wasn't enough to
595 # convince reload() to reparse it. Maybe the timestamp didn't
596 # move enough. We force it to get reparsed by removing the
597 # compiled file too.
598 remove_files(TESTFN)
599
600 # Now damage the module.
601 with open(source, "w", encoding='utf-8') as f:
602 f.write("a = 10\nb=20//0\n")
603
604 self.assertRaises(ZeroDivisionError, importlib.reload, mod)
605 # But we still expect the module to be in sys.modules.
606 mod = sys.modules.get(TESTFN)
607 self.assertIsNotNone(mod, "expected module to be in sys.modules")
608
609 # We should have replaced a w/ 10, but the old b value should
610 # stick.
611 self.assertEqual(mod.a, 10, "module has wrong attribute values")
612 self.assertEqual(mod.b, 2, "module has wrong attribute values")
613
614 finally:
615 del sys.path[0]
616 remove_files(TESTFN)
617 unload(TESTFN)
618
619 @skip_if_dont_write_bytecode
620 def test_file_to_source(self):

Callers

nothing calls this directly

Calls 11

unloadFunction · 0.90
__import__Function · 0.85
remove_filesFunction · 0.85
assertInMethod · 0.80
assertIsNotNoneMethod · 0.80
openFunction · 0.50
writeMethod · 0.45
insertMethod · 0.45
assertEqualMethod · 0.45
assertRaisesMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected