FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-92336: linecache.getline should not raise exceptions on decoding errors by iritkatriel · Pull Request #94410 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion Lib/linecache.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Comment thread
gpshead marked this conversation as resolved.
return []
if lines and not lines[-1].endswith('\n'):
lines[-1] += '\n'
Expand Down
12 changes: 6 additions & 6 deletions Lib/test/test_linecache.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -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.

Back | FazBrowse Home | New Git URL