summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPablo Galindo Salgado <Pablogsal@gmail.com>2021-11-17 23:17:18 (GMT)
committerGitHub <noreply@github.com>2021-11-17 23:17:18 (GMT)
commite3aa9fd77bf474bb3e8a7a1d1bd1ebf45147945a (patch)
treee1211dfaad7341169da5e497a89b9eb9b60ac506
parent4ffde90dccd741b04a448f2e44f0b82a41b6fe96 (diff)
downloadcpython-e3aa9fd77bf474bb3e8a7a1d1bd1ebf45147945a.zip
cpython-e3aa9fd77bf474bb3e8a7a1d1bd1ebf45147945a.tar.gz
cpython-e3aa9fd77bf474bb3e8a7a1d1bd1ebf45147945a.tar.bz2
[3.10] bpo-45822: Respect PEP 263's coding cookies in the parser even if flags are not provided (GH-29582) (GH-29586)
(cherry picked from commit da20d7401de97b425897d3069f71f77b039eb16f) Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
-rw-r--r--Lib/test/test_capi.py8
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2021-11-16-19-41-04.bpo-45822.OT6ueS.rst2
-rw-r--r--Modules/_testcapimodule.c14
-rw-r--r--Parser/pegen.c2
4 files changed, 25 insertions, 1 deletions
diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py
index 6ed5ecd..974e3d0 100644
--- a/Lib/test/test_capi.py
+++ b/Lib/test/test_capi.py
@@ -1013,6 +1013,14 @@ class Test_ModuleStateAccess(unittest.TestCase):
with self.assertRaises(TypeError):
increment_count(1, 2, 3)
+ def test_Py_CompileString(self):
+ # 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()
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-11-16-19-41-04.bpo-45822.OT6ueS.rst b/Misc/NEWS.d/next/Core and Builtins/2021-11-16-19-41-04.bpo-45822.OT6ueS.rst
new file mode 100644
index 0000000..1ac7a8b
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-11-16-19-41-04.bpo-45822.OT6ueS.rst
@@ -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
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 5e59549..9f25b64 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -391,6 +391,19 @@ static PyTypeObject _HashInheritanceTester_Type = {
};
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);
+}
+
+static PyObject*
test_lazy_hash_inheritance(PyObject* self, PyObject *Py_UNUSED(ignored))
{
PyTypeObject *type;
@@ -5826,6 +5839,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},
diff --git a/Parser/pegen.c b/Parser/pegen.c
index cfb4b8e..170d28b 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -1434,7 +1434,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);