How can I convert Python boolean object to C int (or C++ boolean) (Python C API)
<p>I have a variable <code>PyObject</code> that I know is a Python bool. It either is <code>True</code> or <code>False</code> (eg. <code>Py_True</code> or <code>Py_False</code>). Now I would like to convert it to C++ somehow.</p>

<p>Doing this with strings isn't so hard, there is a helper function for that - <code>PyBytes_AsString</code> that converts python string into C string. Now I need something like that for boolean (or int as there is no <code>bool</code> in C).</p>

<p>Or if there isn't conversion, maybe some function that can compare with true or false? Something like <code>int PyBool_IsTrue(PyObject*)</code>?</p>

<p>Here is some example code for easier understanding of what I need:</p>

<pre><code>#include &lt;Python.h&gt;

int main()
{
    /* here I create Python boolean with value of True */
    PyObject *b = Py_RETURN_TRUE;
    /* now that I have it I would like to turn in into C type so that I can determine if it's True or False */
    /* something like */
    if (PyBool_IsTrue(b))
    { /* it's true! */ }
    else
    { /* it's false */ }
    return 0;
}
</code></pre>

<p>This obviously wouldn't work as there is no such function like <code>PyBool_IsTrue</code> :( how can I do that?</p>

<p>Snippet of Python header (boolobject.h):</p>

<pre><code>/* Boolean object interface */

#ifndef Py_BOOLOBJECT_H
#define Py_BOOLOBJECT_H
#ifdef __cplusplus
extern "C" {
#endif


PyAPI_DATA(PyTypeObject) PyBool_Type;

#define PyBool_Check(x) (Py_TYPE(x) == &amp;PyBool_Type)

/* Py_False and Py_True are the only two bools in existence.
Don't forget to apply Py_INCREF() when returning either!!! */

/* Don't use these directly */
PyAPI_DATA(struct _longobject) _Py_FalseStruct, _Py_TrueStruct;

/* Use these macros */
#define Py_False ((PyObject *) &amp;_Py_FalseStruct)
#define Py_True ((PyObject *) &amp;_Py_TrueStruct)

/* Macros for returning Py_True or Py_False, respectively */
#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False

/* Function to return a bool from a C long */
PyAPI_FUNC(PyObject *) PyBool_FromLong(long);

#ifdef __cplusplus
}
#endif
#endif /* !Py_BOOLOBJECT_H */
</code></pre>

<p>The answer is in Python headers but may not be obvious.</p>

<p>Python headers declare 2 somewhat static objects here, with couple of macros:</p>

<pre><code>/* Don't use these directly */
PyAPI_DATA(struct _longobject) _Py_FalseStruct, _Py_TrueStruct;

/* Use these macros */
#define Py_False ((PyObject *) &amp;_Py_FalseStruct)
#define Py_True ((PyObject *) &amp;_Py_TrueStruct)

/* Macros for returning Py_True or Py_False, respectively */
#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
</code></pre>

<p>It seems that both <code>True</code> and <code>False</code> are in fact Python objects and all values in Python that are <code>True</code> or <code>False</code> are in fact references to these two global <code>Py_True</code> and <code>Py_False</code> objects. When such object is returned using <code>Py_RETURN_TRUE</code>, the reference count is incremented.</p>

<p>This means that every C pointer that points to PyObject of value <code>Py_True</code> in fact points to same memory address. Therefore checking if <code>PyObject</code> is true or false is as simple as:</p>

<pre><code>/* here I create Python boolean with value of True */
PyObject *b = Py_RETURN_TRUE;
/* now we compare the address of pointer with address of Py_True */
if (b == Py_True)
{ /* it's true! */ }
else
{ /* it's false */ }
</code></pre>

<p>It's generally good idea to use <code>int PyBool_Check(PyObject*)</code> to verify if object in question is Python boolean.</p>

python c