[Python-checkins] bpo-40943: Fix skipitem() didn't raise SystemError (GH-25937)

miss-islington webhook-mailer at python.org
Thu May 6 23:18:46 EDT 2021


https://github.com/python/cpython/commit/569ca81adf0be92be8752f6cc6492117f9ef3c0b
commit: 569ca81adf0be92be8752f6cc6492117f9ef3c0b
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-05-06T20:18:42-07:00
summary:

bpo-40943: Fix skipitem() didn't raise SystemError (GH-25937)


`convertitem()` raises `SystemError` when 'GH-' is used without `PY_SSIZE_T_CLEAN`.
This commit makes `skipitem()` raise it too.
(cherry picked from commit 4ebf4a6bfad4afcbab3baf9c0159c7767e2a64c0)

Co-authored-by: Inada Naoki <songofacandy at gmail.com>

files:
M Lib/test/test_getargs2.py
M Modules/_testcapimodule.c
M Python/getargs.c

diff --git a/Lib/test/test_getargs2.py b/Lib/test/test_getargs2.py
index c67e6f51a23464..e0db9e40e650b6 100644
--- a/Lib/test/test_getargs2.py
+++ b/Lib/test/test_getargs2.py
@@ -874,6 +874,13 @@ def test_s_hash(self):
         self.assertRaises(TypeError, getargs_s_hash, memoryview(b'memoryview'))
         self.assertRaises(TypeError, getargs_s_hash, None)
 
+    def test_s_hash_int(self):
+        # "s#" without PY_SSIZE_T_CLEAN defined.
+        from _testcapi import getargs_s_hash_int
+        self.assertRaises(SystemError, getargs_s_hash_int, "abc")
+        self.assertRaises(SystemError, getargs_s_hash_int, x=42)
+        # getargs_s_hash_int() don't raise SystemError because skipitem() is not called.
+
     def test_z(self):
         from _testcapi import getargs_z
         self.assertEqual(getargs_z('abc\xe9'), b'abc\xc3\xa9')
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 0a3040f703da97..d926ad8f87a7a4 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -5455,9 +5455,6 @@ pynumber_tobase(PyObject *module, PyObject *args)
 }
 
 
-static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
-
-
 static PyObject*
 test_set_type_size(PyObject *self, PyObject *Py_UNUSED(ignored))
 {
@@ -5596,6 +5593,8 @@ test_fatal_error(PyObject *self, PyObject *args)
 }
 
 
+static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
+static PyObject *getargs_s_hash_int(PyObject *, PyObject *, PyObject*);
 
 static PyMethodDef TestMethods[] = {
     {"raise_exception",         raise_exception,                 METH_VARARGS},
@@ -5703,6 +5702,8 @@ static PyMethodDef TestMethods[] = {
     {"getargs_s",               getargs_s,                       METH_VARARGS},
     {"getargs_s_star",          getargs_s_star,                  METH_VARARGS},
     {"getargs_s_hash",          getargs_s_hash,                  METH_VARARGS},
+    {"getargs_s_hash_int",      (PyCFunction)(void(*)(void))getargs_s_hash_int,
+      METH_VARARGS|METH_KEYWORDS},
     {"getargs_z",               getargs_z,                       METH_VARARGS},
     {"getargs_z_star",          getargs_z_star,                  METH_VARARGS},
     {"getargs_z_hash",          getargs_z_hash,                  METH_VARARGS},
@@ -7375,3 +7376,19 @@ test_buildvalue_issue38913(PyObject *self, PyObject *Py_UNUSED(ignored))
 
     Py_RETURN_NONE;
 }
+
+#undef PyArg_ParseTupleAndKeywords
+PyAPI_FUNC(int) PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
+                                            const char *, char **, ...);
+
+static PyObject *
+getargs_s_hash_int(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+    static char *keywords[] = {"", "x", NULL};
+    const char *s;
+    int len;
+    int i = 0;
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s#i", keywords, &s, &len, &i))
+        return NULL;
+    Py_RETURN_NONE;
+}
diff --git a/Python/getargs.c b/Python/getargs.c
index b85b575a147fe6..d5e083509efefc 100644
--- a/Python/getargs.c
+++ b/Python/getargs.c
@@ -2532,15 +2532,12 @@ skipitem(const char **p_format, va_list *p_va, int flags)
             }
             if (*format == '#') {
                 if (p_va != NULL) {
-                    if (flags & FLAG_SIZE_T)
-                        (void) va_arg(*p_va, Py_ssize_t *);
-                    else {
-                        if (PyErr_WarnEx(PyExc_DeprecationWarning,
-                                    "PY_SSIZE_T_CLEAN will be required for '#' formats", 1)) {
-                            return NULL;
-                        }
-                        (void) va_arg(*p_va, int *);
+                    if (!(flags & FLAG_SIZE_T)) {
+                        PyErr_SetString(PyExc_SystemError,
+                                "PY_SSIZE_T_CLEAN macro must be defined for '#' formats");
+                        return NULL;
                     }
+                    (void) va_arg(*p_va, Py_ssize_t *);
                 }
                 format++;
             } else if ((c == 's' || c == 'z' || c == 'y' || c == 'w')



More information about the Python-checkins mailing list