From 11f0b41e9de3805441ddd4142df9f6b7f4432ca7 Mon Sep 17 00:00:00 2001 From: Florent Xicluna Date: Sat, 7 Jul 2012 12:13:35 +0200 Subject: Issue #14990: tokenize: correctly fail with SyntaxError on invalid encoding declaration. --- Lib/test/test_tokenize.py | 4 ++++ Lib/tokenize.py | 2 +- Misc/NEWS | 3 +++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_tokenize.py b/Lib/test/test_tokenize.py index 63d084d..b6a9ca1 100644 --- a/Lib/test/test_tokenize.py +++ b/Lib/test/test_tokenize.py @@ -674,6 +674,10 @@ class TestTokenizerAdheresToPep0263(TestCase): f = 'tokenize_tests-utf8-coding-cookie-and-utf8-bom-sig.txt' self.assertTrue(self._testFile(f)) + def test_bad_coding_cookie(self): + self.assertRaises(SyntaxError, self._testFile, 'bad_coding.py') + self.assertRaises(SyntaxError, self._testFile, 'bad_coding2.py') + class Test_Tokenize(TestCase): diff --git a/Lib/tokenize.py b/Lib/tokenize.py index f283c6d..59081d3 100644 --- a/Lib/tokenize.py +++ b/Lib/tokenize.py @@ -310,7 +310,7 @@ def detect_encoding(readline): raise SyntaxError("unknown encoding: " + encoding) if bom_found: - if codec.name != 'utf-8': + if encoding != 'utf-8': # This behaviour mimics the Python interpreter raise SyntaxError('encoding problem: utf-8') encoding += '-sig' diff --git a/Misc/NEWS b/Misc/NEWS index 699b4fb..92131b4 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -87,6 +87,9 @@ Core and Builtins Library ------- +- Issue #14990: Correctly fail with SyntaxError on invalid encoding + declaration. + - Issue #15247: FileIO now raises an error when given a file descriptor pointing to a directory. -- cgit v0.12