Skip to content

Commit da20d74

Browse files
authored
bpo-45822: Respect PEP 263's coding cookies in the parser even if flags are not provided (GH-29582)
1 parent d7e2100 commit da20d74

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

Lib/test/test_capi.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,14 @@ def test_state_access(self):
10171017
with self.assertRaises(TypeError):
10181018
increment_count(1, 2, 3)
10191019

1020+
def test_Py_CompileString(self):
1021+
# Check that Py_CompileString respects the coding cookie
1022+
_compile = _testcapi.Py_CompileString
1023+
code = b"# -*- coding: latin1 -*-\nprint('\xc2\xa4')\n"
1024+
result = _compile(code)
1025+
expected = compile(code, "<string>", "exec")
1026+
self.assertEqual(result.co_consts, expected.co_consts)
1027+
10201028

10211029
if __name__ == "__main__":
10221030
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed a bug in the parser that was causing it to not respect :pep:`263`
2+
coding cookies when no flags are provided. Patch by Pablo Galindo

Modules/_testcapimodule.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,19 @@ static PyTypeObject _HashInheritanceTester_Type = {
381381
PyType_GenericNew, /* tp_new */
382382
};
383383

384+
static PyObject*
385+
pycompilestring(PyObject* self, PyObject *obj) {
386+
if (PyBytes_CheckExact(obj) == 0) {
387+
PyErr_SetString(PyExc_ValueError, "Argument must be a bytes object");
388+
return NULL;
389+
}
390+
const char *the_string = PyBytes_AsString(obj);
391+
if (the_string == NULL) {
392+
return NULL;
393+
}
394+
return Py_CompileString(the_string, "blech", Py_file_input);
395+
}
396+
384397
static PyObject*
385398
test_lazy_hash_inheritance(PyObject* self, PyObject *Py_UNUSED(ignored))
386399
{
@@ -6070,6 +6083,7 @@ static PyMethodDef TestMethods[] = {
60706083
{"return_null_without_error", return_null_without_error, METH_NOARGS},
60716084
{"return_result_with_error", return_result_with_error, METH_NOARGS},
60726085
{"getitem_with_error", getitem_with_error, METH_VARARGS},
6086+
{"Py_CompileString", pycompilestring, METH_O},
60736087
{"PyTime_FromSeconds", test_pytime_fromseconds, METH_VARARGS},
60746088
{"PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS},
60756089
{"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS},

Parser/pegen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,7 @@ _PyPegen_run_parser_from_string(const char *str, int start_rule, PyObject *filen
14631463
int exec_input = start_rule == Py_file_input;
14641464

14651465
struct tok_state *tok;
1466-
if (flags == NULL || flags->cf_flags & PyCF_IGNORE_COOKIE) {
1466+
if (flags != NULL && flags->cf_flags & PyCF_IGNORE_COOKIE) {
14671467
tok = _PyTokenizer_FromUTF8(str, exec_input);
14681468
} else {
14691469
tok = _PyTokenizer_FromString(str, exec_input);

0 commit comments

Comments
 (0)