Skip to content

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

@athulds

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>

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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy