Skip to content

Enums now don't convert to UInt32 in 3.0.0rc4 #1913

Closed

Description

@Pcres91

Environment

  • Pythonnet version: 3.0.0rc4
  • Python version: 3.9 64-bit
  • Operating System: Windows 10
  • .NET Runtime: .NET Framework 4.8.4510.0

Details

  • Describe what you were trying to get done.

I am trying passing my library's enum into a function that takes in a UInt32. Previously on Pythonnet 2.5, this conversion was done automatically. Now, I have to use System.Convert.ToUInt32(myenum.value) for conversion. Even trying UInt32(myenum.value) fails.

Is this expected behaviour? I've also noticed that printing the enumerated value now prints the name, while previously it printed the value. Is this to make it more inline with what's expected in C#? I'd still expect the UInt32 conversion to work.

clr.AddReference("MyLibrary")
from MyLibrary import eCompatibleVersion, MyClass

mylib = MyClass()
mylib.CheckCompatibility(eCompatibleVersion.BuildVersion)

Error received:

Python.Runtime.PythonException: MyLibrary.eCompatibleVersion value cannot be converted to System.UInt32

The above exception was the direct cause of the following exception:

System.ArgumentException: MyLibrary.eCompatibleVersion value cannot be converted to System.UInt32 in method Void CheckCompatibility(UInt32) ---> Python.Runtime.PythonException: MyLibrary.eCompatibleVersion value cannot be converted to System.UInt32
   --- End of inner exception stack trace ---

The above exception was the direct cause of the following exception:

System.AggregateException: One or more errors occurred. ---> System.ArgumentException: MyLibrary.eCompatibleVersion value cannot be converted to System.UInt32 in method Void CheckCompatibility(UInt32) ---> Python.Runtime.PythonException: MyLibrary.eCompatibleVersion value cannot be converted to System.UInt32
   --- End of inner exception stack trace ---
   --- End of inner exception stack trace ---
---> (Inner Exception #0) System.ArgumentException: MyLibrary.eCompatibleVersion value cannot be converted to System.UInt32 in method Void CheckCompatibility(UInt32) ---> Python.Runtime.PythonException: MyLibrary.eCompatibleVersion value cannot be converted to System.UInt32
   --- End of inner exception stack trace ---<---


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "***", line 2, in <module>
    import load_library
  File "***", line 14, in <module>
    mylib.CheckCompatibility(eCompatibleVersion.BuildVersion)
TypeError: No method matches given arguments for IMyClass.CheckCompatibility: (<class 'MyLibrary.eCompatibleVersion'>)

Activity

  1. lostmsu commented on Aug 16, 2022

    @lostmsu
    Member

    This is a bug we should fix, but it looks like in your case you could do mylib.CheckCompatibility(int(eCompatibleVersion.BuildVersion)) assuming eCompatibleVersion is the enum you were talking about.

    After the fix you will still have to call UInt32(eCompatibleVersion.BuildVersion). Simply passing the enum value where an integer type is expected won't work in 3.0 since we are disabling ambiguous implicit conversions, and Enum <-> int is one of them.

  2. Pcres91 commented on Aug 16, 2022

    @Pcres91
    Author

    Ah I didn't think of using int as the cast - mainly because I've run into previous issues with 2's complemented-c# ints from the library being converted into >32bit ints which can subtly break things.

    After the fix you will still have to call UInt32(eCompatibleVersion.BuildVersion).

    That seems fine with me.

  3. lostmsu commented on Sep 15, 2022

    @lostmsu
    Member

    I believe this was fixed in #1904

  4. forrestgan1981 commented on Feb 11, 2024

    @forrestgan1981

    sorry to bring up this question again. I am using pythonnet 3.0.3 and python3.9.
    when I am passing a enum to a dll function which suppose to get a enum, it shows error as shown below.

    self.myCam.EGB.Get(EGBDesignator.Exposure)
    TypeError: No method matches given arguments for IADataSourceEGB.Get: (<enum 'EGBDesignator'>)

    below are the function definition:
    image

    seems older version python3.7 and lower version of pythonnet works, but is there any way to fix this?
    Thanks!

  5. lostmsu commented on Feb 11, 2024

    @lostmsu
    Member

    @forrestgan1981 I can't reproduce this with a simple test for either a simple public static method, or a simple class with public instance method. Please, narrow the issue down. Ideally, a single file example.

  6. forrestgan1981 commented on Feb 14, 2024

    @forrestgan1981

    @lostmsu thanks for your reply. Since this dll is to control a camera, I think I am not able to provide a sample code. Here is the link of the python wrap I used: https://github.com/Ophir-Spiricon/BP-Automation/blob/main/BeamGage/Python/beamgagepy.py

    seems the "Get", "Set" functions doesn't work due to the TypeError: No method matches given arguments for IADataSourceEGB.Get: (<enum 'EGBDesignator'>)

    I posted an issue on that site too, but haven't get feedback yet. not sure if there is a quick fix from python code.

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