From 29dc0129f1bb62c9a8f5c82d600f80ad4b489900 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Wed, 29 Jun 2022 10:41:33 +0100 Subject: [PATCH 1/3] bpo-92336: linecache.getline should not raise exceptions on decoding errors --- Lib/linecache.py | 2 +- Lib/test/test_linecache.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Lib/linecache.py b/Lib/linecache.py index 23191d6501d2a8..97644a8e3794e1 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 c6e2dadbb25e1f..67664845f1c353 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, 1), []) 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): From f86376667dc1c841a3331c87a6a729eb462ef6b6 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 29 Jun 2022 09:48:39 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-06-29-09-48-37.gh-issue-92336.otA6c6.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-06-29-09-48-37.gh-issue-92336.otA6c6.rst 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 00000000000000..eb74e0ceb74466 --- /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. From 7e5e28bb362b20423f3a91b8e68a697382a00f7b Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Wed, 29 Jun 2022 23:01:02 +0100 Subject: [PATCH 3/3] fix typo in test --- Lib/test/test_linecache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_linecache.py b/Lib/test/test_linecache.py index 67664845f1c353..72dd40136cfdb2 100644 --- a/Lib/test/test_linecache.py +++ b/Lib/test/test_linecache.py @@ -76,7 +76,7 @@ def test_getline(self): self.assertEqual(linecache.getline(self.file_name, 1), '') def test_getlines(self): - self.assertEqual(linecache.getlines(self.file_name, 1), []) + self.assertEqual(linecache.getlines(self.file_name), []) class EmptyFile(GetLineTestsGoodData, unittest.TestCase):