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