Skip to content
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

python3.9 Enum issue #6

Closed
forrestgan1981 opened this issue Feb 11, 2024 · 3 comments · May be fixed by #7
Closed

python3.9 Enum issue #6

forrestgan1981 opened this issue Feb 11, 2024 · 3 comments · May be fixed by #7

Comments

@forrestgan1981
Copy link

I was trying to use python3.9 to control beamgage camera.
It seems ok to connect camera, and take snapshot. But when trying to get properties or set properties, it shows error as below:

Python.Runtime.PythonException: since Python.NET 3.0 int can not be converted to Enum implicitly. Use Enum(int_value)

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

System.ArgumentException: since Python.NET 3.0 int can not be converted to Enum implicitly. Use Enum(int_value) in method Double Get(Spiricon.Automation.EGBDesignator) ---> Python.Runtime.PythonException: since Python.NET 3.0 int can not be converted to Enum implicitly. Use Enum(int_value)
   --- End of inner exception stack trace ---

searched, but didn't get any solution beside downgrade python to 3.7 or 3.8 version.
what causes the issue? the input is EGBDesignator for Get method, but even with enum input, the error still shows up.

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

is there any way to fix that?

@russleikis
Copy link
Contributor

Thanks for submitting your question. I'm currently using Python 3.10 and have reproduced the issue.

Pythonnet changed how they handle .NET enumerations in v3.0 and later. I can't identify why the exception is only thrown when using Python v3.9 or later, however this has been confirmed by other users of Pythonnet.
pythonnet/pythonnet#1220

Previously Pythonnet converted any .NET Enums to an int value. So the example wrapper, beamgagepy.py, had to adapt for that. This was a limitation of Pythonnet and so that is the way the example wrapper handles it. In theory, the change to Pythonnet should allow enums to be used more naturally, without using the integer values.

This appears to require a rework to how the enumerations are handled in the wrapper. There are no clear examples of the new usage. We are looking into it and have some parts working. You are welcome to view some of the changes I have so far on the following branch. I'll describe some of the discovery below so you can replicate it in your client.
https://github.com/Ophir-Spiricon/BP-Automation/tree/6-python39-enum-issue

  1. The Enum values do not have to be referenced in the getters and setters. I am not pleased with accessing the reference to the Spiricon.Automation namespace via the beamgage object/instance, but otherwise it seemed that the import declaration needed to be in every class member. So this needs to be cleaned up.
    image
  2. This does fix the members within the DataSource class, but I now have exceptions involving other enumerations such as CalibrationStatus. I don't have a solution yet.
  3. Per some threads and documentation some .NET objects that are not standard types or classes may have to be implemented in the wrapper using Codecs. Since any concrete examples of .NET enum usage in Pythonnet 3.x are non-existent, I don't know if this is inevitable.

@russleikis
Copy link
Contributor

I solved the issue with the other enums used directly in the example. The branch contains a full working example. I'll need to review the rest of the wrapper for other members that need to be updated.

Please let me know if you have any questions or any suggestions for the example code.

@russleikis russleikis linked a pull request Feb 15, 2024 that will close this issue
@forrestgan1981
Copy link
Author

forrestgan1981 commented Feb 15, 2024 via email

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

Successfully merging a pull request may close this issue.

3 participants