Skip to content

Python int type is translated to pyInt when passed as a .net Dictionary value only in 3.x version #1755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
athulds opened this issue Apr 6, 2022 · 3 comments · Fixed by #1762

Comments

@athulds
Copy link

athulds commented Apr 6, 2022

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>
@filmor
Copy link
Member

filmor commented Apr 6, 2022

This behavioural change is intentional as converting automatically to int32 (or int64, or maybe BigInteger) created too many problems (#1568). One might be able to implement IConvertible to recover your particular use-case, PRs are welcome :)

@vinaykk5

This comment was marked as off-topic.

@filmor
Copy link
Member

filmor commented Apr 10, 2022

@athulds Can you please check whether the version from the linked PR (#1762) works for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants