]> granicus.if.org Git - python/commitdiff
complain if the codec doesn't return unicode
authorBenjamin Peterson <benjamin@python.org>
Sat, 28 Dec 2013 16:33:58 +0000 (10:33 -0600)
committerBenjamin Peterson <benjamin@python.org>
Sat, 28 Dec 2013 16:33:58 +0000 (10:33 -0600)
Lib/test/bad_coding3.py [new file with mode: 0644]
Lib/test/test_pep263.py
Misc/NEWS
Parser/tokenizer.c

diff --git a/Lib/test/bad_coding3.py b/Lib/test/bad_coding3.py
new file mode 100644 (file)
index 0000000..77836d9
--- /dev/null
@@ -0,0 +1,2 @@
+# coding: string-escape
+\x70\x72\x69\x6e\x74\x20\x32\x2b\x32\x0a
index 4b60624be21bf3f62f7cdf0feb2d1ba4d61455b2..a3abc3c2ec48c24df0705aeeee0ada24fafa3197 100644 (file)
@@ -58,6 +58,11 @@ class PEP263Test(unittest.TestCase):
         with self.assertRaisesRegexp(SyntaxError, 'BOM'):
             compile('\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
 
+    def test_non_unicode_codec(self):
+        with self.assertRaisesRegexp(SyntaxError,
+                                     'codec did not return a unicode'):
+            from test import bad_coding3
+
 
 def test_main():
     test_support.run_unittest(PEP263Test)
index fe793e84d96c583d5752af57242136126a0b8b2b..7fff7809fe2db3fbbd21d3244dca4164aa49440e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,9 @@ What's New in Python 2.7.7?
 Core and Builtins
 -----------------
 
+- Raise a better error when non-unicode codecs are used for a file's coding
+  cookie.
+
 - Issue #17976: Fixed potential problem with file.write() not detecting IO error
   by inspecting the return value of fwrite().  Based on patches by Jaakko Moisio
   and Victor Stinner.
index 46cf9b297cdf03be33f9552eec911f19e197831e..249bb962065c5fbe8326ff2363d2f0321c8ed09f 100644 (file)
@@ -400,6 +400,12 @@ fp_readl(char *s, int size, struct tok_state *tok)
         buf = PyObject_CallObject(tok->decoding_readline, NULL);
         if (buf == NULL)
             return error_ret(tok);
+        if (!PyUnicode_Check(buf)) {
+            Py_DECREF(buf);
+            PyErr_SetString(PyExc_SyntaxError,
+                            "codec did not return a unicode object");
+            return error_ret(tok);
+        }
     } else {
         tok->decoding_buffer = NULL;
         if (PyString_CheckExact(buf))