]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45325: Add a new 'p' parameter to Py_BuildValue to convert an integer into a...
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Tue, 18 Feb 2025 17:14:11 +0000 (17:14 +0000)
committerGitHub <noreply@github.com>
Tue, 18 Feb 2025 17:14:11 +0000 (17:14 +0000)
Doc/c-api/arg.rst
Doc/whatsnew/3.14.rst
Misc/NEWS.d/next/C_API/2025-02-13-00-57-05.gh-issue-45325.Cwif9z.rst [new file with mode: 0644]
Modules/_testcapimodule.c
Python/modsupport.c

index 209056ef2f8bce30fb00c76dce2fbcac7df4fcf1..deebaba195299954cee23a5c2910c64ed9ba3fa8 100644 (file)
@@ -645,6 +645,10 @@ Building values
    ``n`` (:class:`int`) [:c:type:`Py_ssize_t`]
       Convert a C :c:type:`Py_ssize_t` to a Python integer.
 
+   ``p`` (:class:`bool`) [int]
+      Convert a C :c:expr:`int` to a Python :class:`bool` object.
+      .. versionadded:: 3.14
+
    ``c`` (:class:`bytes` of length 1) [char]
       Convert a C :c:expr:`int` representing a byte to a Python :class:`bytes` object of
       length 1.
index ac0ae8cf0133e6d50b7d546b7ba06c2d57f6d8d5..556af51756a521919b99d629f4148043f33f498c 100644 (file)
@@ -1433,6 +1433,10 @@ New features
   and get an attribute of the module.
   (Contributed by Victor Stinner in :gh:`128911`.)
 
+* Add support for a new ``p`` format unit in :c:func:`Py_BuildValue` that allows to
+  take a C integer and produce a Python :class:`bool` object. (Contributed by
+  Pablo Galindo in :issue:`45325`.)
+
 
 Limited C API changes
 ---------------------
diff --git a/Misc/NEWS.d/next/C_API/2025-02-13-00-57-05.gh-issue-45325.Cwif9z.rst b/Misc/NEWS.d/next/C_API/2025-02-13-00-57-05.gh-issue-45325.Cwif9z.rst
new file mode 100644 (file)
index 0000000..d953654
--- /dev/null
@@ -0,0 +1,3 @@
+Add a new ``p`` format parameter to :c:func:`Py_BuildValue` that allows to
+take a C integer and produce a Python :class:`bool` object. Patch by Pablo
+Galindo.
index c84646ccf03fa74a322d131c7659257f503b63ee..3d5fa9006447ded0876287109db90c886f9d89ae 100644 (file)
@@ -411,6 +411,31 @@ test_buildvalue_N(PyObject *self, PyObject *Py_UNUSED(ignored))
     Py_RETURN_NONE;
 }
 
+static PyObject *
+test_buildvalue_p(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+    PyObject *res = Py_BuildValue("p", 3);
+    if (res == NULL) {
+        return NULL;
+    }
+    if (!Py_IsTrue(res)) {
+        Py_DECREF(res);
+        return raiseTestError(self, "test_buildvalue_p", "Py_BuildValue(\"p\", 3) returned wrong result");
+    }
+    Py_DECREF(res);
+
+    res = Py_BuildValue("p", 0);
+    if (res == NULL) {
+        return NULL;
+    }
+    if (!Py_IsFalse(res)) {
+        Py_DECREF(res);
+        return raiseTestError(self, "test_buildvalue_p", "Py_BuildValue(\"p\", 0) returned wrong result");
+    }
+    Py_DECREF(res);
+
+    Py_RETURN_NONE;
+}
 
 static PyObject *
 pyobject_repr_from_null(PyObject *self, PyObject *Py_UNUSED(ignored))
@@ -2512,6 +2537,7 @@ static PyMethodDef TestMethods[] = {
     {"py_buildvalue",            py_buildvalue,                  METH_VARARGS},
     {"py_buildvalue_ints",       py_buildvalue_ints,             METH_VARARGS},
     {"test_buildvalue_N",        test_buildvalue_N,              METH_NOARGS},
+    {"test_buildvalue_p",       test_buildvalue_p,               METH_NOARGS},
     {"test_reftracer",          test_reftracer,                  METH_NOARGS},
     {"_test_thread_state",      test_thread_state,               METH_VARARGS},
     {"gilstate_ensure_release", gilstate_ensure_release,         METH_NOARGS},
index 517dc971f88c8710d47dcf7f96395bfed01d4fa3..501231affe8cd4de726c09fcc8ea20387dcffab1 100644 (file)
@@ -364,6 +364,11 @@ do_mkvalue(const char **p_format, va_list *p_va)
             int i = va_arg(*p_va, int);
             return PyUnicode_FromOrdinal(i);
         }
+        case 'p':
+        {
+            int i = va_arg(*p_va, int);
+            return PyBool_FromLong(i);
+        }
 
         case 's':
         case 'z':