issue disambiguating overloaded method that takes an array

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

issue disambiguating overloaded method that takes an array

Adrian Perez-11
Hello everyone,

This is probably a total n00b question, but I've been using pythonnet in our toolchain to communicate with a private c# assembly for months now without ever running into a problem I couldn't eventually figure out.  This one has me totally stumped, and I'm hoping something obvious will jump out at experienced eyes.

(I did a find/replace for secrecy, besides silly names these are the raw results I get back)

A C# class exposes a method 'WriteByte' that has 5 overloads (note that only one of them takes 2 arguments):

>>> accessor= MyCompany.SomeNamespace.SomeTypeArrayAccessor
>>> accessor.WriteByte.__overloads__
Boolean WriteByte(MyCompany.SomeNamespace.SomeType, Byte[])
Boolean WriteByte(MyCompany.SomeNamespace.SomeType, Byte[], Int32 ByRef)
Boolean WriteByte(MyCompany.SomeNamespace.SomeType, System.Collections.Generic.IEnumerable`1[System.Byte], Int32 ByRef)
Boolean WriteByte(MyCompany.SomeNamespace.SomeType, Int32, System.Collections.Generic.IEnumerable`1[System.Byte], Int32 ByRef)
Boolean WriteByte(MyCompany.SomeNamespace.SomeType, Int32, Byte[], Int32 ByRef)

I attempt to disambiguate by selecting the 2-arg method I want by type:

>>> desired_method= accessor.WriteByte.__overloads__[MyCompany.SomeNamespace.SomeType, System.Array[System.Byte]]

The problem may be right here - if I print out desired_method.__doc__ I get the same 5 methods listed above.  Anyway, then I compute my arguments, and can verify they have the types I expect (If it matters, arg_b is created by a call to System.IO.BinaryReader.ReadBytes()):

>>> arg_a= ...
>>> arg_b= ...
>>> type(arg_a)
<class 'MyCompany.SomeNamespace.SomeType'>
>>> type(arg_b)
<class 'System.Byte[]'>

However, when I try to call the disambiguated method with the correct args, I get no love:

>>> result= desired_method(arg_a, arg_b)
Traceback (most recent call last):
  File "<string>", line 1, in <fragment>
TypeError: No method matches given arguments

Does anyone see the obvious thing I'm doing wrong?  Disambiguating can be a pain but I've done it before for other methods and have never had such an impossible time getting the correct method called.

Thanks,

Adrian Perez
_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|

Re: issue disambiguating overloaded method that takes an array

Barton

def TestCharArray():
     from System import Array
     charArrType = Array[Char]
     ##arrType = charArrType('hello')
     ##for char in arrType:
     ##    print char,
     ## Also works on instances of the type! ##
     strCharArr = s.__overloads__[charArrType]
     s = strCharArr(list('hello'))
     print s

On 5/16/2011 6:29 PM, Adrian Perez wrote:

> Hello everyone,
>
> This is probably a total n00b question, but I've been using pythonnet in our toolchain to communicate with a private c# assembly for months now without ever running into a problem I couldn't eventually figure out.  This one has me totally stumped, and I'm hoping something obvious will jump out at experienced eyes.
>
> (I did a find/replace for secrecy, besides silly names these are the raw results I get back)
>
> A C# class exposes a method 'WriteByte' that has 5 overloads (note that only one of them takes 2 arguments):
>
>>>> accessor= MyCompany.SomeNamespace.SomeTypeArrayAccessor
>>>> accessor.WriteByte.__overloads__
> Boolean WriteByte(MyCompany.SomeNamespace.SomeType, Byte[])
> Boolean WriteByte(MyCompany.SomeNamespace.SomeType, Byte[], Int32 ByRef)
> Boolean WriteByte(MyCompany.SomeNamespace.SomeType, System.Collections.Generic.IEnumerable`1[System.Byte], Int32 ByRef)
> Boolean WriteByte(MyCompany.SomeNamespace.SomeType, Int32, System.Collections.Generic.IEnumerable`1[System.Byte], Int32 ByRef)
> Boolean WriteByte(MyCompany.SomeNamespace.SomeType, Int32, Byte[], Int32 ByRef)
>
> I attempt to disambiguate by selecting the 2-arg method I want by type:
>
>>>> desired_method= accessor.WriteByte.__overloads__[MyCompany.SomeNamespace.SomeType, System.Array[System.Byte]]
> The problem may be right here - if I print out desired_method.__doc__ I get the same 5 methods listed above.  Anyway, then I compute my arguments, and can verify they have the types I expect (If it matters, arg_b is created by a call to System.IO.BinaryReader.ReadBytes()):
>
>>>> arg_a= ...
>>>> arg_b= ...
>>>> type(arg_a)
> <class 'MyCompany.SomeNamespace.SomeType'>
>>>> type(arg_b)
> <class 'System.Byte[]'>
>
> However, when I try to call the disambiguated method with the correct args, I get no love:
>
>>>> result= desired_method(arg_a, arg_b)
> Traceback (most recent call last):
>    File "<string>", line 1, in<fragment>
> TypeError: No method matches given arguments
>
> Does anyone see the obvious thing I'm doing wrong?  Disambiguating can be a pain but I've done it before for other methods and have never had such an impossible time getting the correct method called.
>
> Thanks,
>
> Adrian Perez
> _________________________________________________
> Python.NET mailing list - [hidden email]
> http://mail.python.org/mailman/listinfo/pythondotnet
>
_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|

Re: issue disambiguating overloaded method UPDATE

BCline

from System import String, Char, Int32
def TestCharArray():
     from System import Array
     charArrType = Array[Char]
     ##arrType = charArrType('hello')
     ##for char in arrType:
     ##    print char,
     ## Also works on instances of the type! ##
     strCharArr = String.__overloads__[charArrType]
     s = strCharArr(list('hello'))
     print s
TestCharArray()

On 5/17/2011 12:38 AM, Barton wrote:

>
> def TestCharArray():
>     from System import Array
>     charArrType = Array[Char]
>     ##arrType = charArrType('hello')
>     ##for char in arrType:
>     ##    print char,
>     ## Also works on instances of the type! ##
>     strCharArr = s.__overloads__[charArrType]
>     s = strCharArr(list('hello'))
>     print s
>
> On 5/16/2011 6:29 PM, Adrian Perez wrote:
>> Hello everyone,
>>
>> This is probably a total n00b question, but I've been using pythonnet
>> in our toolchain to communicate with a private c# assembly for months
>> now without ever running into a problem I couldn't eventually figure
>> out.  This one has me totally stumped, and I'm hoping something
>> obvious will jump out at experienced eyes.
>>
>> (I did a find/replace for secrecy, besides silly names these are the
>> raw results I get back)
>>
>> A C# class exposes a method 'WriteByte' that has 5 overloads (note
>> that only one of them takes 2 arguments):
>>
>>>>> accessor= MyCompany.SomeNamespace.SomeTypeArrayAccessor
>>>>> accessor.WriteByte.__overloads__
>> Boolean WriteByte(MyCompany.SomeNamespace.SomeType, Byte[])
>> Boolean WriteByte(MyCompany.SomeNamespace.SomeType, Byte[], Int32 ByRef)
>> Boolean WriteByte(MyCompany.SomeNamespace.SomeType,
>> System.Collections.Generic.IEnumerable`1[System.Byte], Int32 ByRef)
>> Boolean WriteByte(MyCompany.SomeNamespace.SomeType, Int32,
>> System.Collections.Generic.IEnumerable`1[System.Byte], Int32 ByRef)
>> Boolean WriteByte(MyCompany.SomeNamespace.SomeType, Int32, Byte[],
>> Int32 ByRef)
>>
>> I attempt to disambiguate by selecting the 2-arg method I want by type:
>>
>>>>> desired_method=
>>>>> accessor.WriteByte.__overloads__[MyCompany.SomeNamespace.SomeType,
>>>>> System.Array[System.Byte]]
>> The problem may be right here - if I print out desired_method.__doc__
>> I get the same 5 methods listed above.  Anyway, then I compute my
>> arguments, and can verify they have the types I expect (If it
>> matters, arg_b is created by a call to
>> System.IO.BinaryReader.ReadBytes()):
>>
>>>>> arg_a= ...
>>>>> arg_b= ...
>>>>> type(arg_a)
>> <class 'MyCompany.SomeNamespace.SomeType'>
>>>>> type(arg_b)
>> <class 'System.Byte[]'>
>>
>> However, when I try to call the disambiguated method with the correct
>> args, I get no love:
>>
>>>>> result= desired_method(arg_a, arg_b)
>> Traceback (most recent call last):
>>    File "<string>", line 1, in<fragment>
>> TypeError: No method matches given arguments
>>
>> Does anyone see the obvious thing I'm doing wrong?  Disambiguating
>> can be a pain but I've done it before for other methods and have
>> never had such an impossible time getting the correct method called.
>>
>> Thanks,
>>
>> Adrian Perez
>> _________________________________________________
>> Python.NET mailing list - [hidden email]
>> http://mail.python.org/mailman/listinfo/pythondotnet
>>
_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|

Re: issue disambiguating overloaded method UPDATE

Adrian Perez-11
Thank you for the snippets.  I had poured over the unit tests and seen similar examples of how to do this already, but two commentless replies inspired me to trace through this in a debugger.

The issue ended up being that I was erroneously under the impression I was calling a static method, and was retrieving the method off of the 'type' object, rather than an instance of the type.  The error you get back is the generic "no method matches given arguments", because my local 'desired_method' expected an initial 'self' argument, and so silently took 'arg_a' to be 'self'.  This meant MethodBinder.Bind() only thought it had 1 input argument and could not match it to any of the overloads.

Cheers,

Adrian Perez


-----Original Message-----
From: BCline [mailto:[hidden email]]
Sent: Tuesday, May 17, 2011 12:56 AM
Cc: Adrian Perez; [hidden email]
Subject: Re: [Python.NET] issue disambiguating overloaded method UPDATE


from System import String, Char, Int32
def TestCharArray():
     from System import Array
     charArrType = Array[Char]
     ##arrType = charArrType('hello')
     ##for char in arrType:
     ##    print char,
     ## Also works on instances of the type! ##
     strCharArr = String.__overloads__[charArrType]
     s = strCharArr(list('hello'))
     print s
TestCharArray()

On 5/17/2011 12:38 AM, Barton wrote:

>
> def TestCharArray():
>     from System import Array
>     charArrType = Array[Char]
>     ##arrType = charArrType('hello')
>     ##for char in arrType:
>     ##    print char,
>     ## Also works on instances of the type! ##
>     strCharArr = s.__overloads__[charArrType]
>     s = strCharArr(list('hello'))
>     print s
>
> On 5/16/2011 6:29 PM, Adrian Perez wrote:
>> Hello everyone,
>>
>> This is probably a total n00b question, but I've been using pythonnet
>> in our toolchain to communicate with a private c# assembly for months
>> now without ever running into a problem I couldn't eventually figure
>> out.  This one has me totally stumped, and I'm hoping something
>> obvious will jump out at experienced eyes.
>>
>> (I did a find/replace for secrecy, besides silly names these are the
>> raw results I get back)
>>
>> A C# class exposes a method 'WriteByte' that has 5 overloads (note
>> that only one of them takes 2 arguments):
>>
>>>>> accessor= MyCompany.SomeNamespace.SomeTypeArrayAccessor
>>>>> accessor.WriteByte.__overloads__
>> Boolean WriteByte(MyCompany.SomeNamespace.SomeType, Byte[]) Boolean
>> WriteByte(MyCompany.SomeNamespace.SomeType, Byte[], Int32 ByRef)
>> Boolean WriteByte(MyCompany.SomeNamespace.SomeType,
>> System.Collections.Generic.IEnumerable`1[System.Byte], Int32 ByRef)
>> Boolean WriteByte(MyCompany.SomeNamespace.SomeType, Int32,
>> System.Collections.Generic.IEnumerable`1[System.Byte], Int32 ByRef)
>> Boolean WriteByte(MyCompany.SomeNamespace.SomeType, Int32, Byte[],
>> Int32 ByRef)
>>
>> I attempt to disambiguate by selecting the 2-arg method I want by type:
>>
>>>>> desired_method=
>>>>> accessor.WriteByte.__overloads__[MyCompany.SomeNamespace.SomeType,
>>>>> System.Array[System.Byte]]
>> The problem may be right here - if I print out desired_method.__doc__
>> I get the same 5 methods listed above.  Anyway, then I compute my
>> arguments, and can verify they have the types I expect (If it
>> matters, arg_b is created by a call to
>> System.IO.BinaryReader.ReadBytes()):
>>
>>>>> arg_a= ...
>>>>> arg_b= ...
>>>>> type(arg_a)
>> <class 'MyCompany.SomeNamespace.SomeType'>
>>>>> type(arg_b)
>> <class 'System.Byte[]'>
>>
>> However, when I try to call the disambiguated method with the correct
>> args, I get no love:
>>
>>>>> result= desired_method(arg_a, arg_b)
>> Traceback (most recent call last):
>>    File "<string>", line 1, in<fragment>
>> TypeError: No method matches given arguments
>>
>> Does anyone see the obvious thing I'm doing wrong?  Disambiguating
>> can be a pain but I've done it before for other methods and have
>> never had such an impossible time getting the correct method called.
>>
>> Thanks,
>>
>> Adrian Perez
>> _________________________________________________
>> Python.NET mailing list - [hidden email]
>> http://mail.python.org/mailman/listinfo/pythondotnet
>>
_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet