Iv noticed that theres no one liner way to get a number (double) from a PyNumber from the C api.
PyFloat_AsDouble - http://www.python.org/doc/api/floatObjects.html
- Does what I want, but only works on PyFloat (and PyInts), not user implementations of the PyNumber protocol.
PyNumber_Float - http://www.python.org/doc/api/number.html
- does what I need but, returnd a PyFloat that needs to then be passed to PyFloat_AsDouble, PyNumber_Float
Id like somthing like PyNumber_AsDouble,
thinking of writing a utility function for this that could read...
/* assumes PyNumber_Check has been tested, should not segfault either way */ double PyNumber_AsDouble(PyObject * value) { if (PyFloat_Check(value) || PyInt_Check(value)) { return PyFloat_AsDouble(value); } else { PyObject *pyfloat; double d;
pyfloat = PyNumber_Float(value);
d = PyNumber_AsDouble(pyfloat);
Py_XDECREF(pyfloat);
return d;
}
}
Using PyNumber_Float for all values would be a bit slower since it needs to make a new pyfloat each time which is why Id prefer to use PyFloat_AsDouble where possible.
Wondering if other people would fine something like this useful in the Py/C api - or if Im missing something.
Thanks
-- Campbell J Barton (ideasman42)
participants (1)
-
Campbell Barton