Skip to content

Commit 22d9ee7

Browse files
committed
complain if the codec doesn't return unicode
1 parent 63cc99d commit 22d9ee7

4 files changed

Lines changed: 16 additions & 0 deletions

File tree

Lib/test/bad_coding3.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# coding: string-escape
2+
\x70\x72\x69\x6e\x74\x20\x32\x2b\x32\x0a

Lib/test/test_pep263.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ def test_error_message(self):
5858
with self.assertRaisesRegexp(SyntaxError, 'BOM'):
5959
compile('\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
6060

61+
def test_non_unicode_codec(self):
62+
with self.assertRaisesRegexp(SyntaxError,
63+
'codec did not return a unicode'):
64+
from test import bad_coding3
65+
6166

6267
def test_main():
6368
test_support.run_unittest(PEP263Test)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ What's New in Python 2.7.7?
99
Core and Builtins
1010
-----------------
1111

12+
- Raise a better error when non-unicode codecs are used for a file's coding
13+
cookie.
14+
1215
- Issue #17976: Fixed potential problem with file.write() not detecting IO error
1316
by inspecting the return value of fwrite(). Based on patches by Jaakko Moisio
1417
and Victor Stinner.

Parser/tokenizer.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,12 @@ fp_readl(char *s, int size, struct tok_state *tok)
400400
buf = PyObject_CallObject(tok->decoding_readline, NULL);
401401
if (buf == NULL)
402402
return error_ret(tok);
403+
if (!PyUnicode_Check(buf)) {
404+
Py_DECREF(buf);
405+
PyErr_SetString(PyExc_SyntaxError,
406+
"codec did not return a unicode object");
407+
return error_ret(tok);
408+
}
403409
} else {
404410
tok->decoding_buffer = NULL;
405411
if (PyString_CheckExact(buf))

0 commit comments

Comments
 (0)