summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-06-09 13:53:55 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-06-09 13:53:55 (GMT)
commit9670543a00beb429799986b4f8b09632d624f859 (patch)
tree16f79b521f1f402be315ead5e5e8d214198feb1a
parent7dc4c03344e7d356f5f6080258f03b7d6db6aa96 (diff)
parent3af14aaba5e5df6e1bbe67f037bef1b1789209ca (diff)
downloadcpython-9670543a00beb429799986b4f8b09632d624f859.zip
cpython-9670543a00beb429799986b4f8b09632d624f859.tar.gz
cpython-9670543a00beb429799986b4f8b09632d624f859.tar.bz2
Issue #18038: SyntaxError raised during compilation sources with illegal
encoding now always contains an encoding name.
-rw-r--r--Lib/test/test_pep263.py18
-rw-r--r--Misc/NEWS3
-rw-r--r--Parser/tokenizer.c14
3 files changed, 28 insertions, 7 deletions
diff --git a/Lib/test/test_pep263.py b/Lib/test/test_pep263.py
index 598d980..1290bc7 100644
--- a/Lib/test/test_pep263.py
+++ b/Lib/test/test_pep263.py
@@ -55,6 +55,24 @@ class PEP263Test(unittest.TestCase):
# two bytes in common with the UTF-8 BOM
self.assertRaises(SyntaxError, eval, b'\xef\xbb\x20')
+ def test_error_message(self):
+ compile(b'# -*- coding: iso-8859-15 -*-\n', 'dummy', 'exec')
+ compile(b'\xef\xbb\xbf\n', 'dummy', 'exec')
+ compile(b'\xef\xbb\xbf# -*- coding: utf-8 -*-\n', 'dummy', 'exec')
+ with self.assertRaisesRegexp(SyntaxError, 'fake'):
+ compile(b'# -*- coding: fake -*-\n', 'dummy', 'exec')
+ with self.assertRaisesRegexp(SyntaxError, 'iso-8859-15'):
+ compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n',
+ 'dummy', 'exec')
+ with self.assertRaisesRegexp(SyntaxError, 'BOM'):
+ compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n',
+ 'dummy', 'exec')
+ with self.assertRaisesRegexp(SyntaxError, 'fake'):
+ compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
+ with self.assertRaisesRegexp(SyntaxError, 'BOM'):
+ compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
+
+
def test_main():
support.run_unittest(PEP263Test)
diff --git a/Misc/NEWS b/Misc/NEWS
index b337d48..b6cf7c9 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.4.0 Alpha 1?
Core and Builtins
-----------------
+- Issue #18038: SyntaxError raised during compilation sources with illegal
+ encoding now always contains an encoding name.
+
- Issue #17931: Resolve confusion on Windows between pids and process
handles.
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c
index 5480278..62b1a91 100644
--- a/Parser/tokenizer.c
+++ b/Parser/tokenizer.c
@@ -291,20 +291,20 @@ check_coding_spec(const char* line, Py_ssize_t size, struct tok_state *tok,
tok->encoding = cs;
tok->decoding_state = STATE_NORMAL;
}
- else
+ else {
+ PyErr_Format(PyExc_SyntaxError,
+ "encoding problem: %s", cs);
PyMem_FREE(cs);
+ }
}
} else { /* then, compare cs with BOM */
r = (strcmp(tok->encoding, cs) == 0);
+ if (!r)
+ PyErr_Format(PyExc_SyntaxError,
+ "encoding problem: %s with BOM", cs);
PyMem_FREE(cs);
}
}
- if (!r) {
- cs = tok->encoding;
- if (!cs)
- cs = "with BOM";
- PyErr_Format(PyExc_SyntaxError, "encoding problem: %s", cs);
- }
return r;
}