Py_BuildValue: make a tuple with bool?

I see in docs that I can build a tuple value using int (with an ā€œiā€). I need to make a tuple with bool, for example. (True, 10). How can I make such a tuple with bool (which specifier is needed)?

+4
source share
1 answer

There is no predefined format character for this conversion, but it is trivial to simulate it by inserting an object into the tuple Py_Trueor Py_False, if necessary. For example:

int i = ...;
bool b = ...;
PyObject *tuple_with_bool = Py_BuildValue("Oi", b ? Py_True: Py_False, i);

- PyBool_FromLong . N PyBool_FromLong, :

PyObject *tuple_with_bool = Py_BuildValue("Ni", PyBool_FromLong(b), i);
+9

All Articles