- Notifications
You must be signed in to change notification settings - Fork749
Description
Environment
- Pythonnet version: 2.5.2
- Python version: 3.8.3
- Operating System: Win10 64bit
- .NET Runtime: 4.8
Details
- Describe what you were trying to get done.
In C# I have defined an interface with a method with an out parameter. I implemented this interface in python, an now I want to call this method from C#. (using pythonnet)
What I see is that a method without out parameter works fine, but a method with an out parameter throws an access violation.
I know that out parameters are handled differently in pythonnet: you return a tuple with first the return value and the second tuple item is the out parameter.
- What commands did you run to trigger this issue? If you can provide a
Minimal, Complete, and Verifiable example
this will help us understand the issue.
My C# code
publicinterfaceIMyInterface{stringMyMethod_Out(stringname,outintindex);stringMyMethod(stringname);}publicclassMyServer{publicvoidCallMyMethod_Out(IMyInterfacemyInterface){Console.WriteLine("C#.CallMyMethod_Out: before MyMethod_Out");intindex=1;myInterface.MyMethod_Out("myclient",outindex);Console.WriteLine($"C#:CallMyMethod_Out: after MyMethod_Out: index:{index}");}publicvoidCallMyMethod(IMyInterfacemyInterface){Console.WriteLine("C#.CallMyMethod: before MyMethod");myInterface.MyMethod("myclient");Console.WriteLine($"C#:CallMyMethod: after MyMethod");}publicvoidDoSomething(){Console.WriteLine("C#.DoSomething");}}
My Python code:
importclrimportsyssys.path.append('some_dir')clr.AddReference("ClassLibrary1")fromClassLibrary1importIMyInterface,MyServerclassMyInterfaceImpl(IMyInterface):__namespace__="ClassLibrary1"defMyMethod_Out(self,name,index):print("Python.MyInterfaceImpl.MyMethod_Out")other_index=101return ('MyName',other_index)defMyMethod(self,name):print("Python.MyInterfaceImpl.MyMethod")return'MyName'print('\nCall regular C# function')my_server=MyServer()my_server.DoSomething()my_interface_impl=MyInterfaceImpl()print('\nCall without out param')my_server.CallMyMethod(my_interface_impl)print('\nCall with out param')my_server.CallMyMethod_Out(my_interface_impl)# Access Violation 0xC0000005
- If there was a crash, please include the traceback here.
Output:
Call regular C# function
C#.DoSomethingCall without out param
C#.CallMyMethod: before MyMethod
Python.MyInterfaceImpl.MyMethod
C#:CallMyMethod: after MyMethodCall with out param
C#.CallMyMethod_Out: before MyMethod_Out
Process finished with exit code -1073741819 (0xC0000005)
Expected output
...
Call with out param
C#.CallMyMethod_Out: before MyMethod_Out
Python.MyInterfaceImpl.MyMethod_Out
C#:CallMyMethod_Out: after MyMethod_Out: index:101