Repository navigation
Enums now don't convert to UInt32 in 3.0.0rc4 #1913
Copy link
Copy link
Closed
Description
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'>)
Metadata
Metadata
Assignees
Labels
No labels
Activity
lostmsu commentedon Aug 16, 2022
This is a bug we should fix, but it looks like in your case you could do
mylib.CheckCompatibility(int(eCompatibleVersion.BuildVersion))assumingeCompatibleVersionis 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, andEnum <-> intis one of them.Pcres91 commentedon Aug 16, 2022
Ah I didn't think of using
intas 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.That seems fine with me.
lostmsu commentedon Sep 15, 2022
I believe this was fixed in #1904
forrestgan1981 commentedon Feb 11, 2024
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.
below are the function definition:

seems older version python3.7 and lower version of pythonnet works, but is there any way to fix this?
Thanks!
lostmsu commentedon Feb 11, 2024
@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.
forrestgan1981 commentedon Feb 14, 2024
@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.