| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f6cdc6b commit 52ef443
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -137,7 +137,9 @@ def updatecache(filename, module_globals=None): | |||
| 137 | 137 | lines = fp.readlines() | |
| 138 | 138 | except (OSError, UnicodeDecodeError, SyntaxError): | |
| 139 | 139 | return [] | |
| 140 | - if lines and not lines[-1].endswith('\n'): | ||
| 140 | + if not lines: | ||
| 141 | + lines = ['\n'] | ||
| 142 | + elif not lines[-1].endswith('\n'): | ||
| 141 | 143 | lines[-1] += '\n' | |
| 142 | 144 | size, mtime = stat.st_size, stat.st_mtime | |
| 143 | 145 | cache[filename] = size, mtime, lines, fullname | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -35,7 +35,7 @@ | |||
| 35 | 35 | from test.support import cpython_only | |
| 36 | 36 | from test.support import MISSING_C_DOCSTRINGS, ALWAYS_EQ | |
| 37 | 37 | from test.support.import_helper import DirsOnSysPath, ready_to_import | |
| 38 | - from test.support.os_helper import TESTFN | ||
| 38 | + from test.support.os_helper import TESTFN, temp_cwd | ||
| 39 | 39 | from test.support.script_helper import assert_python_ok, assert_python_failure, kill_python | |
| 40 | 40 | from test.support import has_subprocess_support, SuppressCrashReport | |
| 41 | 41 | from test import support | |
@@ -730,6 +730,18 @@ def test_getsourcefile(self): | |||
| 730 | 730 | finally: | |
| 731 | 731 | del linecache.cache[co.co_filename] | |
| 732 | 732 | ||
| 733 | + def test_getsource_empty_file(self): | ||
| 734 | + with temp_cwd() as cwd: | ||
| 735 | + with open('empty_file.py', 'w'): | ||
| 736 | + pass | ||
| 737 | + sys.path.insert(0, cwd) | ||
| 738 | + try: | ||
| 739 | + import empty_file | ||
| 740 | + self.assertEqual(inspect.getsource(empty_file), '\n') | ||
| 741 | + self.assertEqual(inspect.getsourcelines(empty_file), (['\n'], 0)) | ||
| 742 | + finally: | ||
| 743 | + sys.path.remove(cwd) | ||
| 744 | + | ||
| 733 | 745 | def test_getfile(self): | |
| 734 | 746 | self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__) | |
| 735 | 747 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -83,6 +83,10 @@ def test_getlines(self): | |||
| 83 | 83 | class EmptyFile(GetLineTestsGoodData, unittest.TestCase): | |
| 84 | 84 | file_list = [] | |
| 85 | 85 | ||
| 86 | + def test_getlines(self): | ||
| 87 | + lines = linecache.getlines(self.file_name) | ||
| 88 | + self.assertEqual(lines, ['\n']) | ||
| 89 | + | ||
| 86 | 90 | ||
| 87 | 91 | class SingleEmptyLine(GetLineTestsGoodData, unittest.TestCase): | |
| 88 | 92 | file_list = ['\n'] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + :func:`inspect.getsource` (and related functions) work with | ||
| 2 | + empty module files, returning ``'\n'`` (or reasonable equivalent) | ||
| 3 | + instead of raising ``OSError``. Patch by Kernc. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments