diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-06-30 09:46:27 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-30 09:46:27 (GMT) |
commit | ee937571e72a8515f87488bc146553b2dc35dfd9 (patch) | |
tree | d118247a57488ec97a8e7cf9bf058c3a99796781 | |
parent | 2bf974ec842cc0ffeb6aae7a97a3a4ad774e61ff (diff) | |
download | cpython-ee937571e72a8515f87488bc146553b2dc35dfd9.zip cpython-ee937571e72a8515f87488bc146553b2dc35dfd9.tar.gz cpython-ee937571e72a8515f87488bc146553b2dc35dfd9.tar.bz2 |
gh-92336: linecache.getline should not raise exceptions on decoding errors (GH-94410)
(cherry picked from commit 21cbdae90ffdac047d27d1b83a5442fabcf89f7c)
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
-rw-r--r-- | Lib/linecache.py | 2 | ||||
-rw-r--r-- | Lib/test/test_linecache.py | 12 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2022-06-29-09-48-37.gh-issue-92336.otA6c6.rst | 1 |
3 files changed, 8 insertions, 7 deletions
diff --git a/Lib/linecache.py b/Lib/linecache.py index 23191d6..97644a8 100644 --- a/Lib/linecache.py +++ b/Lib/linecache.py @@ -135,7 +135,7 @@ def updatecache(filename, module_globals=None): try: with tokenize.open(fullname) as fp: lines = fp.readlines() - except OSError: + except (OSError, UnicodeDecodeError, SyntaxError): return [] if lines and not lines[-1].endswith('\n'): lines[-1] += '\n' diff --git a/Lib/test/test_linecache.py b/Lib/test/test_linecache.py index c6e2dad..72dd401 100644 --- a/Lib/test/test_linecache.py +++ b/Lib/test/test_linecache.py @@ -73,12 +73,10 @@ class GetLineTestsBadData(TempFile): # file_byte_string = b'Bad data goes here' def test_getline(self): - self.assertRaises((SyntaxError, UnicodeDecodeError), - linecache.getline, self.file_name, 1) + self.assertEqual(linecache.getline(self.file_name, 1), '') def test_getlines(self): - self.assertRaises((SyntaxError, UnicodeDecodeError), - linecache.getlines, self.file_name) + self.assertEqual(linecache.getlines(self.file_name), []) class EmptyFile(GetLineTestsGoodData, unittest.TestCase): @@ -92,9 +90,11 @@ class SingleEmptyLine(GetLineTestsGoodData, unittest.TestCase): class GoodUnicode(GetLineTestsGoodData, unittest.TestCase): file_list = ['á\n', 'b\n', 'abcdef\n', 'ááááá\n'] +class BadUnicode_NoDeclaration(GetLineTestsBadData, unittest.TestCase): + file_byte_string = b'\n\x80abc' -class BadUnicode(GetLineTestsBadData, unittest.TestCase): - file_byte_string = b'\x80abc' +class BadUnicode_WithDeclaration(GetLineTestsBadData, unittest.TestCase): + file_byte_string = b'# coding=utf-8\n\x80abc' class LineCacheTests(unittest.TestCase): diff --git a/Misc/NEWS.d/next/Library/2022-06-29-09-48-37.gh-issue-92336.otA6c6.rst b/Misc/NEWS.d/next/Library/2022-06-29-09-48-37.gh-issue-92336.otA6c6.rst new file mode 100644 index 0000000..eb74e0c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-06-29-09-48-37.gh-issue-92336.otA6c6.rst @@ -0,0 +1 @@ +Fix bug where :meth:`linecache.getline` fails on bad files with :exc:`UnicodeDecodeError` or :exc:`SyntaxError`. It now returns an empty string as per the documentation. |