You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Crash when using nested classes with inheritance, which are instantiated from C# #1414
I am accessing a member of a nested C# class which inherits from the enclosing class.
When doing this, the Pythonnet runtime crashes!
Here is a minimal example:
namespacepythonnet_testing{publicclassBar{publicclassHej:Bar{}}publicclassFoo{publicBarBar;publicstaticFooCreate(){Foof=new();f.Bar=newBar.Hej();returnf;}}classProgram{staticvoidMain(string[]args){varpyCode=@"import pythonnet_testingf = pythonnet_testing.Foo.Create()print('Trying to access member of nested subclass')print(f'Bar: {f.Bar}') ";PythonEngine.Initialize();using(Py.GIL()){PythonEngine.Exec(pyCode);}Console.WriteLine("EXIT");}}}
This seem to be specifically for nested classes, where the object instance is created from the C# environment, and the class has not been explicitly referenced/used from python.
I have found a way to prevent the crash, by first explicitly using the nested class in the python code.
Eg instead of this:
importpythonnet_testingf=pythonnet_testing.Foo.Create()
print('Trying to access member of nested subclass')
print(f'Bar: {f.Bar}')
I insert a reference to the nested Bar.Hej class
importpythonnet_testingf=pythonnet_testing.Foo.Create()
print('Trying to access member of nested subclass')
_=pythonnet_testing.Bar.Hej# Explicitly referencing the nested classprint(f'Bar: {f.Bar}')
If I do this, then the Pythonnet runtime no longer crashes. It is even enough to reference the enclosing baseclass Bar
_=pythonnet_testing.Bar# Explicitly referencing the enclosing classprint(f'Bar: {f.Bar}')
When I do this, the python code executes properly:
Trying to access member of nested subclass
Bar: pythonnet_testing.Bar+Hej
On my Mac I do not get any error message, the process dies silently. I have a report from a Windows machine with the following error:
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Repeat 2 times:
--------------------------------
at Python.Runtime.Runtime.PyDict_SetItemString(IntPtr, System.String, IntPtr)
--------------------------------
at Python.Runtime.ClassManager.InitClassBase(System.Type, Python.Runtime.ClassBase)
at Python.Runtime.ClassManager.GetClass(System.Type)
at Python.Runtime.TypeManager.CreateType(Python.Runtime.ManagedType, System.Type)
at Python.Runtime.TypeManager.GetTypeHandle(Python.Runtime.ManagedType, System.Type)
at Python.Runtime.ClassManager.InitClassBase(System.Type, Python.Runtime.ClassBase)
at Python.Runtime.ClassManager.GetClass(System.Type)
at Python.Runtime.Converter.ToPython(System.Object, System.Type)
at Python.Runtime.Converter.ToPython(System.Object, System.Type)
at Python.Runtime.MethodBinder.Invoke(IntPtr, IntPtr, IntPtr, System.Reflection.MethodBase, System.Reflection.MethodInfo[])
at Python.Runtime.MethodBinder.Invoke(IntPtr, IntPtr, IntPtr)
at Python.Runtime.Indexer.GetItem(IntPtr, IntPtr)
at Python.Runtime.ClassObject.mp_subscript(IntPtr, IntPtr)
I have also tested this in master, and the same crash and workaround applies.
Activity
filmor commentedon Mar 10, 2021
Thank you for the bug report. Two things:
masteras well?koliyo commentedon Mar 10, 2021
On my Mac I do not get any error message, the process dies silently. I have a report from a Windows machine with the following error:
I have also tested this in master, and the same crash and workaround applies.
filmor commentedon Mar 10, 2021
Thank you, I'll try to reproduce this.
When reflecting nested types, ensure their corresponding PyType is al…
When reflecting nested types, ensure their corresponding PyType is al…