[Python.NET] Memory Type Problems

Jeffrey Bush jeff at coderforlife.com
Tue Jan 8 09:31:21 CET 2013

How about my other questions? Pass as a pointer (like ctypes would do)?
Convert that pointer back? And I don't seem to be able to get the slices
(":") to work with byte arrays.

Thanks for the help getting the byte arrays started though. I am able to
take the output from reading a file and send it right to the array

import clr
from System import Array, Byte
with open("file.txt", 'rb') as f: input = f.read()
a = Array[Byte](input)

One problem though is that equality doesn't work (a==input is False). But
maybe this is because one is a list of (unsigned) bytes and one is a list
of (signed) characters. a[0] and input[0] display differently.


On Sat, Jan 5, 2013 at 2:07 PM, Barton <barton at bcdesignswell.com> wrote:

>  Definitely more useful:
> >>> import clr
> >>> from System import Array, Byte
> >>> g = (0 for i in range(10))
> >>> a = Array[Byte](list(i for i in g))
> >>> a[5]
> 0
> On 01/04/2013 06:33 PM, Jeffrey Bush wrote:
> 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
> _________________________________________________
> Python.NET mailing list - PythonDotNet at python.orghttp://mail.python.org/mailman/listinfo/pythondotnet
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20130108/b027ff9f/attachment.html>

More information about the PythonDotNet mailing list