Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Must explicitly convert IntEnum to int to use equality comparison #1848

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
baptisteguidoux opened this issue Jun 30, 2022 · 1 comment
Closed

Comments

@baptisteguidoux
Copy link

Environment

  • Pythonnet version: 3.0.0-a2
  • Python version: 3.9.10
  • Operating System: Windows 10 Pro - 21H2 - 19044.1766
  • .NET Runtime: Unity/Mono

Details

  • When trying to compare the equality of Python IntEnum instances from C#, they must be explicitly converted to an int.
# in my_module.py file
class MyEnum(enum.IntEnum):
    OK = 1
    ERROR = 2

def get_status():
    return MyEnum.OK 

No crash.

PythonRunner.EnsureInitialized();
using (Py.GIL())
{  
    // Import our module
    dynamic my_module = Py.Import("my_module");
    dynamic MyEnum= my_module .MyEnum; 
    // Call the Python func on C# side
    dynamic status = my_module.get_status();
    if ((int)status == (int)MyEnum.OK) // <-- have to convert to int
    if (status == MyEnum.OK)            // when not converting to int, they are not evaluated as equal (no crash)
}
@filmor
Copy link
Member

filmor commented Jun 30, 2022

Thank you for the complete report. I'll have a look into this once I'm back at a proper computer in about a week.

lostmsu added a commit to losttech/pythonnet that referenced this issue Jun 30, 2022
fixed unhandled Python errors during comparison attempts

fixes pythonnet#1848
lostmsu added a commit to losttech/pythonnet that referenced this issue Jun 30, 2022
fixed unhandled Python errors during comparison attempts

fixes pythonnet#1848
filmor pushed a commit that referenced this issue Jul 5, 2022
fixed unhandled Python errors during comparison attempts

fixes #1848
filmor pushed a commit that referenced this issue Jul 5, 2022
fixed unhandled Python errors during comparison attempts

fixes #1848
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

No branches or pull requests

2 participants