]> granicus.if.org Git - python/commitdiff
ensure the last line has a trailing newline #8782
authorBenjamin Peterson <benjamin@python.org>
Fri, 21 May 2010 21:31:24 +0000 (21:31 +0000)
committerBenjamin Peterson <benjamin@python.org>
Fri, 21 May 2010 21:31:24 +0000 (21:31 +0000)
Lib/linecache.py
Lib/test/test_linecache.py
Misc/NEWS

index e7c33e1464fb918dd72f182eb96fb33e65affc18..c9998515b2c3259aab3d609be02fecda957b8f33 100644 (file)
@@ -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
index 2dd3672dad785a3e9d2ae1753c61d770afaf889e..88346be0deda3c2518a73217910b9cb7155a53ce 100644 (file)
@@ -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,13 @@ class LineCacheTests(unittest.TestCase):
         empty = linecache.getlines('a/b/c/__init__.py')
         self.assertEquals(empty, [])
 
+    def test_no_ending_newline(self):
+        self.addCleanup(support.unlink, support.TESTFN)
+        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"])
+
     def test_clearcache(self):
         cached = []
         for entry in TESTS:
index 23b079c93f303281dda05ced449f55828e9b30fe..768805b635894625a470b27292bd9b1ce2b8ac27 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,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.