Skip to content

Creating a Python class extending a C# interface with generic return type causes a System.TypeLoadException: VTable setup of type #2511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
robin-moss opened this issue Nov 15, 2024 · 0 comments

Comments

@robin-moss
Copy link

Environment

  • Pythonnet version: 3.0.4
  • Python version: 3.10.12
  • Operating System: Ubuntu 22.04 LTS
  • .NET Runtime: Mono 6.12.0.200 (tarball)

Details

  • Describe what you were trying to get done.

I've stumbled upon an exception when creating a Type in Python from a C# interface that includes a generic return type (example code below)

namespace Robin
{
    public interface IRobin
    {
        T? Find<T>() where T : IRobin;
    }
}

Converted to a DLL using: csc /t:library IRobin.cs

Then bug validated with:

import sys
import os
import pythonnet

print(__file__)
sys.path.append(os.path.dirname(os.path.realpath(__file__)))

import clr

clr.AddReference("IRobin")
from Robin import IRobin

print(pythonnet.get_runtime_info())

class A(IRobin):
    __namespace__ = "mock"
  • If there was a crash, please include the traceback here.
TypeError: Failed to create Python type for mock.A

I modified the Pythonnet.dll to console error the C# exception and I get:

Python.Runtime.InternalPythonnetException: Failed to create Python type for mock.A ---> System.TypeLoadException: VTable setup of type mock.A failed
  at (wrapper managed-to-native) System.RuntimeType.GetMethodsByName_native(System.RuntimeType,intptr,System.Reflection.BindingFlags,System.RuntimeType/MemberListType)
  at System.RuntimeType.GetMethodsByName (System.String name, System.Reflection.BindingFlags bindingAttr, System.RuntimeType+MemberListType listType, System.RuntimeType reflectedType) [0x0001b] in <d636f104d58046fd9b195699bcb1a744>:0 
  at System.RuntimeType.GetMethodCandidates (System.String name, System.Reflection.BindingFlags bindingAttr, System.Reflection.CallingConventions callConv, System.Type[] types, System.Int32 genericParamCount, System.Boolean allowPrefixLookup) [0x00010] in <d636f104d58046fd9b195699bcb1a744>:0 
  at System.RuntimeType.GetMembers (System.Reflection.BindingFlags bindingAttr) [0x00000] in <d636f104d58046fd9b195699bcb1a744>:0 
  at Python.Runtime.ClassManager.GetClassInfo (System.Type type, Python.Runtime.ClassBase impl) [0x00028] in <2aa3218182dd47ff892dddbce801f07b>:0 
  at Python.Runtime.ClassManager.InitClassBase (System.Type type, Python.Runtime.ClassBase impl, Python.Runtime.ReflectedClrType pyType) [0x00001] in <2aa3218182dd47ff892dddbce801f07b>:0 
  at Python.Runtime.ReflectedClrType.GetOrCreate (System.Type type) [0x00047] in <2aa3218182dd47ff892dddbce801f07b>:0 
   --- End of inner exception stack trace ---
  at Python.Runtime.ReflectedClrType.GetOrCreate (System.Type type) [0x00076] in <2aa3218182dd47ff892dddbce801f07b>:0 
  at Python.Runtime.ReflectedClrType.CreateSubclass (Python.Runtime.ClassBase baseClass, System.Collections.Generic.IList`1[T] interfaces, System.String name, System.String assembly, System.String ns, Python.Runtime.BorrowedReference dict) [0x0001f] in <2aa3218182dd47ff892dddbce801f07b>:0 Traceback (most recent call last):

With a bit more console out I found the issue is when we try to get the members of the Python class: https://github.com/pythonnet/pythonnet/blob/v3.0.4/src/runtime/ClassManager.cs#L344

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant