[C++-sig] double vs float in Py_BuildValue

Gary Robinson grobinson at goombah.com
Mon Jun 27 00:48:45 CEST 2005


I'm having a problem I think is fairly bizarre using Py_BuildValue 
(note, this code isn't presently using boost.python, it's the straight 
Python-C interface).

Here's an example c module:

> #include <Python.h>
> static float         gfSumChiSquare = 0.0; 
>     
>         
> static PyObject *    
> getSumChiSquare(PyObject *self, PyObject *args){
>     return Py_BuildValue("f", gfSumChiSquare);
>     }
>     
> static PyMethodDef SimMethods[] = {
>     {"getSumChiSquare", getSumChiSquare, METH_NOARGS, "Return 
> fSumChiSquare"},
>     {NULL, NULL, 0, NULL}        /* Sentinel */
> };
> 
> PyMODINIT_FUNC
> inittestfloat(void)
> {
>     (void) Py_InitModule("testfloat", SimMethods);
> }
> 
> 

If the float, gfSumChiSquare, is defined as a float, as above, it works 
fine.


But  if I turn it into a double:

    static double         gfSumChiSquare = 0.0; 
 ...
    static PyObject *    
    getSumChiSquare(PyObject *self, PyObject *args){
        return Py_BuildValue("d", gfSumChiSquare);
        }
 ...


I get a bus error when I call it from Python.


I'm extremely rusty in C, not having used it in about 7 years, so maybe 
that's what's responsible. But I would appreciate some insight here!

As always, in appreciation the help I find in this sig,
Gary



More information about the Cplusplus-sig mailing list