ensure the last line has a trailing newline #8782
diff --git a/Lib/linecache.py b/Lib/linecache.py
index e7c33e1..c999851 100644
--- a/Lib/linecache.py
+++ b/Lib/linecache.py
@@ -133,6 +133,8 @@
     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 2dd3672d..88346be 100644
--- a/Lib/test/test_linecache.py
+++ b/Lib/test/test_linecache.py
@@ -31,6 +31,11 @@
 
 '''
 
+SOURCE_3 = '''
+def f():
+    return 3''' # No ending newline
+
+
 class LineCacheTests(unittest.TestCase):
 
     def test_getline(self):
@@ -63,6 +68,13 @@
         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:
diff --git a/Misc/NEWS b/Misc/NEWS
index 23b079c..768805b 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,9 @@
 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.