diff options
| author | Christian Heimes <christian@cheimes.de> | 2008-01-07 17:13:09 (GMT) | 
|---|---|---|
| committer | Christian Heimes <christian@cheimes.de> | 2008-01-07 17:13:09 (GMT) | 
| commit | 13a7a21258f0cd241c2cf1367a954d6742daa2a6 (patch) | |
| tree | 4c49495d5dfb4b591d2a93e9f2567b4f2ba70f49 /Lib/test/test_import.py | |
| parent | 8de8e03849ecbb08da6deb4679d08c0206b71316 (diff) | |
| download | cpython-13a7a21258f0cd241c2cf1367a954d6742daa2a6.zip cpython-13a7a21258f0cd241c2cf1367a954d6742daa2a6.tar.gz cpython-13a7a21258f0cd241c2cf1367a954d6742daa2a6.tar.bz2  | |
Issue #1762972: Readded the reload() function as imp.reload()
Diffstat (limited to 'Lib/test/test_import.py')
| -rw-r--r-- | Lib/test/test_import.py | 40 | 
1 files changed, 40 insertions, 0 deletions
diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py index cb4059a..6c9e0b0 100644 --- a/Lib/test/test_import.py +++ b/Lib/test/test_import.py @@ -7,6 +7,7 @@ import shutil  import sys  import py_compile  import warnings +import imp  from test.test_support import unlink, TESTFN, unload @@ -160,6 +161,45 @@ class ImportTest(unittest.TestCase):              warnings.simplefilter('error', ImportWarning)              self.assertRaises(ImportWarning, __import__, "site-packages") +    def test_failing_reload(self): +        # A failing reload should leave the module object in sys.modules. +        source = TESTFN + ".py" +        with open(source, "w") as f: +            f.write("a = 1\nb=2\n") + +        sys.path.insert(0, os.curdir) +        try: +            mod = __import__(TESTFN) +            self.assert_(TESTFN in sys.modules, "expected module in sys.modules") +            self.assertEquals(mod.a, 1, "module has wrong attribute values") +            self.assertEquals(mod.b, 2, "module has wrong attribute values") + +            # On WinXP, just replacing the .py file wasn't enough to +            # convince reload() to reparse it.  Maybe the timestamp didn't +            # move enough.  We force it to get reparsed by removing the +            # compiled file too. +            remove_files(TESTFN) + +            # Now damage the module. +            with open(source, "w") as f: +                f.write("a = 10\nb=20//0\n") + +            self.assertRaises(ZeroDivisionError, imp.reload, mod) +            # But we still expect the module to be in sys.modules. +            mod = sys.modules.get(TESTFN) +            self.failIf(mod is None, "expected module to still be in sys.modules") + +            # We should have replaced a w/ 10, but the old b value should +            # stick. +            self.assertEquals(mod.a, 10, "module has wrong attribute values") +            self.assertEquals(mod.b, 2, "module has wrong attribute values") + +        finally: +            sys.path.pop(0) +            remove_files(TESTFN) +            if TESTFN in sys.modules: +                del sys.modules[TESTFN] +  class PathsTests(unittest.TestCase):      SAMPLES = ('test', 'test\u00e4\u00f6\u00fc\u00df', 'test\u00e9\u00e8',                 'test\u00b0\u00b3\u00b2')  | 
