Skip to content

bpo-45822: Respect PEP 263's coding cookies in the parser even if flags are not provided #29582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/test/test_capi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,14 @@ def test_state_access(self):
with self.assertRaises(TypeError):
increment_count(1, 2, 3)

def test_Py_CompileString(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, while backporting this patch to our internal 3.9, I noticed this test method is in the wrong class. It should be in the CAPITest class.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will submit a fix today

# Check that Py_CompileString respects the coding cookie
_compile = _testcapi.Py_CompileString
code = b"# -*- coding: latin1 -*-\nprint('\xc2\xa4')\n"
result = _compile(code)
expected = compile(code, "<string>", "exec")
self.assertEqual(result.co_consts, expected.co_consts)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed a bug in the parser that was causing it to not respect :pep:`263`
coding cookies when no flags are provided. Patch by Pablo Galindo
14 changes: 14 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,19 @@ static PyTypeObject _HashInheritanceTester_Type = {
PyType_GenericNew, /* tp_new */
};

static PyObject*
pycompilestring(PyObject* self, PyObject *obj) {
if (PyBytes_CheckExact(obj) == 0) {
PyErr_SetString(PyExc_ValueError, "Argument must be a bytes object");
return NULL;
}
const char *the_string = PyBytes_AsString(obj);
if (the_string == NULL) {
return NULL;
}
return Py_CompileString(the_string, "blech", Py_file_input);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Py_CompileString(the_string, "blech", Py_file_input);
return Py_CompileString(the_string, "<string>", Py_file_input);

So it matches the test.

}

static PyObject*
test_lazy_hash_inheritance(PyObject* self, PyObject *Py_UNUSED(ignored))
{
Expand Down Expand Up @@ -6070,6 +6083,7 @@ static PyMethodDef TestMethods[] = {
{"return_null_without_error", return_null_without_error, METH_NOARGS},
{"return_result_with_error", return_result_with_error, METH_NOARGS},
{"getitem_with_error", getitem_with_error, METH_VARARGS},
{"Py_CompileString", pycompilestring, METH_O},
{"PyTime_FromSeconds", test_pytime_fromseconds, METH_VARARGS},
{"PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS},
{"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS},
Expand Down
2 changes: 1 addition & 1 deletion Parser/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,7 @@ _PyPegen_run_parser_from_string(const char *str, int start_rule, PyObject *filen
int exec_input = start_rule == Py_file_input;

struct tok_state *tok;
if (flags == NULL || flags->cf_flags & PyCF_IGNORE_COOKIE) {
if (flags != NULL && flags->cf_flags & PyCF_IGNORE_COOKIE) {
tok = _PyTokenizer_FromUTF8(str, exec_input);
} else {
tok = _PyTokenizer_FromString(str, exec_input);
Expand Down