error on crude test of embedding python in c++ HELP PLEASE

D

Donnie Leen

I wrote a program to test calling c function from python code embedding in c
as following, it cause error after running a while(about 398 circle). I
test it in msvc6, python2.3, windows 2k, could anyone tell me why this
happened since i just work according to the document? Thanks first.

Donnie Leen


source code:


#include <Python.h>

static PyObject* pymyfun( PyObject* self, PyObject* args )
{
return Py_None; // do nothing
}
static PyMethodDef emb_methods[] = {
{ "fun", pymyfun, METH_VARARGS, "doc." },
{NULL, NULL, 0, NULL}
};

void main( )
{
Py_Initialize( );

PyObject* r = Py_InitModule( "mymodule", emb_methods );
PyRun_SimpleString( "import mymodule" );
while ( 1 )
{
// error occur after 398 loops here
PyRun_SimpleString( "mymodule.fun( 'testest' )" );
}

Py_Finalize();
}
 
T

Thomas Heller

Donnie Leen said:
I wrote a program to test calling c function from python code embedding in c
as following, it cause error after running a while(about 398 circle). I
test it in msvc6, python2.3, windows 2k, could anyone tell me why this
happened since i just work according to the document? Thanks first.

Donnie Leen


source code:


#include <Python.h>

static PyObject* pymyfun( PyObject* self, PyObject* args )
{
return Py_None; // do nothing

You forgot an Py_INCREF here:
Py_INCREF(Py_None);
return Py_None; // do nothing
}
static PyMethodDef emb_methods[] = {
{ "fun", pymyfun, METH_VARARGS, "doc." },
{NULL, NULL, 0, NULL}
};

void main( )
{
Py_Initialize( );

PyObject* r = Py_InitModule( "mymodule", emb_methods );
PyRun_SimpleString( "import mymodule" );
while ( 1 )
{
// error occur after 398 loops here
PyRun_SimpleString( "mymodule.fun( 'testest' )" );
}

Py_Finalize();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top