Index: Lib/test/test_inspect.py =================================================================== --- Lib/test/test_inspect.py (revision 71888) +++ Lib/test/test_inspect.py (working copy) @@ -4,9 +4,9 @@ import inspect import datetime import collections -from os.path import normcase +from os.path import normcase, dirname, join -from test.support import TESTFN, run_unittest +from test.support import TESTFN, run_unittest, unlink from test import inspect_fodder as mod from test import inspect_fodder2 as mod2 @@ -41,6 +41,14 @@ git = mod.StupidGit() +SOURCE_1 = '''def foo(): + print("Bla") +''' + +SOURCE_2 = '''def foo(): + print("Oh no!") +''' + class IsTestBase(unittest.TestCase): predicates = set([inspect.isbuiltin, inspect.isclass, inspect.iscode, inspect.isframe, inspect.isfunction, inspect.ismethod, @@ -483,9 +491,50 @@ self.assert_(('m1', 'method', D) in attrs, 'missing plain method') self.assert_(('datablob', 'data', A) in attrs, 'missing data') + +class TestReload(unittest.TestCase): + def test_getsource_reload(self): + # Issue #1218234 + getsource = inspect.getsource + try: + # Create a source file and import it + testdir = dirname(mod.__file__) + source_name = join(testdir, 'source_reload_test.py') + source = open(source_name, 'w') + source.write(SOURCE_1) + source.close() + from . import source_reload_test + + # Keep a copy of the old contents + source_list = [] + source = open(source_name) + inspected_source = getsource(source_reload_test).splitlines(True) + for read, inspected in zip(source, inspected_source): + self.assertEquals(read, inspected) + source_list.append(read) + source.close() + + source = open(source_name, 'w') + source.write(SOURCE_2) + source.close() + + # Test that we get the new file contents + source = open(source_name) + inspected_source = getsource(source_reload_test).splitlines(True) + for read, inspected in zip(source, inspected_source): + self.assertEquals(read, inspected) + source.close() + + finally: + try: + source.close() + finally: + unlink(source_name) + + def test_main(): run_unittest(TestDecorators, TestRetrievingSourceCode, TestOneliners, - TestBuggyCases, + TestBuggyCases, TestReload, TestInterpreterStack, TestClassesAndFunctions, TestPredicates) if __name__ == "__main__":