memcpy

Discussion in 'Python' started by Tim, Sep 10, 2007.

  1. Tim

    Tim Guest

    How do I memcpy from a pointer to an array of floats in python?

    I get errors: NameError: global name 'row' is not defined

    I want to be able to get the row array element. In C I would
    normally place the address of row as the first argument.

    cdll.msvcrt.memcpy( row, pData, 256 )


    If I define row as the following I also get the following error:

    row = ones( TOTAL_PARAMETER_ENTRIES, dtype=float )

    ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know
    how to convert parameter 1

    Thanks
    Tim, Sep 10, 2007
    #1
    1. Advertising

  2. On Mon, 10 Sep 2007 11:38:50 -0700, Tim wrote:

    > How do I memcpy from a pointer to an array of floats in python?
    >
    > I get errors: NameError: global name 'row' is not defined


    Well than the (global) name `row` is not defined. Quite clear message,
    isn't it? ;-)

    > I want to be able to get the row array element. In C I would
    > normally place the address of row as the first argument.
    >
    > cdll.msvcrt.memcpy( row, pData, 256 )
    >
    >
    > If I define row as the following I also get the following error:
    >
    > row = ones( TOTAL_PARAMETER_ENTRIES, dtype=float )
    >
    > ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know
    > how to convert parameter 1


    You don't give enough information so we have to guess. For example I
    guess the `ones()` function comes from one of the packages `numeric`,
    `numarray` or `numpy`!?

    This function returns a Python object. You can't use arbitrary Python
    objects with `ctypes`. `memcpy` expects a pointer not an object.

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Sep 10, 2007
    #2
    1. Advertising

  3. Tim

    Tim Guest

    On Sep 10, 3:31 pm, Marc 'BlackJack' Rintsch <> wrote:
    > On Mon, 10 Sep 2007 11:38:50 -0700, Tim wrote:
    > > How do I memcpy from a pointer to an array of floats in python?

    >
    > > I get errors: NameError: global name 'row' is not defined

    >
    > Well than the (global) name `row` is not defined. Quite clear message,
    > isn't it? ;-)
    >
    > > I want to be able to get the row array element. In C I would
    > > normally place the address of row as the first argument.

    >
    > > cdll.msvcrt.memcpy( row, pData, 256 )

    >
    > > If I define row as the following I also get the following error:

    >
    > > row = ones( TOTAL_PARAMETER_ENTRIES, dtype=float )

    >
    > > ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know
    > > how to convert parameter 1

    >
    > You don't give enough information so we have to guess. For example I
    > guess the `ones()` function comes from one of the packages `numeric`,
    > `numarray` or `numpy`!?
    >
    > This function returns a Python object. You can't use arbitrary Python
    > objects with `ctypes`. `memcpy` expects a pointer not an object.
    >
    > Ciao,
    > Marc 'BlackJack' Rintsch


    Can I initialize something in Python that I can get access to it's
    pointer?
    How about:

    self.data =
    TOTAL_OUTPUT_PARMETERS*[TOTAL_PARAMETER_ENTRIES*[c_float()]

    or

    self.data = TOTAL_OUTPUT_PARMETERS*[c_char_p("temp")]

    or

    self.data = [TOTAL_OUTPUT_PARMETERS*1.0]*TOTAL_PARAMETER_ENTRIES

    I need to be able to copy the contents of a pointer to shared memory
    into an array of floats so I can index into that array and see my
    data. I have a pointer to shared meory but I don't know how to access
    it.

    Here is what I would like to write:

    shared_memory_pointer = windll.kernel32.MapViewOfFile(hMapObject,
    FILE_MAP_ALL_ACCESS,
    0, 0, TABLE_SHMEMSIZE)

    memcpy( self.data, shared_memory_pointer, my_size )

    Thanks
    Tim, Sep 11, 2007
    #3
  4. Tim a écrit :
    <zip>
    > Can I initialize something in Python that I can get access to it's
    > pointer?


    No, there is no pointer in Python semantic (they exist behind the scene
    in C-Python).

    > How about:
    >
    > self.data =
    > TOTAL_OUTPUT_PARMETERS*[TOTAL_PARAMETER_ENTRIES*[c_float()]
    >
    > or
    >
    > self.data = TOTAL_OUTPUT_PARMETERS*[c_char_p("temp")]
    >
    > or
    >
    > self.data = [TOTAL_OUTPUT_PARMETERS*1.0]*TOTAL_PARAMETER_ENTRIES
    >
    > I need to be able to copy the contents of a pointer to shared memory
    > into an array of floats so I can index into that array and see my
    > data. I have a pointer to shared meory but I don't know how to access
    > it.


    See module ctypes (14.14 in library reference manual).

    A+

    Laurent.
    Laurent Pointal, Sep 11, 2007
    #4
  5. On Tue, 11 Sep 2007 05:09:58 -0700, Tim wrote:

    > On Sep 10, 3:31 pm, Marc 'BlackJack' Rintsch <> wrote:
    >> On Mon, 10 Sep 2007 11:38:50 -0700, Tim wrote:
    >> > How do I memcpy from a pointer to an array of floats in python?

    >>
    >> > I get errors: NameError: global name 'row' is not defined

    >>
    >> Well than the (global) name `row` is not defined. Quite clear message,
    >> isn't it? ;-)
    >>
    >> > I want to be able to get the row array element. In C I would
    >> > normally place the address of row as the first argument.

    >>
    >> > cdll.msvcrt.memcpy( row, pData, 256 )

    >>
    >> > If I define row as the following I also get the following error:

    >>
    >> > row = ones( TOTAL_PARAMETER_ENTRIES, dtype=float )

    >>
    >> > ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know
    >> > how to convert parameter 1

    >>
    >> You don't give enough information so we have to guess. For example I
    >> guess the `ones()` function comes from one of the packages `numeric`,
    >> `numarray` or `numpy`!?
    >>
    >> This function returns a Python object. You can't use arbitrary Python
    >> objects with `ctypes`. `memcpy` expects a pointer not an object.

    >
    > Can I initialize something in Python that I can get access to it's
    > pointer?


    "It's pointer"? Then you have a pointer to a structure that represents
    the Python object but still no idea what data is at that pointer. This is
    an implementation detail of the Python version and of the particular
    object.

    > Here is what I would like to write:
    >
    > shared_memory_pointer = windll.kernel32.MapViewOfFile(hMapObject,
    > FILE_MAP_ALL_ACCESS,
    > 0, 0, TABLE_SHMEMSIZE)
    >
    > memcpy( self.data, shared_memory_pointer, my_size )


    I haven't tested but it should be possible to declare the return type of
    `windll.kernel32.MapViewOfFile()` as ``ctypes.POINTER(ctypes.c_double *
    256)`` and then do:

    test_data = numpy.ones(1000)
    shared_memory_pointer.contents[0:256] = test_data[0:256]

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Sep 11, 2007
    #5
  6. Tim

    Tim Guest

    On Sep 11, 8:01 am, Marc 'BlackJack' Rintsch <> wrote:
    > On Tue, 11 Sep 2007 05:09:58 -0700, Tim wrote:
    > > On Sep 10, 3:31 pm, Marc 'BlackJack' Rintsch <> wrote:
    > >> On Mon, 10 Sep 2007 11:38:50 -0700, Tim wrote:
    > >> > How do I memcpy from a pointer to an array of floats in python?

    >
    > >> > I get errors: NameError: global name 'row' is not defined

    >
    > >> Well than the (global) name `row` is not defined. Quite clear message,
    > >> isn't it? ;-)

    >
    > >> > I want to be able to get the row array element. In C I would
    > >> > normally place the address of row as the first argument.

    >
    > >> > cdll.msvcrt.memcpy( row, pData, 256 )

    >
    > >> > If I define row as the following I also get the following error:

    >
    > >> > row = ones( TOTAL_PARAMETER_ENTRIES, dtype=float )

    >
    > >> > ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know
    > >> > how to convert parameter 1

    >
    > >> You don't give enough information so we have to guess. For example I
    > >> guess the `ones()` function comes from one of the packages `numeric`,
    > >> `numarray` or `numpy`!?

    >
    > >> This function returns a Python object. You can't use arbitrary Python
    > >> objects with `ctypes`. `memcpy` expects a pointer not an object.

    >
    > > Can I initialize something in Python that I can get access to it's
    > > pointer?

    >
    > "It's pointer"? Then you have a pointer to a structure that represents
    > the Python object but still no idea what data is at that pointer. This is
    > an implementation detail of the Python version and of the particular
    > object.
    >
    > > Here is what I would like to write:

    >
    > > shared_memory_pointer = windll.kernel32.MapViewOfFile(hMapObject,
    > > FILE_MAP_ALL_ACCESS,
    > > 0, 0, TABLE_SHMEMSIZE)

    >
    > > memcpy( self.data, shared_memory_pointer, my_size )

    >
    > I haven't tested but it should be possible to declare the return type of
    > `windll.kernel32.MapViewOfFile()` as ``ctypes.POINTER(ctypes.c_double *
    > 256)`` and then do:
    >
    > test_data = numpy.ones(1000)
    > shared_memory_pointer.contents[0:256] = test_data[0:256]
    >
    > Ciao,
    > Marc 'BlackJack' Rintsch- Hide quoted text -
    >
    > - Show quoted text -


    Is this what you mean? Python did not like the word c_types in front
    of POINTER. Do you know why? How can I re-declare a function's return
    type if it is declared somewhere else?

    test_data = numpy.ones(1000)
    shared_memory_pointer = POINTER(c_float*256)
    shared_memory_pointer =
    windll.kernel32.MapViewOfFile(hMapObject, FILE_MAP_ALL_ACCESS,
    0, 0, TABLE_SHMEMSIZE)
    test_data[0:256]= shared_memory_pointer.contents[0:256]

    print 'data:', test_data[0]
    Tim, Sep 11, 2007
    #6
  7. On Tue, 11 Sep 2007 06:22:19 -0700, Tim wrote:

    > Is this what you mean? Python did not like the word c_types in front
    > of POINTER. Do you know why?


    Yes I know. Read up how importing works.

    > How can I re-declare a function's return type if it is declared
    > somewhere else?


    The return type of C functions wrapped by `ctypes` is always `int` until
    you say otherwise. How to do that is covered in the `ctypes`
    tutorial/dicumentation.

    > test_data = numpy.ones(1000)
    > shared_memory_pointer = POINTER(c_float*256)


    This binds the resulting "pointer" object to the name
    `shared_memory_pointer`.

    > shared_memory_pointer =
    > windll.kernel32.MapViewOfFile(hMapObject, FILE_MAP_ALL_ACCESS,
    > 0, 0, TABLE_SHMEMSIZE)


    And here you bind a different object to that name, so the first binding
    has no effect.

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Sep 11, 2007
    #7
    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. John Edwards
    Replies:
    1
    Views:
    569
    Dave Rahardja
    Aug 7, 2003
  2. David Bradley
    Replies:
    0
    Views:
    529
    David Bradley
    Aug 7, 2003
  3. Ninan Thomas
    Replies:
    3
    Views:
    408
    Kevin Goodsell
    Aug 22, 2003
  4. Bren
    Replies:
    8
    Views:
    2,011
    Stephen Howe
    Sep 4, 2003
  5. Replies:
    13
    Views:
    17,759
    White Wolf
    Sep 18, 2003
Loading...

Share This Page