memcpy

Tim tec at knology.net
Tue Sep 11 15:22:19 CEST 2007


On Sep 11, 8:01 am, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> On Tue, 11 Sep 2007 05:09:58 -0700, Tim wrote:
> > On Sep 10, 3:31 pm, Marc 'BlackJack' Rintsch <bj_... at gmx.net> 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[i] 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]







More information about the Python-list mailing list