Visitar URL original
Python int type is translated to pyInt when passed as a .net Dictionary value only in 3.x version · Issue #1755 · pythonnet/pythonnet · GitHub
Skip to content

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

Description

@athulds

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>

Activity

  1. filmor commented on Apr 6, 2022

    @filmor
    Member

    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 :)

  2. vinaykk5 commented on Apr 7, 2022

    @vinaykk5
  3. filmor commented on Apr 10, 2022

    @filmor
    Member

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions