[capi-sig] Getting Numbers in C

Campbell Barton cbarton at metavr.com
Fri Jul 13 16:08:39 CEST 2007


Hrvoje Niksic wrote:
>> 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);
> 
> I assume this should be PyFloat_AsDouble?
> 
>> 		Py_XDECREF(pyfloat);
>> 		return d;
>> 	}
>> }
> 
> What happens if PyNumber_Float raises an exception?  As far as I can
> tell, PyNumber_AsDouble will result in a "bad argument" exception
> being raised and will return -1, losing track of the original
> exception in the process.
> 
> Come to think of it, error checking problems could very well be the
> reason why the API doesn't provide a PyNumber_AsDouble.

It is a problem but I dont think thats a good enough reason not to have 
some way to do this since its such a common operation.

PyFloat_AsDouble cant return an error value, so enforcing that the 
pyobject is checked with PyNumber_Check seems reasonable to me.

-- 
Campbell J Barton (ideasman42)


More information about the capi-sig mailing list