How can I return an array from C to python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shashi59
    New Member
    • Mar 2007
    • 17

    How can I return an array from C to python

    Now i need information about interface c and python.Now i call c function from python but i dont know how to return a array from c to python
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by shashi59
    Now i need information about interface c and python.Now i call c function from python but i dont know how to return a array from c to python
    You'll have to remind me which tool you are using. Also, please post an example of what you are working on.

    Comment

    • shashi59
      New Member
      • Mar 2007
      • 17

      #3
      #include <Python.h>
      #include <Numeric/arrayobject.h>
      #include <sys/types.h>
      #include <stdio.h>
      #include <stdlib.h>

      #define IDENTITY_DOC "\
      This method simply unpacks the Array (so that it could be modified)\
      and then returns it unchanged."
      static PyObject *identity(PyObj ect *self, PyObject *args)
      {
      int i,n;
      PyObject *input;
      PyArrayObject *array;
      char *aptr;

      if (!PyArg_ParseTu ple(args, "O", &input ))
      return NULL;
      array = (PyArrayObject *) PyArray_Contigu ousFromObject(i nput, PyArray_INT, 0, 3);
      if (array == NULL)
      return NULL;

      // Compute Size of Array
      if(array->nd == 0)
      n = 1;
      else {
      n = 1;
      for(i=0;i<array->nd;i++)
      n = n * array->dimensions[i];
      }

      aptr=(char*)(ar ray->data);

      // Do Your Array Operation(s)

      // Return the result
      return PyArray_Return( array);
      }


      this is my wrapper code in c.in this code finally i was return a array (pyArray_Return (array).so how can i get the return value in python


      And also i had Python2.5

      Comment

      • shashi59
        New Member
        • Mar 2007
        • 17

        #4
        How can i return a array C to python

        Now i need information about interface c and python.Now i call c function from python but i dont know how to return a array from c to python

        this is my samplecode
        Code:
        #include <Python.h>
        #include <Numeric/arrayobject.h>
        #include <sys/types.h>
        #include <stdio.h>
        #include <stdlib.h>
        
        #define IDENTITY_DOC "\
        This method simply unpacks the Array (so that it could be modified)\
        and then returns it unchanged."
        static PyObject *identity(PyObject *self, PyObject *args)
        {
        int i,n;
        PyObject *input;
        PyArrayObject *array;
        char *aptr;
        
        if (!PyArg_ParseTuple(args, "O", &input ))
        return NULL;
        array = (PyArrayObject *) PyArray_ContiguousFromObject(input, PyArray_INT, 0, 3);
        if (array == NULL)
        return NULL;
        
        // Compute Size of Array 
        if(array->nd == 0)
        n = 1;
        else {
        n = 1;
        for(i=0;i<array->nd;i++) 
        n = n * array->dimensions[i];
        } 
        
        aptr=(char*)(array->data);
        
        // Do Your Array Operation(s)
        
        // Return the result
        return PyArray_Return(array);
        }
        this is my wrapper code in c.in this code finally i was return a array (pyArray_Return (array).so how can i get the return value in python


        And also i had Python2.5
        Last edited by bartonc; Mar 19 '07, 07:36 AM. Reason: added [code][/code] tags

        Comment

        • card
          New Member
          • Jun 2007
          • 10

          #5
          Did you ever figure this out? I'm new to the forum, but I have the answer if you still need it.

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by card
            Did you ever figure this out? I'm new to the forum, but I have the answer if you still need it.
            Hello, card. Welcome to the Python Forum on TheScripts.

            Please feel free to update any posts that may assist others in the future.
            We appreciate any contribution and hope to be able to assist you with you queries.

            Thanks for joining.

            Comment

            Working...