Add bool parser for PyArg_ParseTuple

Use for mathutils.bvhtree
This commit is contained in:
2015-08-03 20:00:16 +10:00
parent 0f690e2186
commit 42d65ef5cc
3 changed files with 34 additions and 8 deletions

View File

@@ -1024,3 +1024,21 @@ int PyC_RunString_AsNumber(const char *expr, double *value, const char *filename
return error_ret;
}
/**
* Use with PyArg_ParseTuple's "O&" formatting.
*/
int PyC_ParseBool(PyObject *o, void *p)
{
bool *bool_p = p;
long value;
if (((value = PyLong_AsLong(o)) == -1) || !ELEM(value, 0, 1)) {
PyErr_Format(PyExc_ValueError,
"expected a bool or int (0/1), got %s",
Py_TYPE(o)->tp_name);
return 0;
}
*bool_p = value ? true : false;
return 1;
}