Python int type is translated to pyInt when passed as a .net Dictionary value only in 3.x version
Environment
- Pythonnet version: 3.0.0a2
- Python version: 3.7, 3.8, 3.9, 3.10
- Operating System: Windows
- .NET Runtime: 4.7.2 framework
Details
After upgrading to pre release 3.0.0a2 version, the python int type is getting translated to PyInt instead of System.int32 inside the .NET DLL, and hence getting a unable to cast exception. this is working fine in 2.x version.
DLL method:
public void DictMethod(Dictionary<string, object> paramDict) { Console.WriteLine("Inside"); Console.WriteLine(paramDict["index"].GetType().ToString()); var index = Convert.ToInt32((paramDict["index"])); }
Python script:
clr.AddReference("System.Collections") from System.Collections.Generic import Dictionary from System import String from System import Object clr.AddReference("PyTest") from PyTest import PyClass class Utils(object): @staticmethod def prepare_dict(**kwargs): dict_net = Dictionary[String,Object]() for key_value in kwargs.keys(): dict_net.Add(key_value,kwargs[key_value]) return dict_net parameters = {} parameters['index'] = 1 net_dict = Utils.prepare_dict(**parameters) obj = PyClass() obj.DictMethod(net_dict)
Output in 3.x version:
C:\Users\Athul\Desktop>c:\Python37\python.exe pynettest.py
Inside
Python.Runtime.PyInt
Traceback (most recent call last):
File "pynettest.py", line 50, in <module>
obj.DictMethod(net_dict)
System.InvalidCastException: Unable to cast object of type 'Python.Runtime.PyInt' to type 'System.IConvertible'.
at System.Convert.ToInt32(Object value)
at PyTest.PyClass.DictMethod(Dictionary`2 paramDict) in C:\Users\Athul\source\repos\PyTest\PyClass.cs:line 46
Output in 2.x version :
C:\Users\Athul\Desktop>c:\Python37\python.exe pynettest.py
Inside
System.Int32
C:\Users\Athul\Desktop>