Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Py_BuildValue: make tuple with bool?

I see in docs, that I can build tuple value with int (specifying 'i'). I need to make tuple with bool, e.g. (True, 10). How can I make such tuple with bool (what specifier needed)?

like image 654
Prog1020 Avatar asked Jan 19 '14 19:01

Prog1020


1 Answers

There is no predefined format character for that conversion, but it is trivial to simulate one by inserting Py_True or Py_False object into the tuple, as appropriate. For example:

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

Another option is to use PyBool_FromLong to do the conversion. In that case, remember to use the N format to account for PyBool_FromLong returning a new reference:

PyObject *tuple_with_bool = Py_BuildValue("Ni", PyBool_FromLong(b), i);
like image 94
user4815162342 Avatar answered Nov 12 '22 09:11

user4815162342