Skip to content

gh-71765: Fix inspect.getsource() on empty file #20809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/linecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ def updatecache(filename, module_globals=None):
lines = fp.readlines()
except (OSError, UnicodeDecodeError, SyntaxError):
return []
if lines and not lines[-1].endswith('\n'):
if not lines:
lines = ['\n']
elif not lines[-1].endswith('\n'):
lines[-1] += '\n'
size, mtime = stat.st_size, stat.st_mtime
cache[filename] = size, mtime, lines, fullname
Expand Down
14 changes: 13 additions & 1 deletion Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from test.support import cpython_only
from test.support import MISSING_C_DOCSTRINGS, ALWAYS_EQ
from test.support.import_helper import DirsOnSysPath, ready_to_import
from test.support.os_helper import TESTFN
from test.support.os_helper import TESTFN, temp_cwd
from test.support.script_helper import assert_python_ok, assert_python_failure, kill_python
from test.support import has_subprocess_support, SuppressCrashReport
from test import support
Expand Down Expand Up @@ -730,6 +730,18 @@ def test_getsourcefile(self):
finally:
del linecache.cache[co.co_filename]

def test_getsource_empty_file(self):
with temp_cwd() as cwd:
with open('empty_file.py', 'w'):
pass
sys.path.insert(0, cwd)
try:
import empty_file
self.assertEqual(inspect.getsource(empty_file), '\n')
self.assertEqual(inspect.getsourcelines(empty_file), (['\n'], 0))
finally:
sys.path.remove(cwd)

def test_getfile(self):
self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__)

Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_linecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def test_getlines(self):
class EmptyFile(GetLineTestsGoodData, unittest.TestCase):
file_list = []

def test_getlines(self):
lines = linecache.getlines(self.file_name)
self.assertEqual(lines, ['\n'])


class SingleEmptyLine(GetLineTestsGoodData, unittest.TestCase):
file_list = ['\n']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:func:`inspect.getsource` (and related functions) work with
empty module files, returning ``'\n'`` (or reasonable equivalent)
instead of raising ``OSError``. Patch by Kernc.