memoryview / passing string data to a C function without copying

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

memoryview / passing string data to a C function without copying

Peter Schwalm-2
Hi,

I am looking for an efficient way to pass a string to a c++ function.

In CPython this function works on binary (byte) data usually read in
from a file. Now I need an Ironpython version.

Although I am not happy with it I can live with the automatic
transformation of (binary) file data into unicode strings during reading
and writing. So I have modified my C++ function to work on wide chars
(i.e. unicode chars).

Now I am looking for a way to pass the unicode string to the c++
function efficiently, i.e. without copying or modifying it before the
call. I found memoryview and thought it is what I need. But as far as I
see it works only with byte strings.

     x2              = b"abc"                # byte string
     view2           = memoryview(x2)        # ok

     x3              = "abc"
#   the following code line results in:     # char string
#   "error: TypeError: expected IBufferProtocol, got str"
     view3           = memoryview(x3)        # error

My questions:

     Why does memory view not work here?

     Does anyone know how to pass the contents of a string to a C
function without copying it?

The context I am working with is roughly as follows:
...
import ctypes as ct
...
     self.dll = ct.cdll.LoadLibrary(dllPath)
     cObject = self.dll.ctor()
     self.dll.cFunc(cObject, source, destination, length)

where cFunc is the exported name of the c function, and cObject is the
address of a housekeeping object allocated in the C-dll via C++ new and
returned by the ctor function. Source should provide the string data to
the C function in a suitable and efficient form, and for destination I
used an "array.array("H", length).

Thanks in advance
Peter





_______________________________________________
Users mailing list
[hidden email]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Reply | Threaded
Open this post in threaded view
|

Re: memoryview / passing string data to a C functionwithout copying

Markus Schaber-6
Hi, Peter,

> Von: [hidden email] [mailto:users-
[...]
>
>      Does anyone know how to pass the contents of a string to a C
function
> without copying it?

A non-pythonic suggestion:

Maybe you could use the .NET System.FileStream to read the data into a
byte array (or a .NET memory stream), and pass that one to the cFunc?

Or you wait for IronPython 3 which has better support for byte data.

Best regards

Markus Schaber

___________________________
We software Automation.

3S-Smart Software Solutions GmbH
Markus Schaber | Developer
Memminger Str. 151 | 87439 Kempten | Germany | Tel. +49-831-54031-0 |
Fax +49-831-54031-50

Email: [hidden email] | Web: http://www.3s-software.com 
CoDeSys internet forum: http://forum.3s-software.com
Download CoDeSys sample projects:
http://www.3s-software.com/index.shtml?sample_projects

Managing Directors: Dipl.Inf. Dieter Hess, Dipl.Inf. Manfred Werner |
Trade register: Kempten HRB 6186 | Tax ID No.: DE 167014915
_______________________________________________
Users mailing list
[hidden email]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Reply | Threaded
Open this post in threaded view
|

Re: memoryview / passing string data to a C functionwithout copying

Jeff Hardy-4
On Tue, May 31, 2011 at 11:01 PM, Markus Schaber
<[hidden email]> wrote:

> Hi, Peter,
>
>> Von: [hidden email] [mailto:users-
> [...]
>>
>>      Does anyone know how to pass the contents of a string to a C
> function
>> without copying it?
>
> A non-pythonic suggestion:
>
> Maybe you could use the .NET System.FileStream to read the data into a
> byte array (or a .NET memory stream), and pass that one to the cFunc?

This is probably your best bet. file() doesn't return bytes until Python 3.

- Jeff
_______________________________________________
Users mailing list
[hidden email]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com