From aebcdee8ccae0adca3281247df2c7e3b4f1a9bb5 Mon Sep 17 00:00:00 2001 From: junkmd Date: Tue, 17 Dec 2024 21:06:23 +0900 Subject: [PATCH 1/7] Update import section. --- comtypes/_comobject.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/comtypes/_comobject.py b/comtypes/_comobject.py index e1122fb4c..41cb8e173 100644 --- a/comtypes/_comobject.py +++ b/comtypes/_comobject.py @@ -1,8 +1,13 @@ +import logging +import os +import queue +import sys +from _ctypes import CopyComPointer from ctypes import ( - FormatError, POINTER, - Structure, WINFUNCTYPE, + FormatError, + Structure, byref, c_long, c_void_p, @@ -10,22 +15,16 @@ pointer, windll, ) -from _ctypes import CopyComPointer -import logging -import os -import queue -import sys -from comtypes import COMError, ReturnHRESULT, instancemethod +from comtypes import COMError, IPersist, ReturnHRESULT, instancemethod from comtypes._memberspec import _encode_idl -from comtypes.errorinfo import ISupportErrorInfo, ReportException, ReportError -from comtypes import IPersist +from comtypes.errorinfo import ISupportErrorInfo, ReportError, ReportException from comtypes.hresult import ( DISP_E_BADINDEX, DISP_E_MEMBERNOTFOUND, E_FAIL, - E_NOINTERFACE, E_INVALIDARG, + E_NOINTERFACE, E_NOTIMPL, RPC_E_CHANGED_MODE, S_FALSE, @@ -33,7 +32,6 @@ ) from comtypes.typeinfo import IProvideClassInfo, IProvideClassInfo2 - logger = logging.getLogger(__name__) _debug = logger.debug _warning = logger.warning From a9f3fbbd2f20e6a64f8a8fb3e5f71fedcb2e99df Mon Sep 17 00:00:00 2001 From: junkmd Date: Tue, 17 Dec 2024 21:06:23 +0900 Subject: [PATCH 2/7] Add type hints to `LocalServer`. --- comtypes/_comobject.py | 16 ++++++++++------ comtypes/hints.pyi | 1 + 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/comtypes/_comobject.py b/comtypes/_comobject.py index 41cb8e173..249109535 100644 --- a/comtypes/_comobject.py +++ b/comtypes/_comobject.py @@ -15,6 +15,7 @@ pointer, windll, ) +from typing import TYPE_CHECKING, Optional, Sequence from comtypes import COMError, IPersist, ReturnHRESULT, instancemethod from comtypes._memberspec import _encode_idl @@ -32,6 +33,9 @@ ) from comtypes.typeinfo import IProvideClassInfo, IProvideClassInfo2 +if TYPE_CHECKING: + from comtypes import hints # type: ignore + logger = logging.getLogger(__name__) _debug = logger.debug _warning = logger.warning @@ -346,9 +350,9 @@ def _InterlockedDecrement(ob): class LocalServer(object): - _queue = None + _queue: Optional[queue.Queue] = None - def run(self, classobjects): + def run(self, classobjects: Sequence["hints.localserver.ClassFactory"]) -> None: # Use windll instead of oledll so that we don't get an # exception on a FAILED hresult: result = windll.ole32.CoInitialize(None) @@ -368,19 +372,19 @@ def run(self, classobjects): for obj in classobjects: obj._revoke_class() - def run_sta(self): + def run_sta(self) -> None: from comtypes import messageloop messageloop.run() - def run_mta(self): + def run_mta(self) -> None: self._queue = queue.Queue() self._queue.get() - def Lock(self): + def Lock(self) -> None: oledll.ole32.CoAddRefServerProcess() - def Unlock(self): + def Unlock(self) -> None: rc = oledll.ole32.CoReleaseServerProcess() if rc == 0: if self._queue: diff --git a/comtypes/hints.pyi b/comtypes/hints.pyi index 30b03e1d0..bc4a308db 100644 --- a/comtypes/hints.pyi +++ b/comtypes/hints.pyi @@ -27,6 +27,7 @@ import comtypes from comtypes import IUnknown as IUnknown, GUID as GUID from comtypes.automation import IDispatch as IDispatch, VARIANT as VARIANT from comtypes.server import IClassFactory as IClassFactory +from comtypes.server import localserver as localserver from comtypes.typeinfo import ITypeInfo as ITypeInfo from comtypes._safearray import tagSAFEARRAY as tagSAFEARRAY From a72815ac5ce419583f2bdbfebee1bedea17a82ae Mon Sep 17 00:00:00 2001 From: junkmd Date: Tue, 17 Dec 2024 21:06:23 +0900 Subject: [PATCH 3/7] Add type hints to `InprocServer`. --- comtypes/_comobject.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/comtypes/_comobject.py b/comtypes/_comobject.py index 249109535..298166e6d 100644 --- a/comtypes/_comobject.py +++ b/comtypes/_comobject.py @@ -394,16 +394,16 @@ def Unlock(self) -> None: class InprocServer(object): - def __init__(self): + def __init__(self) -> None: self.locks = c_long(0) - def Lock(self): + def Lock(self) -> None: _InterlockedIncrement(self.locks) - def Unlock(self): + def Unlock(self) -> None: _InterlockedDecrement(self.locks) - def DllCanUnloadNow(self): + def DllCanUnloadNow(self) -> int: if self.locks.value: return S_FALSE if COMObject._instances_: From fafd3d8a57ba413e6bba22aaaeae3955c962687e Mon Sep 17 00:00:00 2001 From: junkmd Date: Tue, 17 Dec 2024 21:06:23 +0900 Subject: [PATCH 4/7] Add type hints to `_InterlockedIncrement` and `_InterlockedDecrement`. --- comtypes/_comobject.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/comtypes/_comobject.py b/comtypes/_comobject.py index 298166e6d..a88f64d65 100644 --- a/comtypes/_comobject.py +++ b/comtypes/_comobject.py @@ -328,14 +328,14 @@ class Vtbl(Structure): _release = _lock.release # win 64 doesn't have these functions - def _InterlockedIncrement(ob): + def _InterlockedIncrement(ob: c_long) -> int: _acquire() refcnt = ob.value + 1 ob.value = refcnt _release() return refcnt - def _InterlockedDecrement(ob): + def _InterlockedDecrement(ob: c_long) -> int: _acquire() refcnt = ob.value - 1 ob.value = refcnt From ee04202ad80da885b93df36831a9f7cdda9dd5ee Mon Sep 17 00:00:00 2001 From: junkmd Date: Tue, 17 Dec 2024 21:06:23 +0900 Subject: [PATCH 5/7] Add type hints to `HRESULT_FROM_WIN32`. --- comtypes/_comobject.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comtypes/_comobject.py b/comtypes/_comobject.py index a88f64d65..55c3cc9f1 100644 --- a/comtypes/_comobject.py +++ b/comtypes/_comobject.py @@ -55,7 +55,7 @@ class E_NotImplemented(Exception): """COM method is not implemented""" -def HRESULT_FROM_WIN32(errcode): +def HRESULT_FROM_WIN32(errcode: Optional[int]) -> int: "Convert a Windows error code into a HRESULT value." if errcode is None: return 0x80000000 From 06941d66639085b95d59745d51e08bd73e15548d Mon Sep 17 00:00:00 2001 From: junkmd Date: Tue, 17 Dec 2024 21:06:23 +0900 Subject: [PATCH 6/7] Add type hints to `winerror`. --- comtypes/_comobject.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comtypes/_comobject.py b/comtypes/_comobject.py index 55c3cc9f1..ac01a02f0 100644 --- a/comtypes/_comobject.py +++ b/comtypes/_comobject.py @@ -64,7 +64,7 @@ def HRESULT_FROM_WIN32(errcode: Optional[int]) -> int: return (errcode & 0xFFFF) | 0x80070000 -def winerror(exc): +def winerror(exc: Exception) -> int: """Return the windows error code from a WindowsError or COMError instance.""" if isinstance(exc, COMError): From b5ea31e94f0969655795f459ee0897c56a58b384 Mon Sep 17 00:00:00 2001 From: junkmd Date: Tue, 17 Dec 2024 21:06:23 +0900 Subject: [PATCH 7/7] Add type hints to `_do_implement`. --- comtypes/_comobject.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/comtypes/_comobject.py b/comtypes/_comobject.py index ac01a02f0..7aa3a5a27 100644 --- a/comtypes/_comobject.py +++ b/comtypes/_comobject.py @@ -15,7 +15,7 @@ pointer, windll, ) -from typing import TYPE_CHECKING, Optional, Sequence +from typing import TYPE_CHECKING, Callable, Optional, Sequence from comtypes import COMError, IPersist, ReturnHRESULT, instancemethod from comtypes._memberspec import _encode_idl @@ -82,7 +82,7 @@ def winerror(exc: Exception) -> int: ) -def _do_implement(interface_name, method_name): +def _do_implement(interface_name: str, method_name: str) -> Callable[..., int]: def _not_implemented(*args): """Return E_NOTIMPL because the method is not implemented.""" _debug("unimplemented method %s_%s called", interface_name, method_name)