Help with chaos math extensions.

Fredrik Lundh fredrik at pythonware.com
Wed Oct 5 15:16:50 CEST 2005


Brandon K" wrote:

> Here's the Script it was being used in (forgive it if it seems a bit
> messy, i have been tinkering with variables and such to try different
> ideas and haven't really cleaned it up).

the C compiler does in fact provide you with some clues:

c:\test\c_lib.cpp(22) : warning C4700: local variable 'c' used without having been initialized

here's the culprit:

    Py_complex *c;
    Complex* cplx;
    if(!PyArg_ParseTuple(args,"D",c)) return NULL;

PyArg_ParseTuple expects a pointer to a Py_complex object, but you're
passing in an initialized pointer.  this works better:

    Py_complex c;
    Complex* cplx;
    if(!PyArg_ParseTuple(args,"D",&c))
        return NULL;
    cplx = new Complex(&c);
    return Py_BuildValue("D",cplx->toOld());

here's the next warning:

c:\test\complex.cpp(50) : warning C4700: local variable 'c' used without having been initialized

this is your toOld function, which has a similar problem:

    Py_complex* Complex::toOld(void)
    {
        Py_complex* c;
        c->real = this->real;
        c->imag = this->imag;
        return c;
    }

since c isn't initialized, you'll overwrite some random memory locations when
you assign things to it.  here's a brute-force hack:

Py_complex* Complex::toOld(void)
{
    static Py_complex c;
    c.real = this->real;
    c.imag = this->imag;
    return &c;
}

here's a better fix:

Py_complex Complex::toOld(void)
{
    Py_complex c;
    c.real = this->real;
    c.imag = this->imag;
    return c;
}

which requires you to change

 return Py_BuildValue("D",cplx->toOld());

to

 return Py_BuildValue("D",&cplx->toOld());

I only got this far before I ran out of time; there might be more bugs in there.

</F> 






More information about the Python-list mailing list