mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
gh-92336: linecache.getline should not raise exceptions on decoding errors (GH-94410)
This commit is contained in:
parent
68fb03249f
commit
21cbdae90f
3 changed files with 8 additions and 7 deletions
|
@ -135,7 +135,7 @@ def updatecache(filename, module_globals=None):
|
||||||
try:
|
try:
|
||||||
with tokenize.open(fullname) as fp:
|
with tokenize.open(fullname) as fp:
|
||||||
lines = fp.readlines()
|
lines = fp.readlines()
|
||||||
except OSError:
|
except (OSError, UnicodeDecodeError, SyntaxError):
|
||||||
return []
|
return []
|
||||||
if lines and not lines[-1].endswith('\n'):
|
if lines and not lines[-1].endswith('\n'):
|
||||||
lines[-1] += '\n'
|
lines[-1] += '\n'
|
||||||
|
|
|
@ -73,12 +73,10 @@ class GetLineTestsBadData(TempFile):
|
||||||
# file_byte_string = b'Bad data goes here'
|
# file_byte_string = b'Bad data goes here'
|
||||||
|
|
||||||
def test_getline(self):
|
def test_getline(self):
|
||||||
self.assertRaises((SyntaxError, UnicodeDecodeError),
|
self.assertEqual(linecache.getline(self.file_name, 1), '')
|
||||||
linecache.getline, self.file_name, 1)
|
|
||||||
|
|
||||||
def test_getlines(self):
|
def test_getlines(self):
|
||||||
self.assertRaises((SyntaxError, UnicodeDecodeError),
|
self.assertEqual(linecache.getlines(self.file_name), [])
|
||||||
linecache.getlines, self.file_name)
|
|
||||||
|
|
||||||
|
|
||||||
class EmptyFile(GetLineTestsGoodData, unittest.TestCase):
|
class EmptyFile(GetLineTestsGoodData, unittest.TestCase):
|
||||||
|
@ -92,9 +90,11 @@ class SingleEmptyLine(GetLineTestsGoodData, unittest.TestCase):
|
||||||
class GoodUnicode(GetLineTestsGoodData, unittest.TestCase):
|
class GoodUnicode(GetLineTestsGoodData, unittest.TestCase):
|
||||||
file_list = ['á\n', 'b\n', 'abcdef\n', 'ááááá\n']
|
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):
|
class BadUnicode_WithDeclaration(GetLineTestsBadData, unittest.TestCase):
|
||||||
file_byte_string = b'\x80abc'
|
file_byte_string = b'# coding=utf-8\n\x80abc'
|
||||||
|
|
||||||
|
|
||||||
class LineCacheTests(unittest.TestCase):
|
class LineCacheTests(unittest.TestCase):
|
||||||
|
|
|
@ -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.
|
Loading…
Add table
Add a link
Reference in a new issue