[Python.NET] struct with no explicit constructor

Jeff Ames jeffames at gmail.com
Thu Oct 16 01:23:05 CEST 2008


I'm trying to create an instance of a struct that has no explicit
constructor in C#, but in python I always get a TypeError.

My test struct in C#:

  namespace Foo
      public struct MyStruct
          public int x;

In python:

  mystruct = Foo.MyStruct()

This gives me "TypeError: no constructor matches given arguments".
I get slightly further calling __new__ explicitly:

  mystruct = object.__new__(Foo.MyStruct)

However, then I get an exception from when mystruct goes out of scope:

  Unhandled Exception: System.ArgumentException: GCHandle value cannot be zero
    at System.Runtime.InteropServices.GCHandle.op_Explicit (IntPtr
value) [0x00000]
    at Python.Runtime.ManagedType.GetManagedObject (IntPtr ob) [0x00000]
    at Python.Runtime.ClassBase.tp_dealloc (IntPtr ob) [0x00000]
    at (wrapper native-to-managed) Python.Runtime.ClassBase:tp_dealloc (intptr)
    at (wrapper managed-to-native) Python.Runtime.Runtime:Py_Main (int,string[])
    at Python.Runtime.PythonConsole.Main (System.String[] args) [0x00000]

If I add an explicit constructor to MyStruct, everything works fine,
but this is a third-party library that I'd like to use as-is if
possible.  Is there any way to do this?


More information about the PythonDotNet mailing list