Index: Lib/test/test_inspect.py =================================================================== --- Lib/test/test_inspect.py (revision 71503) +++ Lib/test/test_inspect.py (working copy) @@ -3,8 +3,9 @@ import unittest import inspect import datetime +import os.path -from test.test_support import TESTFN, run_unittest +from test.test_support import TESTFN, run_unittest, unlink from test import inspect_fodder as mod from test import inspect_fodder2 as mod2 @@ -32,6 +33,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, @@ -514,9 +523,49 @@ 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 = os.path.dirname(mod.__file__) + source_name = os.path.join(testdir, 'source_reload_test.py') + source = open(source_name, 'w') + source.write(SOURCE_1) + source.close() + 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__":