Thanks for all your input! With your help I have
been able to get everything working as expected. Getting the
address of a string with ctypes was a bit trickier than I
expected, but I the end the following works (if anyone would
like to know):
import
ctypes
from System
import IntPtr
addr
= ctypes.cast(ctypes.c_char_p(s), ctypes.c_void_p).value
ptr =
IntPtr.[long](addr)
That is for "str" objects. As a side note these
should be treated as read-only due to Python optimizations
with strings. For things that come from ctypes.create_string_bufffer() the
ctypes.c_char_p(s)
should be just s.
These are not read-only.
The last thing I would like to suggest to make
things more "wrist friendly" as you put it is to make the need
for __overloads__ less by making it smarter. Two examples come
to mind. First is the example above. If I try IntPtr(5) or IntPtr(5L) it complains
that "'int/long' value
cannot be converted to System.IntPtr". It seems like
it is trying to cast or something instead of call the
constructor. Or maybe since ints and longs are both ValueTypes
it can't choose which to use. Or maybe even just the fact they
use the same number of arguments.
Another example is later on when I am using Memory
objects from my code. The Memory class is abstract and has two
concrete implementations: ManagedMemory and UnmanagedMemory. I
was trying to call a method that takes either one of these
three along with any type of array of value-types. In this
case all of Python crashes when it tries to convert the
UnmanagedMemory object to an array (exception text is at the
end of this message). This is something that should be
determinable by the .NET bridge.
For now I won't be needing to write much more than
I have using Python for .NET, however it would be nice to make
the system a little smarter with overloads.
Unhandled Exception: System.NotSupportedException: Cannot
create arrays of TypedReference, ArgIterator, ByRef, or
RuntimeArgumentHandle Objects.
at System.Array.InternalCreate(Void* elementType,
Int32 rank, Int32* pLengths, Int32* pLowerBounds)
at System.Array.CreateInstance(Type elementType, Int32
length)
at Python.Runtime.Converter.ToArray(IntPtr value, Type
obType, Object& result, Boolean setError) in
D:\Users\Barton\Documents\Visual Studio
2010\Projects\PySharp\trunk\pythonnet\src\runtime\converter.cs:line
648
at Python.Runtime.Converter.ToManagedValue(IntPtr
value, Type obType, Object& result, Boolean set Error)
in D:\Users\Barton\Documents\Visual Studio
2010\Projects\PySharp\trunk\pythonnet\src\runtime\converter.cs:line
263
at Python.Runtime.Converter.ToManaged(IntPtr value,
Type type, Object& result, Boolean setError) in
D:\Users\Barton\Documents\Visual Studio
2010\Projects\PySharp\trunk\pythonnet\src\runtime\converter.cs:line
201
at Python.Runtime.MethodBinder.Bind(IntPtr inst,
IntPtr args, IntPtr kw, MethodBase info, MethodInfo[]
methodinfo) in D:\Users\Barton\Documents\Visual Studio
2010\Projects\PySharp\trunk\pythonnet\src\runtime\methodbinder.cs:line
278
at Python.Runtime.MethodBinder.Invoke(IntPtr inst,
IntPtr args, IntPtr kw, MethodBase info, MethodInfo[]
methodinfo) in D:\Users\Barton\Documents\Visual Studio
2010\Projects\PySharp\trunk\pythonnet\src\runtime\methodbinder.cs:line
340
at Python.Runtime.MethodObject.Invoke(IntPtr target,
IntPtr args, IntPtr kw, MethodBase info)
in D:\Users\Barton\Documents\Visual Studio
2010\Projects\PySharp\trunk\pythonnet\src\runtime\methodobject.cs:line
63
at Python.Runtime.MethodBinding.tp_call(IntPtr ob,
IntPtr args, IntPtr kw) in D:\Users\Barton\Documents\Visual
Studio
2010\Projects\PySharp\trunk\pythonnet\src\runtime\methodbinding.cs:line
135