-
-
Notifications
You must be signed in to change notification settings - Fork 97
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
Fix TypeError
when defining enumeration types
#525
Conversation
Still an issue with comtypes=1.4.2 installed on python 3.11.09 while trying to access CSI api. Error shown: |
Thank you for the bug report. I have a few questions for you.
Since there are theoretically an infinite number of COM type libraries, some may have type information configurations that we have not anticipated before. I do not know the specifications of all COM type libraries, so I would like you to share the information and knowledge you have with the community to help us solve the problem. Best regards |
Arguments passed: The gen folder is attached. API link: https://wiki.csiamerica.com/display/kb/OAPI Detailed traceback is below: File c:\Users<username>.conda\envs\py030900\lib\site-packages\comtypes\client_init_.py:188, in GetActiveObject(progid, interface, dynamic) File c:\Users<username>.conda\envs\py030900\lib\site-packages\comtypes\client_init_.py:196, in _manage(obj, clsid, interface) File c:\Users<username>.conda\envs\py030900\lib\site-packages\comtypes\client_init_.py:121, in GetBestInterface(punk) File c:\Users<username>.conda\envs\py030900\lib\site-packages\comtypes\client_generate.py:128, in GetModule(tlib) File c:\Users<username>.conda\envs\py030900\lib\site-packages\comtypes\client_generate.py:245, in ModuleGenerator.generate(self) File c:\Users<username>.conda\envs\py030900\lib\site-packages\comtypes\client_generate.py:245, in (.0) File c:\Users<username>.conda\envs\py030900\lib\site-packages\comtypes\client_generate.py:217, in _create_module(modulename, code) File c:\Users<username>.conda\envs\py030900\lib\site-packages\comtypes\client_generate.py:28, in _my_import(fullname) File c:\Users<username>.conda\envs\py030900\lib\importlib_init_.py:127, in import_module(name, package) File :1030, in _gcd_import(name, package, level) File :1007, in find_and_load(name, import) File :986, in find_and_load_unlocked(name, import) File :680, in _load_unlocked(spec) File :850, in exec_module(self, module) File :228, in _call_with_frames_removed(f, *args, **kwds) File c:\Users<username>.conda\envs\py030900\lib\site-packages\comtypes\gen\CSiAPIv1.py:708 File c:\Users<username>.conda\envs\py030900\lib\site-packages\comtypes\gen\CSiAPIv1.py:712, in eHingeDistributionType() File c:\Users<username>.conda\envs\py030900\lib\enum.py:133, in _EnumDict.setitem(self, key, value) TypeError: Attempted to reuse key: 'eHingeDistributionType_EqualSpacing'` |
Thank you for your information. The following is my speculation. Error investigation
>>> from enum import IntFlag
>>> class Foo(IntFlag):
... SPAM = 1
... HAM = 2
... SPAM = 1
... BACON = 3
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in Foo
File "...\Python310\lib\enum.py", line 134, in __setitem__
raise TypeError('Attempted to reuse key: %r' % key)
TypeError: Attempted to reuse key: 'SPAM'
I have checked the Surprisingly, it seems that the type information for # In `gen/_F896D55D_8BDF_4232_B9AB_4B210897A81D_0_1_0.py`, L102-L108;
# values for enumeration 'eHingeDistributionType'
eHingeDistributionType_NonlinearBeamColumn = 1
eHingeDistributionType_DistributedPlasticity = 2
eHingeDistributionType_EqualSpacing = 3
eHingeDistributionType_EqualSpacing = 4
eHingeDistributionType_EqualSpacing = 5
eHingeDistributionType = c_int # enum # In `gen/CSiAPIv1.py`, L708-L713;
class eHingeDistributionType(IntFlag):
eHingeDistributionType_NonlinearBeamColumn = 1
eHingeDistributionType_DistributedPlasticity = 2
eHingeDistributionType_EqualSpacing = 3
eHingeDistributionType_EqualSpacing = 4
eHingeDistributionType_EqualSpacing = 5
|
caused by defining members with the same name within an enum type. enthought#525 (comment)
In the following branch, to prevent the Please execute the following command to install it and try it in your environment. I have confirmed that this change has no impact on the COM type libraries used in CI and the environments I can verify. On the other hand, I do not have the CSI-API. Whether this change works or not, please share the contents of Best regards |
Is there an update on this? If we can confirm that this patch does not cause any errors in your script, I am considering including it in the next release scheduled for early June. |
…n enum type. (#626) * clean-up import part * Prevent errors caused by defining members with the same name within an enum type. #525 (comment) * refactoring * Add a conditional branch to issue a warning when adding duplicate members to `EnumerationNamespaces.add`. Also, added cases to the examples that trigger a warning. This is related to #550.
This is a patch to prevent the
TypeError
reported in #524 (comment).In
comtypes
, names of enumeration members are also used as names for constants at the wrapper module level.Leveraging this feature, the functionality to define enumeration types within friendly modules was implemented in #475.
However, some COM libraries have duplicated names between enumeration members and CoClass or Interface names.
In other words, integers may not be assigned to these names, they may actually be of a different type.
When such a situation occurs and
comtypes
attempt to assign a non-integer object to the value of an enumeration member, aTypeError
is raised.In this PR, for resolving the aforementioned issue,
comtypes
assigns the integer literal values to the members of the enumeration types instead of referencing the namespace defined within the wrapper module.