]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45822: Respect PEP 263's coding cookies in the parser even if flags are not provi...
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Wed, 17 Nov 2021 23:18:16 +0000 (23:18 +0000)
committerGitHub <noreply@github.com>
Wed, 17 Nov 2021 23:18:16 +0000 (00:18 +0100)
(cherry picked from commit da20d7401de97b425897d3069f71f77b039eb16f)

Lib/test/test_capi.py
Misc/NEWS.d/next/Core and Builtins/2021-11-16-19-41-04.bpo-45822.OT6ueS.rst [new file with mode: 0644]
Modules/_testcapimodule.c
Parser/pegen/pegen.c

index 6b2fe2f1580c36a3f5339d1321bea3144295b784..23e65df67190c01892d0a2922b384d72f1a8d574 100644 (file)
@@ -926,6 +926,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 (file)
index 0000000..1ac7a8b
--- /dev/null
@@ -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
index ae1284178dc60e1f6b7622f89f06bdaaf501971b..d1f756395373da1a00feedace72401f90bd09a8d 100644 (file)
@@ -329,6 +329,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);
+}
+
 static PyObject*
 test_lazy_hash_inheritance(PyObject* self, PyObject *Py_UNUSED(ignored))
 {
@@ -5537,6 +5550,7 @@ static PyMethodDef TestMethods[] = {
         return_null_without_error, METH_NOARGS},
     {"return_result_with_error",
         return_result_with_error, METH_NOARGS},
+    {"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},
index efcf9ac1ebd742470feddd70c0627bbf04ca082e..2e986c5294f1998c0c9b328fa969b1c7cd190775 100644 (file)
@@ -1225,7 +1225,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);