Skip to content

Selecting overloaded methods with unsigned integer argument fails #283

Open

Description

@makalu79

When trying to invoke a .NET method with a unsigned integer argument, it fails with "No method matches given arguments", if there is another method signature with the same name. To reproduce, use the class:

public class Class1
    {
      public string MethodWithOverloading(UInt16 aParam1)
      {
        return string.Format("MethodWithOverloading(int {0})", aParam1);
      }

      public string MethodWithOverloading(string aParam2)
      {
        return string.Format("MethodWithOverloading(string {0})", aParam2);
      }

      public string MethodWithOverloading(UInt16 aParam1, string aParam2)
      {
        return string.Format("MethodWithOverloading(int {0}, string {1})", aParam1, aParam2);
      }
    }

Then called from python with:

c = Class1()
print c.MethodWithOverloading('test')
print c.MethodWithOverloading(42)
print c.MethodWithOverloading(42, 'test')

line 2 works fine, but lines 3 and 4 fail with TypeError: No method matches given arguments (using 2.1.0)

Activity

  1. den-run-ai commented on Nov 4, 2016

    @den-run-ai
    Contributor

    Can't you convert to .NET type(s) before passing in? There is no built-in
    equivalent of unsigned integer in Python.

    On Fri, Nov 4, 2016, 8:05 AM makalu79 [email protected] wrote:

    When trying to invoke a .NET method with a unsigned integer argument, it
    fails with "No method matches given arguments", if there is another method
    signature with the same name. To reproduce, use the class:

    public class Class1
    {
    public string MethodWithOverloading(UInt16 aParam1)
    {
    return string.Format("MethodWithOverloading(int {0})", aParam1);
    }

      public string MethodWithOverloading(string aParam2)
      {
        return string.Format("MethodWithOverloading(string {0})", aParam2);
      }
    
      public string MethodWithOverloading(UInt16 aParam1, string aParam2)
      {
        return string.Format("MethodWithOverloading(int {0}, string {1})", aParam1, aParam2);
      }
    }
    

    Then called from python with:

    c = Class1()
    print c.MethodWithOverloading('test')
    print c.MethodWithOverloading(42)
    print c.MethodWithOverloading(42, 'test')

    line 2 works fine, but lines 3 and 4 fail with TypeError: No method
    matches given arguments

    —
    You are receiving this because you are subscribed to this thread.
    Reply to this email directly, view it on GitHub
    #283, or mute the thread
    https://github.com/notifications/unsubscribe-auth/AHgZ5c7Y_eFaESTrZJdyTYKQ8UZdFHVNks5q6y2tgaJpZM4Kpg9E
    .

  2. makalu79 commented on Nov 10, 2016

    @makalu79
    Author

    Yes, that would be possible. But when using "3rd party" assembly, that would mean to write wrappers around every method including range checking. Of course, if the .NET method has overloaded signature which just differs by an argument being either unsigned or signet integer, we're out of luck! But the strage thing is, the exact same code worked in pythonnet 2.0.0. I tried to attach a patch file for pythonnet 2.1.0 version of runtime/converter.cs which solved the problem for me, didn't work, so pardon me for pasting it into the comments below.

    --- C:/work/Test/pythonnet-2.1.0/src/runtime/converter (2).cs   Do Nov 10 08:30:16 2016
    +++ C:/work/Test/pythonnet-2.1.0/src/runtime/converter.cs   Do Nov 10 10:20:18 2016
    @@ -35,6 +35,9 @@ namespace Python.Runtime {
             static Type int16Type;
             static Type int32Type;
             static Type int64Type;
    +        static Type uint16Type;
    +        static Type uint32Type;
    +        static Type uint64Type;
             static Type flagsType;
             static Type boolType;
             static Type typeType;
    @@ -46,6 +49,9 @@ namespace Python.Runtime {
                 int16Type = typeof(Int16);
                 int32Type = typeof(Int32);
                 int64Type = typeof(Int64);
    +            uint16Type = typeof(UInt16);
    +            uint32Type = typeof(UInt32);
    +            uint64Type = typeof(UInt64);
                 singleType = typeof(Single);
                 doubleType = typeof(Double);
                 decimalType = typeof(Decimal);
    @@ -92,10 +98,13 @@ namespace Python.Runtime {
                 }
     #endif
                 else if ((op == int16Type) ||
    -                    (op == int32Type)) {
    +                     (op == int32Type) || 
    +                     (op == uint16Type) ||
    +                     (op == uint32Type)) {
                     return Runtime.PyIntType;
                 }
    -            else if (op == int64Type) {
    +            else if ((op == int64Type) ||
    +                     (op == uint64Type)) {
                     return Runtime.PyLongType;
                 }
                 else if ((op == doubleType) ||
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions