Skip to content

Commit 0ef308a

Browse files
authored
bpo-45822: Respect PEP 263's coding cookies in the parser even if flags are not provided (GH-29582) (GH-29585)
(cherry picked from commit da20d74)
1 parent 87787c8 commit 0ef308a

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
@@ -926,6 +926,14 @@ def test_state_access(self):
926926
with self.assertRaises(TypeError):
927927
increment_count(1, 2, 3)
928928

929+
def test_Py_CompileString(self):
930+
# Check that Py_CompileString respects the coding cookie
931+
_compile = _testcapi.Py_CompileString
932+
code = b"# -*- coding: latin1 -*-\nprint('\xc2\xa4')\n"
933+
result = _compile(code)
934+
expected = compile(code, "<string>", "exec")
935+
self.assertEqual(result.co_consts, expected.co_consts)
936+
929937

930938
if __name__ == "__main__":
931939
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
@@ -329,6 +329,19 @@ static PyTypeObject _HashInheritanceTester_Type = {
329329
PyType_GenericNew, /* tp_new */
330330
};
331331

332+
static PyObject*
333+
pycompilestring(PyObject* self, PyObject *obj) {
334+
if (PyBytes_CheckExact(obj) == 0) {
335+
PyErr_SetString(PyExc_ValueError, "Argument must be a bytes object");
336+
return NULL;
337+
}
338+
const char *the_string = PyBytes_AsString(obj);
339+
if (the_string == NULL) {
340+
return NULL;
341+
}
342+
return Py_CompileString(the_string, "blech", Py_file_input);
343+
}
344+
332345
static PyObject*
333346
test_lazy_hash_inheritance(PyObject* self, PyObject *Py_UNUSED(ignored))
334347
{
@@ -5537,6 +5550,7 @@ static PyMethodDef TestMethods[] = {
55375550
return_null_without_error, METH_NOARGS},
55385551
{"return_result_with_error",
55395552
return_result_with_error, METH_NOARGS},
5553+
{"Py_CompileString", pycompilestring, METH_O},
55405554
{"PyTime_FromSeconds", test_pytime_fromseconds, METH_VARARGS},
55415555
{"PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS},
55425556
{"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS},

Parser/pegen/pegen.c

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

12271227
struct tok_state *tok;
1228-
if (flags == NULL || flags->cf_flags & PyCF_IGNORE_COOKIE) {
1228+
if (flags != NULL && flags->cf_flags & PyCF_IGNORE_COOKIE) {
12291229
tok = PyTokenizer_FromUTF8(str, exec_input);
12301230
} else {
12311231
tok = PyTokenizer_FromString(str, exec_input);

0 commit comments

Comments
 (0)