[Python.NET] Memory Type Problems

Bradley Friedman brad at fie.us
Tue Jan 8 16:19:49 CET 2013


You can construct .net intptr objects from ints.  So you need the address. You already provided the solution to that. In python, you get memory addresses by using ctypes.  Be careful to handle it in a way that will support both 32 and 64 bit binaries.

Sent from my iPad

On Jan 8, 2013, at 4:51 AM, Barton <barton at bcdesignswell.com> wrote:

> 1) Not being much of a C guy myself, I'm not sure what to make of all this pointer business.
>      Especially since the object reference is capable of being indexed (and SO much more [>>> help(a)])
>         - Just don't try a.Address(0) like I just did - got a core dump on Mono -
> 
> 2) C# type rules apply (except that python lets you try things like "a == input"); underneath Array.Equals(System.Array) is called.
>         hence Int32(7) == 7 is false.
>             - I tend to think of this package as a "wrist-friendly" (see Boo) wrapper over the Mono/.NET components more than a trying to be a type compatibility layer.
> 
> 3) Slicing seems like something that is worth implementing. That would take a lot of typing out of 
>     Array.Copy(a1, srcIndx, a2, dstIndx, len)
> 
> Great fun,
> Barton
> 
> On 01/08/2013 12:31 AM, Jeffrey Bush wrote:
>> 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 constructor:
>> 
>> 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.
>> 
>> Jeff 
>> 
>> 
>> 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.org
>>>> http://mail.python.org/mailman/listinfo/pythondotnet
> 
> _________________________________________________
> Python.NET mailing list - PythonDotNet at python.org
> http://mail.python.org/mailman/listinfo/pythondotnet
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20130108/cd74740a/attachment.html>


More information about the PythonDotNet mailing list