- Notifications
You must be signed in to change notification settings - Fork752
Description
Environment
- Pythonnet version: v2.2.2
- Python version: 3.5 64bit
- Operating System: Windows 10
Details
I want to call a .NET method from python and pass a python object as the parameter. If the .NET function declare the argument type as PyObject, everything is fine, however, if declare the argument type as object or dynamic, exception raised.
In my project, the exception is “System.OverflowException” in GetManagedObject method in managedtype.cs.
In order to reproduce it, I built a minimal example, but a new exception raised "TypeError : No method matches given arguments".
These two exception may have some relationship, so I put this one here first since it has a way to be reproduced.
public object callback(dynamic _para) //TypeError : No method matches given argumentspublic object callback(object _para) //TypeError : No method matches given argumentspublic object callback(PyObject _para) //passed
This is similar to thisissue and thisPR fixed it. I tested it, when passing an int or string object the function of 2nd and 3rd signature can be found, but when passing a python object, exception raised.
Here is the minimal example,
using System;using Python.Runtime;namespace pythonnetTest{ public class CallbackBug { public void Main() { PythonEngine.Initialize(); using (Py.GIL()) { dynamic sys = PythonEngine.ImportModule("sys"); sys.attr1 = new Callback(); PythonEngine.Exec( "import sys\n" + "class template(object):\n" + " def __init__(self):\n" + " self.value = None\n" + " def write(self, msg):\n" + " self.value = msg\n" + "para = template()\n" + //call .NET method and pass a custom python object as the parameter "sys.attr2 = sys.attr1.callback(para)\n" + "sys.attr3 = para.value\n" ); Console.WriteLine(sys.attr2.value); Console.WriteLine(sys.attr3); } } } public class CallbackValue { public int value; } public class Callback { public object callback(dynamic _para) //TypeError : No method matches given arguments //public object callback(object _para) //TypeError : No method matches given arguments //public object callback(PyObject _para) //passed { using (Py.GIL()) //attension { dynamic para = _para; para.write("123"); } return new CallbackValue() { value = 100}; } }}