Index: Lib/test/test_compiler.py =================================================================== --- Lib/test/test_compiler.py (revision 43130) +++ Lib/test/test_compiler.py (working copy) @@ -74,6 +74,32 @@ self.assertEquals(flatten([1, [2]]), [1, 2]) self.assertEquals(flatten((1, (2,))), [1, 2]) + def _testErrEnc(self, src, text, offset): + try: + compile(src, "", "exec") + except SyntaxError, e: + self.assertEquals(e.offset, offset) + self.assertEquals(e.text, text) + + def testSourceCodeEncodingsError(self): + # Test SyntaxError with encoding definition + sjis = "print '\x83\x70\x83\x43\x83\x5c\x83\x93', '\n" + ascii = "print '12345678', '\n" + encdef = "#! -*- coding: ShiftJIS -*-\n" + + # ascii source without encdef + self._testErrEnc(ascii, ascii, 19) + + # ascii source with encdef + self._testErrEnc(encdef+ascii, ascii, 19) + + # non-ascii source with encdef + self._testErrEnc(encdef+sjis, sjis, 19) + + # ShiftJIS source without encdef + self._testErrEnc(sjis, sjis, 19) + + NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard) ############################################################################### Index: Parser/parsetok.c =================================================================== --- Parser/parsetok.c (revision 43130) +++ Parser/parsetok.c (working copy) @@ -16,6 +16,9 @@ /* Forward */ static node *parsetok(struct tok_state *, grammar *, int, perrdetail *, int); static void initerr(perrdetail *err_ret, const char* filename); +#ifdef Py_USING_UNICODE +static PyObject * dec_utf8(const char *enc, const char *text, size_t len); +#endif /* Parse input coming from a string. Return error code, print some errors. */ node * @@ -214,16 +217,48 @@ err_ret->error = E_EOF; err_ret->lineno = tok->lineno; if (tok->buf != NULL) { + char *text = NULL; size_t len; assert(tok->cur - tok->buf < INT_MAX); err_ret->offset = (int)(tok->cur - tok->buf); len = tok->inp - tok->buf; - err_ret->text = (char *) PyObject_MALLOC(len + 1); - if (err_ret->text != NULL) { - if (len > 0) - strncpy(err_ret->text, tok->buf, len); - err_ret->text[len] = '\0'; +#ifdef Py_USING_UNICODE + if (tok->encoding) { + /* convert source to original encondig */ + PyObject *lineobj = dec_utf8(tok->encoding, tok->buf, len); + if (lineobj != NULL) { + int linelen = PyString_Size(lineobj); + const char *line = PyString_AsString(lineobj); + text = PyObject_MALLOC(linelen + 1); + if (text != NULL && line != NULL) { + if (linelen) + strncpy(text, line, linelen); + text[linelen] = '\0'; + } + Py_DECREF(lineobj); + + /* adjust error offset */ + if (err_ret->offset > 1) { + PyObject *offsetobj = dec_utf8(tok->encoding, + tok->buf, err_ret->offset-1); + if (offsetobj) { + err_ret->offset = PyString_Size(offsetobj) + 1; + Py_DECREF(offsetobj); + } + } + + } } +#endif + if (text == NULL) { + text = (char *) PyObject_MALLOC(len + 1); + if (text != NULL) { + if (len > 0) + strncpy(text, tok->buf, len); + text[len] = '\0'; + } + } + err_ret->text = text; } } else if (tok->encoding != NULL) { node* r = PyNode_New(encoding_decl); @@ -250,3 +285,19 @@ err_ret->token = -1; err_ret->expected = -1; } + +#ifdef Py_USING_UNICODE +static PyObject * +dec_utf8(const char *enc, const char *text, size_t len) { + PyObject *ret = NULL; + PyObject *unicode_text = PyUnicode_DecodeUTF8(text, len, "replace"); + if (unicode_text) { + ret = PyUnicode_AsEncodedString(unicode_text, enc, "replace"); + Py_DECREF(unicode_text); + } + if (!ret) { + PyErr_Print(); + } + return ret; +} +#endif