summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_tokenize.py
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-04-20 17:23:54 (GMT)
committerBrett Cannon <brett@python.org>2012-04-20 17:23:54 (GMT)
commitc33f3f2339fd3217a0c6fe3df916616abab2fab4 (patch)
tree45b2793ea426faee0669f4a6d34387cd236aff06 /Lib/test/test_tokenize.py
parentdd9a56953e561076b5573d53f6e4fdd7f42b208c (diff)
downloadcpython-c33f3f2339fd3217a0c6fe3df916616abab2fab4.zip
cpython-c33f3f2339fd3217a0c6fe3df916616abab2fab4.tar.gz
cpython-c33f3f2339fd3217a0c6fe3df916616abab2fab4.tar.bz2
Issue #14629: Mention the filename in SyntaxError exceptions from
tokenizer.detect_encoding() (when available).
Diffstat (limited to 'Lib/test/test_tokenize.py')
-rw-r--r--Lib/test/test_tokenize.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/Lib/test/test_tokenize.py b/Lib/test/test_tokenize.py
index 11590ea..915eda9 100644
--- a/Lib/test/test_tokenize.py
+++ b/Lib/test/test_tokenize.py
@@ -904,6 +904,35 @@ class TestDetectEncoding(TestCase):
self.assertEqual(fp.encoding, 'utf-8-sig')
self.assertEqual(fp.mode, 'r')
+ def test_filename_in_exception(self):
+ # When possible, include the file name in the exception.
+ path = 'some_file_path'
+ lines = (
+ b'print("\xdf")', # Latin-1: LATIN SMALL LETTER SHARP S
+ )
+ class Bunk:
+ def __init__(self, lines, path):
+ self.name = path
+ self._lines = lines
+ self._index = 0
+
+ def readline(self):
+ if self._index == len(lines):
+ raise StopIteration
+ line = lines[self._index]
+ self._index += 1
+ return line
+
+ with self.assertRaises(SyntaxError):
+ ins = Bunk(lines, path)
+ # Make sure lacking a name isn't an issue.
+ del ins.name
+ detect_encoding(ins.readline)
+ with self.assertRaisesRegex(SyntaxError, '.*{}'.format(path)):
+ ins = Bunk(lines, path)
+ detect_encoding(ins.readline)
+
+
class TestTokenize(TestCase):
def test_tokenize(self):