Skip to content

Commit 069b9d1

Browse files
authored
Unify Windows message handling APIs in messageloop.py. (#932)
* refactor: Eliminates the alias `LRESULT = LPLONG` by directly importing `LPLONG` as `LRESULT` from `ctypes.wintypes`. * refactor: Streamline `test_eventinterface.py` imports by using `ctypes.wintypes.MSG` - Removes redundant `POINT` and `MSG` structure definitions. - Directly imports `MSG` from `ctypes.wintypes`, simplifying the import statements. - Reduces boilerplate code and improves consistency by leveraging standard `wintypes` definitions. * refactor: Explicitly define WinAPI function signatures in `messageloop.py`. - Adds `argtypes` and `restype` for `GetMessage`, `TranslateMessage`, and `DispatchMessage`. * refactor: Use `POINTER(MSG)` for message function `argtypes` in `messageloop.py` * refactor: Use `HWND` for window handle in `GetMessage` argtypes in `messageloop.py` * refactor: Centralize `PeekMessage` in `messageloop.py` and reuse in tests. - Define `PeekMessage` with `argtypes` and `restype` in `messageloop.py`. - Update `test_eventinterface.py` and `test_git.py` to use `PeekMessage` and others from `messageloop`, removing redundant WinAPI definitions. * refactor: Centralize `PeekMessage` constants in `messageloop.py`. - Add `PM_NOREMOVE`, `PM_REMOVE`, and `PM_NOYIELD` constants to `messageloop.py`. - Update `test_eventinterface.py` and `test_git.py` to import `PM_REMOVE` from `messageloop`, removing redundant local definitions.
1 parent 4a1a11a commit 069b9d1

3 files changed

Lines changed: 39 additions & 48 deletions

File tree

comtypes/messageloop.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import ctypes
2-
from ctypes import WinDLL, WinError, byref
3-
from ctypes.wintypes import MSG
2+
from ctypes import POINTER, WinDLL, WinError, byref
3+
from ctypes.wintypes import BOOL, HWND, MSG
4+
from ctypes.wintypes import LPLONG as LRESULT
45
from typing import TYPE_CHECKING, SupportsIndex
56

67
if TYPE_CHECKING:
@@ -10,12 +11,29 @@
1011

1112
_FilterCallable = Callable[["_CArgObject"], Iterable[Any]] # type: ignore
1213

14+
# PeekMessage options
15+
PM_NOREMOVE = 0x0000
16+
PM_REMOVE = 0x0001
17+
PM_NOYIELD = 0x0002
18+
1319
_user32 = WinDLL("user32")
1420

1521
GetMessage = _user32.GetMessageA
16-
GetMessage.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint, ctypes.c_uint]
22+
GetMessage.argtypes = [POINTER(MSG), HWND, ctypes.c_uint, ctypes.c_uint]
23+
GetMessage.restype = BOOL
24+
1725
TranslateMessage = _user32.TranslateMessage
26+
TranslateMessage.argtypes = [POINTER(MSG)]
27+
TranslateMessage.restype = BOOL
28+
1829
DispatchMessage = _user32.DispatchMessageA
30+
DispatchMessage.argtypes = [POINTER(MSG)]
31+
DispatchMessage.restype = LRESULT
32+
33+
# https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-peekmessagea
34+
PeekMessage = _user32.PeekMessageA
35+
PeekMessage.argtypes = [POINTER(MSG), HWND, ctypes.c_uint, ctypes.c_uint, ctypes.c_uint]
36+
PeekMessage.restype = BOOL
1937

2038

2139
class _MessageLoop:

comtypes/test/test_eventinterface.py

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import unittest as ut
2-
from ctypes import POINTER, Structure, WinDLL, byref, c_long, c_uint, c_ulong
3-
from ctypes.wintypes import BOOL, HWND, LPLONG, UINT
2+
from ctypes import byref
3+
from ctypes.wintypes import MSG
44

55
from comtypes.client import CreateObject, GetEvents
6+
from comtypes.messageloop import (
7+
PM_REMOVE,
8+
DispatchMessage,
9+
PeekMessage,
10+
TranslateMessage,
11+
)
612

713
# FIXME: External test dependencies like this seem bad. Find a different
814
# built-in win32 API to use.
@@ -42,42 +48,11 @@ def DocumentComplete(self, this, *args):
4248
self._events.append("DocumentComplete")
4349

4450

45-
class POINT(Structure):
46-
_fields_ = [("x", c_long), ("y", c_long)]
47-
48-
49-
class MSG(Structure):
50-
_fields_ = [
51-
("hWnd", c_ulong),
52-
("message", c_uint),
53-
("wParam", c_ulong),
54-
("lParam", c_ulong),
55-
("time", c_ulong),
56-
("pt", POINT),
57-
]
58-
59-
6051
def PumpWaitingMessages():
61-
_user32 = WinDLL("user32")
62-
63-
_PeekMessageA = _user32.PeekMessageA
64-
_PeekMessageA.argtypes = [POINTER(MSG), HWND, UINT, UINT, UINT]
65-
_PeekMessageA.restype = BOOL
66-
67-
_TranslateMessage = _user32.TranslateMessage
68-
_TranslateMessage.argtypes = [POINTER(MSG)]
69-
_TranslateMessage.restype = BOOL
70-
71-
LRESULT = LPLONG
72-
_DispatchMessageA = _user32.DispatchMessageA
73-
_DispatchMessageA.argtypes = [POINTER(MSG)]
74-
_DispatchMessageA.restype = LRESULT
75-
7652
msg = MSG()
77-
PM_REMOVE = 0x0001
78-
while _PeekMessageA(byref(msg), 0, 0, 0, PM_REMOVE):
79-
_TranslateMessage(byref(msg))
80-
_DispatchMessageA(byref(msg))
53+
while PeekMessage(byref(msg), 0, 0, 0, PM_REMOVE):
54+
TranslateMessage(byref(msg))
55+
DispatchMessage(byref(msg))
8156

8257

8358
class Test(ut.TestCase):

comtypes/test/test_git.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from _ctypes import COMError
88
from collections.abc import Iterator
99
from ctypes import HRESULT, POINTER, OleDLL, WinDLL, byref
10-
from ctypes.wintypes import BOOL, DWORD, HANDLE, HWND, MSG, UINT
10+
from ctypes.wintypes import BOOL, DWORD, HANDLE, MSG
1111
from pathlib import Path
1212
from queue import Queue
1313

@@ -18,16 +18,16 @@
1818
RegisterInterfaceInGlobal,
1919
RevokeInterfaceFromGlobal,
2020
)
21-
from comtypes.messageloop import DispatchMessage, TranslateMessage
21+
from comtypes.messageloop import (
22+
PM_REMOVE,
23+
DispatchMessage,
24+
PeekMessage,
25+
TranslateMessage,
26+
)
2227
from comtypes.persist import STGM_READ, IPersistFile
2328

2429
_user32 = WinDLL("user32")
2530

26-
# https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-peekmessagea
27-
PeekMessage = _user32.PeekMessageA
28-
PeekMessage.argtypes = [POINTER(MSG), HWND, UINT, UINT, UINT]
29-
PeekMessage.restype = BOOL
30-
3131
# https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-msgwaitformultipleobjects
3232
MsgWaitForMultipleObjects = _user32.MsgWaitForMultipleObjects
3333
MsgWaitForMultipleObjects.restype = DWORD
@@ -49,8 +49,6 @@
4949

5050
QS_ALLINPUT = 0x04FF # All message types including SendMessage
5151

52-
PM_REMOVE = 0x0001 # Remove message from queue after Peek
53-
5452
APTTYPE_MAINSTA = 3
5553

5654
RPC_E_WRONG_THREAD = -2147417842 # 0x8001010E

0 commit comments

Comments
 (0)