Re: Passing pointer to array from C to Python..and modifying same array in python?

Discussion in 'Python' started by J W, Jul 14, 2003.

  1. J W

    J W Guest

    Hi David (and all membors of this list):

    Thank you so much for your advice.

    I have a few follow-up questions.

    1) when you say to "have the C function construct a
    new python object (i.e. the "wrapper")",
    what type of python object would it be:
    PyLong, PyObject, PyCobject (not exactly sure what
    this is, but i've seen this in literature), or
    something else?

    2) When you say "
    > The get method should take self and the array
    > index as paramaters and return the values in that

    slot of the array",

    Do I use the "PyArg_ParseTuple" funciton call in the
    "get()" method? If so, what parameters am I using to
    indicate object type?
    Is it correct to assume that the keyword "_self_" is
    actually seen in Python, and not the C extension
    In the C- extension function, what type of object is
    "_self_" -- PyLong, PyObject?

    Thanks again

    --- David Jeske <> wrote:
    > On Fri, Jul 11, 2003 at 02:06:10PM -0700, JW wrote:
    > > I have an array (presumably 'large') that is

    > mallocced in a C
    > > function and its values are initialized in that

    > same function. I
    > > would like to pass just the pointer to the

    > beginning of the array
    > > from this C function to a Pyton function. Then

    > the python method
    > > can individually access and MODIFY individual

    > members of the array.
    > > After the call to the python method, the C

    > function will see any
    > > changes to the array.

    > > Can this be done?

    > Of course this can be done, and there are a number
    > of ways to do
    > it. Based on your implication that there are many
    > more values put in
    > the array by the C code than will be accessed (or
    > changed) by the
    > Python code, this method might make sense:
    > 1) have the C function construct a new python object
    > (i.e. the "wrapper")
    > 2) put the real pointer to the C array into the
    > wrapper as instance data
    > 3) register two methods for the object "get()" and
    > "set()", impelemented
    > in C as Python exported functions.
    > The get method should take self and the array
    > index as paramaters and
    > return the values in that slot of the array
    > The set method should take self, an index, and
    > the values as paramaters
    > and it should modify the real "C-version" of the
    > array.
    > 4) when you execute your python function, it will be
    > given this
    > "wrapper" object as a paramater. It can then call
    > into the methods in
    > the wrapper object to access and change the
    > C-array "in-place" without
    > converting it at all. Each new value which is set
    > into the array
    > is converted on the fly.
    > 5) If you like, you can convert get() and set() to
    > __get__() and __set__
    > so you can use array access syntax to access the
    > array from python.
    > --
    > David Jeske (N9LCA) + +

    Do you Yahoo!?
    SBC Yahoo! DSL - Now only $29.95 per month!
    J W, Jul 14, 2003
    1. Advertisements

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. JW
    David Jeske
    Jul 12, 2003
  2. entitledX
    Old Wolf
    Jan 12, 2006
  3. jimjim
    Jordan Abel
    Mar 28, 2006
  4. PeterOut

    Passing a 2D array as a pointer to a pointer

    PeterOut, Aug 27, 2008, in forum: C Programming
    Nick Keighley
    Aug 28, 2008
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    James Kuyper
    Sep 23, 2011

Share This Page