Skip to content

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

Open
@robin-moss

Description

@robin-moss

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

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