Problem generating functions from C strings

Discussion in 'Python' started by Ian Glover, Aug 31, 2004.

  1. Ian Glover

    Ian Glover Guest

    Hi there,

    I'm having problems trying to link C++ and Python. What I want to do is have the C provide a function from a string and then run the function. What I've currently got is

    PyObject* lpyModuleName = PyString_FromString( "__main__" ); // New Ref.
    assert( lpyModuleName );
    PyObject* lpyMain = PyImport_Import( lpyModuleName ); // New Ref.
    assert( lpyMain );
    PyObject* lpyDict = PyModule_GetDict( lpyMain ); // Borrowed.
    assert( lpyDict );
    PyObject* lpyLocals = PyDict_New();

    std::string lCode =
    "import config\n"
    "def PyOperator(parameter):\n"
    " v1 = config.GetHardware(\"valve\", 1)\n"
    " v2 = config.GetHardware(\"valve\", 2)\n"
    " v1.set_position( 1 )\n"
    " v2.get_position( 1 )\n";

    PyRun_String( lCode.c_str(), Py_file_input, lpyDict, lpyLocals );


    PyObject* lpyFuncName = PyString_FromString( "PyOperator" ); // New Ref.
    assert( lpyFuncName );
    PyObject* lpyFunction = PyDict_GetItem( lpyDict, lpyFuncName ); // Borrowed.

    // *** This assertion fails ***
    assert( lpyFunction );
    // ****************************

    assert( PyCallable_Check( lpyFunction ) );

    PyObject* lpyArguments = Py_BuildValue("(d)", 2);
    assert( lpyArguments );
    PyObject* lpyResult = PyEval_CallObject( lpyFunction, lpyArguments );

    Py_DECREF( lpyResult );
    Py_DECREF( lpyArguments );
    Py_DECREF( lpyFuncName );
    Py_DECREF( lpyMain );
    Py_DECREF( lpyModuleName );
    Py_DECREF( lpyLocals );


    This is in a C++ extension module that and being called via another function that's being exposed. It seems that the function isn't get put in the scope I'd expect. And indeed if I comment out the lines that try and get it so that the assert failure doesn't occur and I can poke around the scopes via dir() I can't see it. So where is it being put? or am I doing this very wrong?

    Thanks

    Ian
     
    Ian Glover, Aug 31, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,659
    Victor Bazarov
    Jun 21, 2004
  2. Eddie

    generating strings

    Eddie, Aug 25, 2004, in forum: C++
    Replies:
    7
    Views:
    539
    Karthiik Kumar
    Aug 28, 2004
  3. Eddie

    generating strings

    Eddie, Aug 25, 2004, in forum: C Programming
    Replies:
    3
    Views:
    336
    Eric Sosman
    Aug 26, 2004
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    810
    Malcolm
    Jun 24, 2006
  5. Replies:
    7
    Views:
    524
    Jim Langston
    Jul 13, 2005
Loading...

Share This Page