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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Donnie Leen

    #1

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

    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_SimpleStr ing( "import mymodule" );
    while ( 1 )
    {
    // error occur after 398 loops here
    PyRun_SimpleStr ing( "mymodule.f un( 'testest' )" );
    }

    Py_Finalize();
    }



  • Thomas Heller

    #2
    Re: error on crude test of embedding python in c++ HELP PLEASE

    "Donnie Leen" <Kingdom.Lin@ye ah.net> writes:
    [color=blue]
    > 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[/color]

    You forgot an Py_INCREF here:
    Py_INCREF(Py_No ne);
    return Py_None; // do nothing
    [color=blue]
    > }
    > 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_SimpleStr ing( "import mymodule" );
    > while ( 1 )
    > {
    > // error occur after 398 loops here
    > PyRun_SimpleStr ing( "mymodule.f un( 'testest' )" );
    > }
    >
    > Py_Finalize();
    > }[/color]

    Comment

    • Donnie Leen

      #3
      Re: error on crude test of embedding python in c++ HELP PLEASE

      Got it, thanks :)

      Comment

      Working...