How do I use python object in C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lixinyi.23@gmail.com

    How do I use python object in C++

    for example I have the following code:

    #include <Python.h>

    void exec_pythoncode ( int arg, char**argv )
    {
    Py_Initialize() ;
    Py_Main(argc,ar gv);
    Py_Finalize();
    }

    What I would like to know is how can I get the variables I want
    after Py_Main(argc,ar gv)

    say I have a=[1,2,'Hello World',0.1234] in the python space,
    I would like to have it as a struct in C++.

    Any sample code?
    Thanks in advance!
  • Aaron \Castironpi\ Brady

    #2
    Re: How do I use python object in C++

    On Sep 23, 7:50 pm, lixinyi...@gmai l.com wrote:
    for example I have the following code:
    >
    #include <Python.h>
    >
    void exec_pythoncode ( int arg, char**argv )
    {
        Py_Initialize() ;
        Py_Main(argc,ar gv);
        Py_Finalize();
    >
    }
    >
    What I would like to know is how can I get the variables I want
    after Py_Main(argc,ar gv)
    >
    say I have a=[1,2,'Hello World',0.1234] in the python space,
    I would like to have it as a struct in C++.
    >
    Any sample code?
    Thanks in advance!
    What do you know about the contents of 'argc' and 'argv'? If it's
    impossible with Py_Main, can you use one of the other entry points?

    If you've never manipulated PyObject* objects in C, they can be
    hairy. Even if you know 'a' is a sequence, its contents are still all
    PyObject*s, which you can access via PyList_... and PySequence_
    functions.

    Comment

    • lixinyi.23@gmail.com

      #3
      Re: How do I use python object in C++

      If the PyObject is a PyList, and all list items are strings,
      say a=['aaa','bbb','cc c']

      How can I have a
      myArray[0] = "aaa"
      myArray[1] = "bbb"
      myArray[2] = "ccc"
      in C++?

      Do I have to
      use PyModule_GetDic t() to get the dict first?
      what about the next?

      >
      What do you know about the contents of 'argc' and 'argv'?  If it's
      impossible with Py_Main, can you use one of the other entry points?
      >
      If you've never manipulated PyObject* objects in C, they can be
      hairy.  Even if you know 'a' is a sequence, its contents are still all
      PyObject*s, which you can access via PyList_... and PySequence_
      functions.

      Comment

      • Aaron \Castironpi\ Brady

        #4
        Re: How do I use python object in C++

        On Sep 23, 9:30 pm, lixinyi...@gmai l.com wrote:
        If the PyObject is a PyList, and all list items are strings,
        say a=['aaa','bbb','cc c']
        >
        How can I have a
        myArray[0] = "aaa"
        myArray[1] = "bbb"
        myArray[2] = "ccc"
        in C++?
        >
        Do I have to
        use PyModule_GetDic t() to get the dict first?
        what about the next?
        >
        >
        >
        What do you know about the contents of 'argc' and 'argv'?  If it's
        impossible with Py_Main, can you use one of the other entry points?
        >
        If you've never manipulated PyObject* objects in C, they can be
        hairy.  Even if you know 'a' is a sequence, its contents are still all
        PyObject*s, which you can access via PyList_... and PySequence_
        functions.
        >
        >
        This one writes '[aaa, bbb, ccc]' to the console in two different
        ways.

        #include <Python.h>

        int main() {
        PyObject *list, *listrepr;
        Py_Initialize() ;

        /* first */
        list= PyList_New( 3 );
        PyList_SetItem( list, 0, PyString_FromSt ring( "aaa" ) );
        PyList_SetItem( list, 1, PyString_FromSt ring( "bbb" ) );
        PyList_SetItem( list, 2, PyString_FromSt ring( "ccc" ) );

        listrepr= PyObject_Repr( list );
        printf( "%s\n", PyString_AsStri ng( listrepr ) );

        Py_DECREF( listrepr );
        Py_DECREF( list );

        /* second */
        list= Py_BuildValue( "[sss]", "aaa", "bbb", "ccc" );
        listrepr= PyObject_Repr( list );
        printf( "%s\n", PyString_AsStri ng( listrepr ) );

        Py_DECREF( listrepr );
        Py_DECREF( list );

        Py_Finalize();
        return 0;
        }

        Did you want to execute some Python code, and examine variables it
        creates?

        Comment

        • Aaron \Castironpi\ Brady

          #5
          Re: How do I use python object in C++

          On Sep 23, 11:06 pm, "Aaron \"Castironpi \" Brady"
          <castiro...@gma il.comwrote:
          On Sep 23, 9:30 pm, lixinyi...@gmai l.com wrote:
          >
          >
          >
          If the PyObject is a PyList, and all list items are strings,
          say a=['aaa','bbb','cc c']
          >
          How can I have a
          myArray[0] = "aaa"
          myArray[1] = "bbb"
          myArray[2] = "ccc"
          in C++?
          >
          Do I have to
          use PyModule_GetDic t() to get the dict first?
          what about the next?
          >
          What do you know about the contents of 'argc' and 'argv'?  If it's
          impossible with Py_Main, can you use one of the other entry points?
          >
          If you've never manipulated PyObject* objects in C, they can be
          hairy.  Even if you know 'a' is a sequence, its contents are still all
          PyObject*s, which you can access via PyList_... and PySequence_
          functions.
          >
          This one writes '[aaa, bbb, ccc]' to the console in two different
          ways.
          >
          #include <Python.h>
          >
          int main() {
              PyObject *list, *listrepr;
              Py_Initialize() ;
          >
              /* first */
              list= PyList_New( 3 );
              PyList_SetItem( list, 0, PyString_FromSt ring( "aaa" ) );
              PyList_SetItem( list, 1, PyString_FromSt ring( "bbb" ) );
              PyList_SetItem( list, 2, PyString_FromSt ring( "ccc" ) );
          >
              listrepr= PyObject_Repr( list );
              printf( "%s\n", PyString_AsStri ng( listrepr ) );
          >
              Py_DECREF( listrepr );
              Py_DECREF( list );
          >
              /* second */
              list= Py_BuildValue( "[sss]", "aaa", "bbb", "ccc" );
              listrepr= PyObject_Repr( list );
              printf( "%s\n", PyString_AsStri ng( listrepr ) );
          >
              Py_DECREF( listrepr );
              Py_DECREF( list );
          >
              Py_Finalize();
              return 0;
          >
          }
          >
          Did you want to execute some Python code, and examine variables it
          creates?
          Here's a third way:

          PyObject *list, *listrepr, *dict, *result, *name;
          /* third */
          dict= PyDict_New( );
          result= PyRun_String( "myarray= [ 'ggg', 'hhh', 'iii' ]",
          Py_file_input, dict, NULL );
          name= PyString_FromSt ring( "myarray" );
          list= PyDict_GetItem( dict, name );
          listrepr= PyObject_Repr( list );
          printf( "%s\n", PyString_AsStri ng( listrepr ) );

          Py_DECREF( dict );
          Py_DECREF( result );
          Py_DECREF( name );
          Py_DECREF( listrepr );
          Py_DECREF( list );

          PyRun_String runs a namespace, 'dict', which holds the variables.

          Comment

          • lixinyi.23@gmail.com

            #6
            Re: How do I use python object in C++

            On 9$B7n(B24$BF |(B, $B8a8e(B1:15, "Aaron \"Castironpi \" Brady" <castiro...@gma il.com>
            wrote:
            On Sep 23, 11:06 pm, "Aaron \"Castironpi \" Brady"
            >
            >
            >
            <castiro...@gma il.comwrote:
            On Sep 23, 9:30 pm, lixinyi...@gmai l.com wrote:
            >
            If the PyObject is a PyList, and all list items are strings,
            say a=['aaa','bbb','cc c']
            >
            How can I have a
            myArray[0] = "aaa"
            myArray[1] = "bbb"
            myArray[2] = "ccc"
            in C++?
            >
            Do I have to
            use PyModule_GetDic t() to get the dict first?
            what about the next?
            >
            What do you know about the contents of 'argc' and 'argv'? If it's
            impossible with Py_Main, can you use one of the other entry points?
            >
            If you've never manipulated PyObject* objects in C, they can be
            hairy. Even if you know 'a' is a sequence, its contents are still all
            PyObject*s, which you can access via PyList_... and PySequence_
            functions.
            >
            This one writes '[aaa, bbb, ccc]' to the console in two different
            ways.
            >
            #include <Python.h>
            >
            int main() {
            PyObject *list, *listrepr;
            Py_Initialize() ;
            >
            /* first */
            list= PyList_New( 3 );
            PyList_SetItem( list, 0, PyString_FromSt ring( "aaa" ) );
            PyList_SetItem( list, 1, PyString_FromSt ring( "bbb" ) );
            PyList_SetItem( list, 2, PyString_FromSt ring( "ccc" ) );
            >
            listrepr= PyObject_Repr( list );
            printf( "%s\n", PyString_AsStri ng( listrepr ) );
            >
            Py_DECREF( listrepr );
            Py_DECREF( list );
            >
            /* second */
            list= Py_BuildValue( "[sss]", "aaa", "bbb", "ccc" );
            listrepr= PyObject_Repr( list );
            printf( "%s\n", PyString_AsStri ng( listrepr ) );
            >
            Py_DECREF( listrepr );
            Py_DECREF( list );
            >
            Py_Finalize();
            return 0;
            >
            }
            >
            Did you want to execute some Python code, and examine variables it
            creates?
            >
            Here's a third way:
            >
            PyObject *list, *listrepr, *dict, *result, *name;
            /* third */
            dict= PyDict_New( );
            result= PyRun_String( "myarray= [ 'ggg', 'hhh', 'iii' ]",
            Py_file_input, dict, NULL );
            name= PyString_FromSt ring( "myarray" );
            list= PyDict_GetItem( dict, name );
            listrepr= PyObject_Repr( list );
            printf( "%s\n", PyString_AsStri ng( listrepr ) );
            >
            Py_DECREF( dict );
            Py_DECREF( result );
            Py_DECREF( name );
            Py_DECREF( listrepr );
            Py_DECREF( list );
            >
            PyRun_String runs a namespace, 'dict', which holds the variables.

            Thanks! it helps.

            I know how to read from python now.

            main_module = PyImport_AddMod ule("__main__") ;
            global_dict = PyModule_GetDic t(main_module);
            pyObject = PyDict_GetItem( global_dict,key );


            it will require lots of type checking, but it's fine to me.

            Comment

            Working...