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

Replace alias definitions with ctypes.wintypes imports for #662 #694

Merged
merged 5 commits into from
Dec 9, 2024

Conversation

fmtabler
Copy link
Contributor

@fmtabler fmtabler commented Dec 8, 2024

Removes alias definitions and replaces them with imports from ctypes.wintypes where possible for #662

@junkmd junkmd added this to the 1.4.9 milestone Dec 8, 2024
Copy link
Collaborator

@junkmd junkmd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution.

Please fix formatting issues.

comtypes/git.py Outdated
Comment on lines 17 to 21
)

DWORD = c_ulong


class IGlobalInterfaceTable(IUnknown):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 )
 
 
-
 class IGlobalInterfaceTable(IUnknown):

comtypes/GUID.py Outdated
Comment on lines 12 to 15
def binary(obj: "GUID") -> bytes:
return bytes(obj)


BYTE = c_byte
WORD = c_ushort
DWORD = c_ulong

_ole32 = oledll.ole32
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 def binary(obj: "GUID") -> bytes:
     return bytes(obj)
 
+
 _ole32 = oledll.ole32

Copy link
Collaborator

@junkmd junkmd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix one more.

@@ -25,6 +25,7 @@
from ctypes import HRESULT # noqa
from ctypes import _Pointer, _SimpleCData # noqa
from ctypes import c_int, c_ulong, oledll, windll
from ctypes.wintypes import DWORD
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-from ctypes.wintypes import DWORD
+from ctypes.wintypes import DWORD  # noqa

Linter check disabled on import statement,
no direct use of import in module but tests depend
on DWORD import in __init__.py
@junkmd
Copy link
Collaborator

junkmd commented Dec 9, 2024

Thanks for your quick responses.

@junkmd junkmd merged commit 46c34ec into enthought:main Dec 9, 2024
49 checks passed
junkmd added a commit to junkmd/pywinauto that referenced this pull request Dec 9, 2024
@@ -1,7 +1,8 @@
"""comtypes.GUID module"""

from ctypes import oledll, windll
from ctypes import byref, c_byte, c_ushort, c_ulong, c_wchar_p, Structure
from ctypes import byref, c_wchar_p, Structure
from ctypes.wintypes import BYTE, WORD, DWORD
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably don't want to use BYTE
See this issue: python/cpython#60580

Copy link
Contributor Author

@fmtabler fmtabler Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Microsoft Win32 API GUID documentation (https://learn.microsoft.com/en-us/windows/win32/api/guiddef/ns-guiddef-guid), the correct GUID structure would have unsigned chars for the Data4 field.

I think since ctypes.wintypes was updated to have BYTE = ctypes.c_ubyte here: python/cpython#97579, importing BYTE from ctypes.wintypes should be better aligned with the GUID structure than defining BYTE = c_byte in GUID.py.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, c_ubyte is the correct type.
My point is more that wintypes.DWORD = c_ubyte only since python 3.12.
So, for python 3.8 to 3.11, wintypes.DWORD = c_byte.
This may cause an unexpected behaviour because it is not the same value for all the supported python version

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@moi15moi

Is wintypes.DWORD a typo for wintypes.BYTE?
(Both DWORD in 3.11 and DWORD in 3.12 are aliases for c_ulong, and neither c_byte nor c_ubyte.)

Indeed, I overlooked python/cpython#60580. However, I believe this change is not breaking but rather a bug fix.

While it may have been possible to assign signed values to the Data4 field in the past, this was limited to the scope of ctypes structures.
Such values should not have been allowed as part of a COM component.

Tests for GUID are already included in the CI pipeline, so I don't believe there is much to fear here.

If we later discover that this change has unintended consequences after the release, we plan to revert the replacement of BYTE and release a new version.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is wintypes.DWORD a typo for wintypes.BYTE?

Yes, sorry about that.
To be sure that BYTE is c_ubyte on any python version comtypes support, shouldn't we do this?

if sys.version_info >= (3, 12):
    from ctypes.wintypes import BYTE
else:
    from ctypes import c_ubyte
    BYTE = c_ubyte

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@moi15moi

I think that’s a great idea.
Feel free to open the PR!
Let’s also check if the tests in the CI pipeline pass there.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened the issue about this: #696 (comment)

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 this pull request may close these issues.

Replace type alias with compatible symbols that can be imported from ctypes.wintypes.
3 participants