Skip to content

Passing Python objects implementing a generic C# interface to a C# method fails in 3.0.0 #1982

@fractus

Description

@fractus

Environment

  • Pythonnet version: 2.5.2 and 3.0.0.post01
  • Python version: 3.7.6 (2.5.2) and 3.9.12 (3.0.0.post01)
  • Operating System: Win10
  • .NET Runtime: .NET 6

Details

Passing a Python object (obj) that implements a generic interface in Python (ISome) to a C# class fails in pythonnet 3.0 but works in 2.5.2; same behaviour whether specific (SomeSpecific) or generic (SomeGeneric)

namespace Some.Namespace
{

   public interface ISome<T1>
   {
       public T1 Get1(T1 x);
   }

   public class SomeSpecific
   {
       public SomeSpecific(ISome<int> some, int x)
       {
           Console.Out.WriteLine($"Spec1 = {some.Get1(x)}");
       }
   }

   public class SomeGeneric<T1>
   {
       public SomeGeneric(ISome<T1> some, T1 x)
       {
           Console.Out.WriteLine($"Gen1 = {some.Get1(x)}");
       }
   }
}
from Some.Namespace import ISome, SomeGeneric, SomeSpecific

class PySome(ISome[int]):
   __namespace__ = 'Some.Namespace'

   def Get1(self, x):
       return x

obj = PySome()
print(obj.Get1(4))
# Next two lines fail in 3.0.0
SomeSpecific(obj, 4)
SomeGeneric[int](obj, 4)
  • If there was a crash, please include the traceback here.
Traceback (most recent call last):
   SomeSpecific(obj, 4)
System.ArgumentException: Cannot resolve method Int32 Get1(Int32) because the declaring type of the method handle Some.Namespace.ISome`1[T1] is generic. Explicitly provide the declaring type to GetMethodFromHandle.
  at System.Reflection.MethodBase.GetMethodFromHandle(RuntimeMethodHandle handle)
  at Python.Runtime.PythonDerivedType.MarshalByRefsBack(Object[] args, RuntimeMethodHandle methodHandle, PyObject pyResult, Int32 outsOffset) in /tmp/build-via-sdist-rlpexh5w/pythonnet-3.0.0.post1/src/runtime/Types/ClassDerived.cs:line 844
  at Python.Runtime.PythonDerivedType.InvokeMethod[T](IPythonDerivedType obj, String methodName, String origMethodName, Object[] args, RuntimeMethodHandle methodHandle) in /tmp/build-via-sdist-rlpexh5w/pythonnet-3.0.0.post1/src/runtime/Types/ClassDerived.cs:line 727
  at Some.Namespace.PySome.Get1(Int32 )
  at Some.Namespace.SomeSpecific..ctor(ISome`1 some, Int32 x) in

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy