Skip to content

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

Merged
junkmd merged 5 commits into
enthought:mainfrom
fmtabler:refactor-alias-ctype-wintypes
Dec 9, 2024
Merged

Replace alias definitions with ctypes.wintypes imports for #662#694
junkmd merged 5 commits into
enthought:mainfrom
fmtabler:refactor-alias-ctype-wintypes

Conversation

@fmtabler

@fmtabler fmtabler commented Dec 8, 2024

Copy link
Copy Markdown
Contributor

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

@junkmd junkmd left a comment

Copy link
Copy Markdown
Collaborator

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.

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

DWORD = c_ulong


class IGlobalInterfaceTable(IUnknown):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

 )
 
 
-
 class IGlobalInterfaceTable(IUnknown):

Comment thread comtypes/GUID.py
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
Copy Markdown
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

@junkmd junkmd left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please fix one more.

Comment thread comtypes/__init__.py Outdated
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
Copy Markdown
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

junkmd commented Dec 9, 2024

Copy link
Copy Markdown
Collaborator

Thanks for your quick responses.

@junkmd
junkmd merged commit 46c34ec into enthought:main Dec 9, 2024
junkmd added a commit to junkmd/pywinauto that referenced this pull request Dec 9, 2024
Comment thread comtypes/GUID.py
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
Copy Markdown
Contributor

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

@fmtabler fmtabler Dec 9, 2024

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
Contributor

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
Copy Markdown
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.

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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