How to transfer data structure or class from Python to C/C++?

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

    How to transfer data structure or class from Python to C/C++?

    Hi friends,

    I am a newer of Python. I want to ask below question:

    I have a C/C++ application and I want to use Python as its extension.
    To do that, I have to transfer some data structure from C/C++
    application to Python and get some data structure from Python to C/C++
    application. I have researched Python document, but the example only
    describes how to transfer some simple data, such as integer,char,
    etc.

    Could you please guide me to do that? or tell me some document to have
    a research?

    Thanks.
  • bearophileHUGS@lycos.com

    #2
    Re: How to transfer data structure or class from Python to C/C++?

    Hongtian:
    Could you please guide me to do that? or tell me some document to have
    a research?
    You can start googling for:
    - SWIG
    - Boost.Python
    - SIP
    - ctypes (built-in module)
    - And more.

    Bye,
    bearophile

    Comment

    • Aaron \Castironpi\ Brady

      #3
      Re: How to transfer data structure or class from Python to C/C++?

      On Oct 15, 8:08 pm, Hongtian <hongtian.i...@ gmail.comwrote:
      Hi friends,
      >
      I am a newer of Python. I want to ask below question:
      >
      I have a C/C++ application and I want to use Python as its extension.
      To do that, I have to transfer some data structure from C/C++
      application to Python and get some data structure from Python to C/C++
      application. I have researched Python document, but the example only
      describes how to transfer some simple data, such as integer,char,
      etc.
      >
      Could you please guide me to do that? or tell me some document to have
      a research?
      >
      Thanks.
      Take this for what it's worth. If I understand correctly, you want
      this:

      struct info {
      char* name;
      char* address;
      int age;
      };

      int main( ) {
      info A, B;
      python_run( "\
      from urllib import urlget\n\
      from mylib import populate_struct \n\
      page= urlget( 'http://something' )\n\
      populate_struct ( page, A )\n\
      populate_struct ( page, B )\n" );
      if( A.ageB.age ) {
      something_in_C( );
      }
      return 0;
      }

      Am I on the right track? Do you have any questions so far?

      Comment

      • Hongtian

        #4
        Re: How to transfer data structure or class from Python to C/C++?

        Not exactly.

        In my C/C++ application, I have following function or flow:

        void func1(....)
        {
        call PyFunc(struct Tdemo, struct &Tdemo1);
        }

        I mean I want to invoke Python function 'PyFunc' and transfer a data
        structure 'Tdemo' to this function. After some process in Python, I
        want it return 'Tdemo1' back to the C/C++ application.

        I research boost.python and think it is not a reasonable solution
        because it make the C/C++ application too complex.

        Thanks.

        On Oct 16, 12:09 pm, "Aaron \"Castironpi \" Brady"
        <castiro...@gma il.comwrote:
        On Oct 15, 8:08 pm, Hongtian <hongtian.i...@ gmail.comwrote:
        >
        Hi friends,
        >
        I am a newer of Python. I want to ask below question:
        >
        I have a C/C++ application and I want to use Python as its extension.
        To do that, I have to transfer some data structure from C/C++
        application to Python and get some data structure from Python to C/C++
        application. I have researched Python document, but the example only
        describes how to transfer some simple data, such as integer,char,
        etc.
        >
        Could you please guide me to do that? or tell me some document to have
        a research?
        >
        Thanks.
        >
        Take this for what it's worth.  If I understand correctly, you want
        this:
        >
        struct info {
          char* name;
          char* address;
          int age;
        >
        };
        >
        int main( ) {
          info A, B;
          python_run( "\
        from urllib import urlget\n\
        from mylib import populate_struct \n\
        page= urlget( 'http://something')\n\
        populate_struct ( page, A )\n\
        populate_struct ( page, B )\n" );
          if( A.ageB.age ) {
            something_in_C( );
          }
          return 0;
        >
        }
        >
        Am I on the right track?  Do you have any questions so far?

        Comment

        • Aaron \Castironpi\ Brady

          #5
          Re: How to transfer data structure or class from Python to C/C++?

          On Oct 16, 9:10 am, Hongtian <hongtian.i...@ gmail.comwrote:
          Not exactly.
          >
          In my C/C++ application, I have following function or flow:
          >
          void func1(....)
          {
              call PyFunc(struct Tdemo, struct &Tdemo1);
          >
          }
          >
          I mean I want to invoke Python function 'PyFunc' and transfer a data
          structure 'Tdemo' to this function. After some process in Python, I
          want it return 'Tdemo1' back to the C/C++ application.
          >
          I research boost.python and think it is not a reasonable solution
          because it make the C/C++ application too complex.
          >
          Thanks.
          I am stumped. Here's what I have.

          /C file:

          typedef struct {
          int a;
          float b;
          } TypeA;

          static PyObject *
          methA(PyObject *self, PyObject *args) {
          TypeA a;
          TypeA b;
          PyObject* fun;
          PyObject* res;
          TypeA* pa;
          TypeA* pb;

          PyArg_ParseTupl e( args, "O", &fun );
          a.a= 10;
          a.b= 20.5;
          b.a= 30;
          b.b= 40.5;
          printf( "%p %p\n", &a, &b );

          pa= &a;
          pb= &b;

          res= PyObject_CallFu nction( fun, "II", &pa, &pb );
          Py_DECREF( res );

          return PyInt_FromLong( 0 );
          }

          /Py file:

          import ng27ext

          import ctypes as c
          class TypeA( c.Structure ):
          _fields_= [
          ( 'a', c.c_int ),
          ( 'b', c.c_float )
          ]

          def exposed( obj1, obj2 ):
          print 'in exposed'
          print hex( obj1 ), hex( obj2 )
          a= c.POINTER( TypeA ).from_address( obj1 )
          print a
          print a.contents

          print ng27ext.methA( exposed )

          /Output:

          0021FD48 0021FD40
          in exposed
          0x21fd48 0x21fd40
          <ctypes.LP_Type A object at 0x009FF350>
          <__main__.Typ eA object at 0x009FF4E0>
          0

          Which is unexpected. The address on line 4 should be the contents of
          'obj1', 0x21fd48, which it's not. I must not be using 'from_address'
          properly.

          Comment

          • Aaron \Castironpi\ Brady

            #6
            Re: How to transfer data structure or class from Python to C/C++?

            On Oct 16, 9:10 am, Hongtian <hongtian.i...@ gmail.comwrote:
            Not exactly.
            >
            In my C/C++ application, I have following function or flow:
            >
            void func1(....)
            {
                call PyFunc(struct Tdemo, struct &Tdemo1);
            >
            }
            >
            I mean I want to invoke Python function 'PyFunc' and transfer a data
            structure 'Tdemo' to this function. After some process in Python, I
            want it return 'Tdemo1' back to the C/C++ application.
            >
            I research boost.python and think it is not a reasonable solution
            because it make the C/C++ application too complex.
            >
            Thanks.
            >
            On Oct 16, 12:09 pm, "Aaron \"Castironpi \" Brady"
            >
            <castiro...@gma il.comwrote:
            On Oct 15, 8:08 pm, Hongtian <hongtian.i...@ gmail.comwrote:
            >
            Hi friends,
            >
            I am a newer of Python. I want to ask below question:
            >
            I have a C/C++ application and I want to use Python as its extension.
            To do that, I have to transfer some data structure from C/C++
            application to Python and get some data structure from Python to C/C++
            application. I have researched Python document, but the example only
            describes how to transfer some simple data, such as integer,char,
            etc.
            >
            Could you please guide me to do that? or tell me some document to have
            a research?
            >
            Thanks.
            Did you have any luck?

            Comment

            • Aaron \Castironpi\ Brady

              #7
              Re: How to transfer data structure or class from Python to C/C++?

              On Oct 16, 9:10 am, Hongtian <hongtian.i...@ gmail.comwrote:
              Not exactly.
              >
              In my C/C++ application, I have following function or flow:
              >
              void func1(....)
              {
              call PyFunc(struct Tdemo, struct &Tdemo1);
              >
              }
              >
              I mean I want to invoke Python function 'PyFunc' and transfer a data
              structure 'Tdemo' to this function. After some process in Python, I
              want it return 'Tdemo1' back to the C/C++ application.
              >
              I research boost.python and think it is not a reasonable solution
              because it make the C/C++ application too complex.
              >
              Thanks.
              >
              On Oct 16, 12:09 pm, "Aaron \"Castironpi \" Brady"
              >
              <castiro...@gma il.comwrote:
              On Oct 15, 8:08 pm, Hongtian <hongtian.i...@ gmail.comwrote:
              >
              Hi friends,
              >
              I am a newer of Python. I want to ask below question:
              >
              I have a C/C++ application and I want to use Python as its extension.
              To do that, I have to transfer some data structure from C/C++
              application to Python and get some data structure from Python to C/C++
              application. I have researched Python document, but the example only
              describes how to transfer some simple data, such as integer,char,
              etc.
              >
              Could you please guide me to do that? or tell me some document to have
              a research?
              >
              Thanks.
              Did you have any luck?

              Comment

              • Aaron \Castironpi\ Brady

                #8
                Re: How to transfer data structure or class from Python to C/C++?

                On Oct 16, 9:10 am, Hongtian <hongtian.i...@ gmail.comwrote:
                Not exactly.
                >
                In my C/C++ application, I have following function or flow:
                >
                void func1(....)
                {
                    call PyFunc(struct Tdemo, struct &Tdemo1);
                >
                }
                >
                I mean I want to invoke Python function 'PyFunc' and transfer a data
                structure 'Tdemo' to this function. After some process in Python, I
                want it return 'Tdemo1' back to the C/C++ application.
                >
                I research boost.python and think it is not a reasonable solution
                because it make the C/C++ application too complex.
                >
                Thanks.
                snip

                Solution produced here. Includes dirty kludge, which will welcome
                correction.

                /C file:

                #include <Python.h>

                typedef struct {
                int a;
                float b;
                } TypeA;

                static PyObject *
                methA(PyObject *self, PyObject *args) {
                TypeA a;
                TypeA b;
                PyObject* fun;
                PyObject* res;

                PyArg_ParseTupl e( args, "O", &fun );
                a.a= 10;
                a.b= 20.5;

                res= PyObject_CallFu nction( fun, "II", &a, &b );

                printf( "%i %f\n", b.a, b.b );
                Py_DECREF( res );

                return PyInt_FromLong( 0 );
                }

                static PyMethodDef module_methods[] = {
                {"methA", methA, METH_VARARGS, "No doc"},
                {NULL, NULL, 0, NULL} /* Sentinel */
                };


                #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
                #define PyMODINIT_FUNC void
                #endif
                PyMODINIT_FUNC
                initng27ext(voi d)
                {
                PyObject* m;
                m = Py_InitModule3( "ng27ext", module_methods,
                "Custom.");
                if (m == NULL)
                return;
                }

                /Py file:

                import ng27ext

                import ctypes as c
                class TypeA( c.Structure ):
                _fields_= [
                ( 'a', c.c_int ),
                ( 'b', c.c_float )
                ]

                from _ctypes import _cast_addr
                _data_cast= c.PYFUNCTYPE( c.py_object, c.c_void_p, c.py_object,
                c.py_object)( _cast_addr ) #dirty kludge

                def exposed( obj1, obj2 ):
                cob1= _data_cast( obj1, None, c.POINTER( TypeA ) )
                cob2= _data_cast( obj2, None, c.POINTER( TypeA ) )
                print cob1.contents.a , cob1.contents.b
                cob2.contents.a = c.c_int( 60 )
                cob2.contents.b = c.c_float( 70.5 )
                print cob2.contents.a , cob2.contents.b

                print ng27ext.methA( exposed )

                /Compile & link:

                c:/programs/mingw/bin/gcc ng27ext.c -c -Ic:/programs/python25/include
                c:/programs/mingw/bin/gcc -shared ng27ext.o -o ng27ext.pyd -Lc:/
                programs/python25/libs -lpython25

                /Output:

                10 20.5
                60 70.5
                60 70.500000
                0
                Press any key to continue . . .

                Comment

                • Gabriel Genellina

                  #9
                  Re: How to transfer data structure or class from Python to C/C++?

                  En Fri, 17 Oct 2008 20:03:44 -0300, Aaron "Castironpi " Brady
                  <castironpi@gma il.comescribió:
                  On Oct 16, 9:10 am, Hongtian <hongtian.i...@ gmail.comwrote:
                  >Not exactly.
                  >>
                  >In my C/C++ application, I have following function or flow:
                  >>
                  >void func1(....)
                  >{
                  >    call PyFunc(struct Tdemo, struct &Tdemo1);
                  >>
                  >}
                  >>
                  >I mean I want to invoke Python function 'PyFunc' and transfer a data
                  >structure 'Tdemo' to this function. After some process in Python, I
                  >want it return 'Tdemo1' back to the C/C++ application.
                  >>
                  >I research boost.python and think it is not a reasonable solution
                  >because it make the C/C++ application too complex.
                  >>
                  >Thanks.
                  snip
                  >
                  Solution produced here. Includes dirty kludge, which will welcome
                  correction.
                  >
                  /C file:
                  >
                  #include <Python.h>
                  >
                  typedef struct {
                  int a;
                  float b;
                  } TypeA;
                  >
                  static PyObject *
                  methA(PyObject *self, PyObject *args) {
                  TypeA a;
                  TypeA b;
                  PyObject* fun;
                  PyObject* res;
                  >
                  PyArg_ParseTupl e( args, "O", &fun );
                  a.a= 10;
                  a.b= 20.5;
                  >
                  res= PyObject_CallFu nction( fun, "II", &a, &b );
                  >
                  printf( "%i %f\n", b.a, b.b );
                  Py_DECREF( res );
                  >
                  return PyInt_FromLong( 0 );
                  }
                  >
                  static PyMethodDef module_methods[] = {
                  {"methA", methA, METH_VARARGS, "No doc"},
                  {NULL, NULL, 0, NULL} /* Sentinel */
                  };
                  >
                  >
                  #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
                  #define PyMODINIT_FUNC void
                  #endif
                  PyMODINIT_FUNC
                  initng27ext(voi d)
                  {
                  PyObject* m;
                  m = Py_InitModule3( "ng27ext", module_methods,
                  "Custom.");
                  if (m == NULL)
                  return;
                  }
                  >
                  /Py file:
                  >
                  import ng27ext
                  >
                  import ctypes as c
                  class TypeA( c.Structure ):
                  _fields_= [
                  ( 'a', c.c_int ),
                  ( 'b', c.c_float )
                  ]
                  >
                  from _ctypes import _cast_addr
                  _data_cast= c.PYFUNCTYPE( c.py_object, c.c_void_p, c.py_object,
                  c.py_object)( _cast_addr ) #dirty kludge
                  >
                  def exposed( obj1, obj2 ):
                  cob1= _data_cast( obj1, None, c.POINTER( TypeA ) )
                  cob2= _data_cast( obj2, None, c.POINTER( TypeA ) )
                  print cob1.contents.a , cob1.contents.b
                  cob2.contents.a = c.c_int( 60 )
                  cob2.contents.b = c.c_float( 70.5 )
                  print cob2.contents.a , cob2.contents.b
                  >
                  print ng27ext.methA( exposed )
                  >
                  /Compile & link:
                  >
                  c:/programs/mingw/bin/gcc ng27ext.c -c -Ic:/programs/python25/include
                  c:/programs/mingw/bin/gcc -shared ng27ext.o -o ng27ext.pyd -Lc:/
                  programs/python25/libs -lpython25
                  >
                  /Output:
                  >
                  10 20.5
                  60 70.5
                  60 70.500000
                  0
                  Press any key to continue . . .
                  >
                  --

                  >


                  --
                  Gabriel Genellina

                  Comment

                  • Aaron Brady

                    #10
                    Re: How to transfer data structure or class from Python to C/C++?

                    On Oct 21, 12:46 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
                    wrote:
                    En Fri, 17 Oct 2008 20:03:44 -0300, Aaron "Castironpi " Brady  
                    <castiro...@gma il.comescribió:
                    >
                    >
                    >
                    On Oct 16, 9:10 am, Hongtian <hongtian.i...@ gmail.comwrote:
                    Not exactly.
                    >
                    In my C/C++ application, I have following function or flow:
                    >
                    void func1(....)
                    {
                        call PyFunc(struct Tdemo, struct &Tdemo1);
                    >
                    }
                    >
                    I mean I want to invoke Python function 'PyFunc' and transfer a data
                    structure 'Tdemo' to this function. After some process in Python, I
                    want it return 'Tdemo1' back to the C/C++ application.
                    >
                    I research boost.python and think it is not a reasonable solution
                    because it make the C/C++ application too complex.
                    >
                    Thanks.
                    snip
                    >
                    Solution produced here.  Includes dirty kludge, which will welcome
                    correction.
                    snip
                    >
                    --
                    Gabriel Genellina
                    Hi Gabriel,
                    Sorry, did you have some questions about it?

                    Comment

                    • Gabriel Genellina

                      #11
                      Re: How to transfer data structure or class from Python to C/C++?

                      En Tue, 21 Oct 2008 15:21:45 -0200, Aaron Brady <castironpi@gma il.com>
                      escribió:
                      On Oct 21, 12:46 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
                      wrote:
                      >
                      Hi Gabriel,
                      Sorry, did you have some questions about it?
                      No, that empty message was posted by mistake, sorry.

                      --
                      Gabriel Genellina

                      Comment

                      Working...