From a3401654e15da7c0ac981b5c5f69a64aa05694b0 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Fri, 21 May 2010 21:45:16 +0000 Subject: [PATCH] Merged revisions 81432 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r81432 | benjamin.peterson | 2010-05-21 16:31:24 -0500 (Fri, 21 May 2010) | 1 line ensure the last line has a trailing newline #8782 ........ --- Lib/linecache.py | 2 ++ Lib/test/test_linecache.py | 14 ++++++++++++++ Misc/NEWS | 3 +++ 3 files changed, 19 insertions(+) diff --git a/Lib/linecache.py b/Lib/linecache.py index e7c33e1464..c9998515b2 100644 --- a/Lib/linecache.py +++ b/Lib/linecache.py @@ -133,6 +133,8 @@ def updatecache(filename, module_globals=None): except IOError, msg: ## print '*** Cannot open', fullname, ':', msg return [] + if lines and not lines[-1].endswith('\n'): + lines[-1] += '\n' size, mtime = stat.st_size, stat.st_mtime cache[filename] = size, mtime, lines, fullname return lines diff --git a/Lib/test/test_linecache.py b/Lib/test/test_linecache.py index 18206d2be5..945d7689cf 100644 --- a/Lib/test/test_linecache.py +++ b/Lib/test/test_linecache.py @@ -31,6 +31,11 @@ a = f() ''' +SOURCE_3 = ''' +def f(): + return 3''' # No ending newline + + class LineCacheTests(unittest.TestCase): def test_getline(self): @@ -63,6 +68,15 @@ class LineCacheTests(unittest.TestCase): empty = linecache.getlines('a/b/c/__init__.py') self.assertEquals(empty, []) + def test_no_ending_newline(self): + try: + with open(support.TESTFN, "w") as fp: + fp.write(SOURCE_3) + lines = linecache.getlines(support.TESTFN) + self.assertEqual(lines, ["\n", "def f():\n", " return 3\n"]) + finally: + support.unlink(support.TESTFN) + def test_clearcache(self): cached = [] for entry in TESTS: diff --git a/Misc/NEWS b/Misc/NEWS index 2fc7890de4..6b97a3dec3 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -55,6 +55,9 @@ C-API Library ------- +- Issue #8782: Add a trailing newline in linecache.updatecache to the last line + of files without one. + - Issue #8729: Return NotImplemented from collections.Mapping.__eq__ when comparing to a non-mapping. -- 2.50.0