Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-45325: Add a new 'p' parameter to Py_BuildValue to convert an integer into a Python bool #28634

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
Feb 18, 2025
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
4 changes: 4 additions & 0 deletions Doc/c-api/arg.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,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
---------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
26 changes: 26 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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},
Expand Down
5 changes: 5 additions & 0 deletions Python/modsupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
Loading