mirror of
https://github.com/python/cpython.git
synced 2025-11-13 07:26:31 +00:00
Issue #1882: when compiling code from a string, encoding cookies in the
second line of code were not always recognized correctly.
This commit is contained in:
parent
2bdc48c6e0
commit
38d1715b0d
2 changed files with 22 additions and 3 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
#! -*- coding: koi8-r -*-
|
#! -*- coding: koi8-r -*-
|
||||||
# This file is marked as binary in the CVS, to prevent MacCVS from recoding it.
|
# This file is marked as binary in SVN, to prevent MacCVS from recoding it.
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
from test import test_support
|
from test import test_support
|
||||||
|
|
@ -16,6 +16,14 @@ class PEP263Test(unittest.TestCase):
|
||||||
'\\\xd0\x9f'
|
'\\\xd0\x9f'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_compilestring(self):
|
||||||
|
# see #1882
|
||||||
|
c = compile("\n# coding: utf-8\nu = u'\xc3\xb3'\n", "dummy", "exec")
|
||||||
|
d = {}
|
||||||
|
exec c in d
|
||||||
|
self.assertEqual(d['u'], u'\xf3')
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
test_support.run_unittest(PEP263Test)
|
test_support.run_unittest(PEP263Test)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -586,6 +586,7 @@ decode_str(const char *str, struct tok_state *tok)
|
||||||
{
|
{
|
||||||
PyObject* utf8 = NULL;
|
PyObject* utf8 = NULL;
|
||||||
const char *s;
|
const char *s;
|
||||||
|
char *newl[2] = {NULL, NULL};
|
||||||
int lineno = 0;
|
int lineno = 0;
|
||||||
tok->enc = NULL;
|
tok->enc = NULL;
|
||||||
tok->str = str;
|
tok->str = str;
|
||||||
|
|
@ -604,13 +605,23 @@ decode_str(const char *str, struct tok_state *tok)
|
||||||
for (s = str;; s++) {
|
for (s = str;; s++) {
|
||||||
if (*s == '\0') break;
|
if (*s == '\0') break;
|
||||||
else if (*s == '\n') {
|
else if (*s == '\n') {
|
||||||
|
newl[lineno] = s;
|
||||||
lineno++;
|
lineno++;
|
||||||
if (lineno == 2) break;
|
if (lineno == 2) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tok->enc = NULL;
|
tok->enc = NULL;
|
||||||
if (!check_coding_spec(str, s - str, tok, buf_setreadl))
|
/* need to check line 1 and 2 separately since check_coding_spec
|
||||||
|
assumes a single line as input */
|
||||||
|
if (newl[0]) {
|
||||||
|
if (!check_coding_spec(str, newl[0] - str, tok, buf_setreadl))
|
||||||
return error_ret(tok);
|
return error_ret(tok);
|
||||||
|
if (tok->enc == NULL && newl[1]) {
|
||||||
|
if (!check_coding_spec(newl[0]+1, newl[1] - newl[0],
|
||||||
|
tok, buf_setreadl))
|
||||||
|
return error_ret(tok);
|
||||||
|
}
|
||||||
|
}
|
||||||
#ifdef Py_USING_UNICODE
|
#ifdef Py_USING_UNICODE
|
||||||
if (tok->enc != NULL) {
|
if (tok->enc != NULL) {
|
||||||
assert(utf8 == NULL);
|
assert(utf8 == NULL);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue