Skip to content

Commit 5ef0d15

Browse files
committed
bpo-45325: Add a new 'p' parameter to Py_BuildValue to convert an integer into a Python bool
1 parent 8497514 commit 5ef0d15

File tree

6 files changed

+47
-1
lines changed

6 files changed

+47
-1
lines changed

Doc/c-api/arg.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,11 @@ Building values
616616
``n`` (:class:`int`) [Py_ssize_t]
617617
Convert a C :c:type:`Py_ssize_t` to a Python integer.
618618
619+
``p`` (:class:`int`) [int]
620+
Convert a C :c:type:`int` to a Python :class:`bool` object.
621+
622+
.. versionadded:: 3.11
623+
619624
``c`` (:class:`bytes` of length 1) [char]
620625
Convert a C :c:type:`int` representing a byte to a Python :class:`bytes` object of
621626
length 1.

Doc/whatsnew/3.11.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,10 @@ New Features
455455
* Add a new :c:func:`PyType_GetQualName` function to get type's qualified name.
456456
(Contributed by Hai Shi in :issue:`42035`.)
457457

458+
* Add a new ``p`` format parameter to :c:func:`Py_BuildValue` that allows to
459+
take a C integer and produce a Python :class:`bool` object. (Contributed by
460+
Pablo Galindo in :issue:`45325`.)
461+
458462
Porting to Python 3.11
459463
----------------------
460464

Lib/test/test_capi.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,9 @@ def test_getitem_with_error(self):
295295
def test_buildvalue_N(self):
296296
_testcapi.test_buildvalue_N()
297297

298+
def test_buildvalue_P(self):
299+
_testcapi.test_buildvalue_p()
300+
298301
def test_set_nomemory(self):
299302
code = """if 1:
300303
import _testcapi
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add a new ``p`` format parameter to :c:func:`Py_BuildValue` that allows to
2+
take a C integer and produce a Python :class:`bool` object. Patch by Pablo
3+
Galindo.

Modules/_testcapimodule.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,32 @@ test_buildvalue_N_error(const char *fmt)
10391039
return 0;
10401040
}
10411041

1042+
static PyObject *
1043+
test_buildvalue_p(PyObject *self, PyObject *Py_UNUSED(ignored))
1044+
{
1045+
PyObject *res = Py_BuildValue("p", 3);
1046+
if (res == NULL) {
1047+
return NULL;
1048+
}
1049+
if (!Py_IsTrue(res)) {
1050+
Py_DECREF(res);
1051+
return raiseTestError("test_buildvalue_p", "Py_BuildValue(\"p\", 3) returned wrong result");
1052+
}
1053+
Py_DECREF(res);
1054+
1055+
res = Py_BuildValue("p", 0);
1056+
if (res == NULL) {
1057+
return NULL;
1058+
}
1059+
if (!Py_IsFalse(res)) {
1060+
Py_DECREF(res);
1061+
return raiseTestError("test_buildvalue_p", "Py_BuildValue(\"p\", 0) returned wrong result");
1062+
}
1063+
Py_DECREF(res);
1064+
1065+
Py_RETURN_NONE;
1066+
}
1067+
10421068
static PyObject *
10431069
test_buildvalue_N(PyObject *self, PyObject *Py_UNUSED(ignored))
10441070
{
@@ -5717,6 +5743,7 @@ static PyMethodDef TestMethods[] = {
57175743
#endif
57185744
{"getbuffer_with_null_view", getbuffer_with_null_view, METH_O},
57195745
{"PyBuffer_SizeFromFormat", test_PyBuffer_SizeFromFormat, METH_VARARGS},
5746+
{"test_buildvalue_p", test_buildvalue_p, METH_NOARGS},
57205747
{"test_buildvalue_N", test_buildvalue_N, METH_NOARGS},
57215748
{"test_buildvalue_issue38913", test_buildvalue_issue38913, METH_NOARGS},
57225749
{"get_args", get_args, METH_VARARGS},

Python/modsupport.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,11 @@ do_mkvalue(const char **p_format, va_list *p_va, int flags)
389389
int i = va_arg(*p_va, int);
390390
return PyUnicode_FromOrdinal(i);
391391
}
392-
392+
case 'p':
393+
{
394+
int i = va_arg(*p_va, int);
395+
return PyBool_FromLong(i);
396+
}
393397
case 's':
394398
case 'z':
395399
case 'U': /* XXX deprecated alias */

0 commit comments

Comments
 (0)