[Python.NET] Memory Type Problems

Jeffrey Bush jeff at coderforlife.com
Sat Jan 5 03:33:18 CET 2013


Hi,

I have a library in C for compression. I made Python script for testing it,
called using ctypes.

Now, I am working on a .NET library for something very similar and would
like to test it in the same way. However, I am getting lost in how to do
some of the things with your library.

The function I need to call looks like:

public static long Compress(Memory input, Memory output)

The Memory class is a wrapper around either managed byte arrays, void*
(for efficiency), or memory mapped files. This allows a unified system for
accessing general chunks of memory regardless if they are managed, native,
or on the filesystem. They are created through static functions:

public static Memory From(MemoryStream s, bool all = true)public
static Memory From(byte[] b, bool readOnly = false)public static
Memory From(byte[] b, long offset, long size, bool readOnly =
false)public unsafe static Memory From(UnmanagedMemoryStream s, bool
all = true)public unsafe static Memory From(void* b, long size, bool
readOnly = false)public unsafe static Memory From(IntPtr b, long size,
bool readOnly = false)public static Memory FromFile(string file, bool
readOnly = false)public static Memory OfSize(long size)

The only ones I can easily use are the last two (OfSize and FromFile). I
have not yet figured out how to call the other ones properly. I don't seem
to be able to allocate byte arrays with Array[Byte](10), it complains that
10 cannot be converted to System.Byte[] so it seems that it believes I am
casting instead of creating a new array. No overloads seem to exist.

So now here are my questions and the ctypes answer:

   - How do I construct a byte array? (ctypes.create_string_buffer)
   - How do I take a Python list/array and pass it as a pointer? (ctypes
   has this conversion happen automatically)
   - How do I turn a byte array into a useful Python list/array? As-is I
   can't use the Python-style indexers (except negative numbers). (The ctypes
   buffer works directly as a list, but also supports ".raw" and ".value")
   - How do I convert a pointer into a useful Python list/array? (ctypes
   casting allows this to work)

Last point, although this is probably a limitation of .NET, but just to
make sure. The default argument values can't be used, but it is possible
that this isn't even in the assembly information and only works for source
in the same module.

Thanks,
Jeff
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20130104/2eee5535/attachment.html>


More information about the PythonDotNet mailing list