From dbbd282a74619beb201cd43b481c0b9ae8710683 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 01/20] Improve import section. --- comtypes/server/inprocserver.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/comtypes/server/inprocserver.py b/comtypes/server/inprocserver.py index 57935c30..c6ab9d79 100644 --- a/comtypes/server/inprocserver.py +++ b/comtypes/server/inprocserver.py @@ -1,12 +1,11 @@ import ctypes -from comtypes import COMObject, GUID -from comtypes.server import IClassFactory -from comtypes.hresult import * - -import sys import logging +import sys import winreg +from comtypes import GUID, COMObject +from comtypes.hresult import * +from comtypes.server import IClassFactory logger = logging.getLogger(__name__) _debug = logger.debug From 64f66193ad6b022de94a1c3d8c4e44268f0b1dda Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 02/20] Add type hints to `inprocserver.DllCanUnloadNow`. --- comtypes/server/inprocserver.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/comtypes/server/inprocserver.py b/comtypes/server/inprocserver.py index c6ab9d79..3d408780 100644 --- a/comtypes/server/inprocserver.py +++ b/comtypes/server/inprocserver.py @@ -2,6 +2,7 @@ import logging import sys import winreg +from typing import Literal from comtypes import GUID, COMObject from comtypes.hresult import * @@ -138,7 +139,7 @@ def DllGetClassObject(rclsid, riid, ppv): return E_FAIL -def DllCanUnloadNow(): +def DllCanUnloadNow() -> Literal[1]: # S_FALSE COMObject.__run_inprocserver__() result = COMObject.__server__.DllCanUnloadNow() # To avoid a memory leak when PyInitialize()/PyUninitialize() are From e5daa71b453692381bc76b25981748ce25305b54 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 03/20] Add type hints to `inprocserver.DllGetClassObject`. --- comtypes/server/inprocserver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comtypes/server/inprocserver.py b/comtypes/server/inprocserver.py index 3d408780..0a11dda8 100644 --- a/comtypes/server/inprocserver.py +++ b/comtypes/server/inprocserver.py @@ -109,7 +109,7 @@ def _setup_logging(clsid): logging.getLogger(name).setLevel(level) -def DllGetClassObject(rclsid, riid, ppv): +def DllGetClassObject(rclsid: int, riid: int, ppv: int) -> int: COMObject.__run_inprocserver__() iid = GUID.from_address(riid) From b6e41e355b57a2ccedb9fa579e73fc2f04dcda2b Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 04/20] Add type hints to `inprocserver._setup_logging`. --- comtypes/server/inprocserver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comtypes/server/inprocserver.py b/comtypes/server/inprocserver.py index 0a11dda8..e023cca1 100644 --- a/comtypes/server/inprocserver.py +++ b/comtypes/server/inprocserver.py @@ -70,7 +70,7 @@ def inproc_find_class(clsid): _logging_configured = False -def _setup_logging(clsid): +def _setup_logging(clsid: GUID) -> None: """Read from the registry, and configure the logging module. Currently, the handler (NTDebugHandler) is hardcoded. From 560634637503265671c8b34798c4a5989132d737 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 05/20] Add type hints to `inprocserver.inproc_find_class`. --- comtypes/server/inprocserver.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/comtypes/server/inprocserver.py b/comtypes/server/inprocserver.py index e023cca1..cfc00a8d 100644 --- a/comtypes/server/inprocserver.py +++ b/comtypes/server/inprocserver.py @@ -2,7 +2,7 @@ import logging import sys import winreg -from typing import Literal +from typing import Literal, Type from comtypes import GUID, COMObject from comtypes.hresult import * @@ -40,7 +40,7 @@ def IClassFactory_LockServer(self, this, fLock): _clsid_to_class = {} -def inproc_find_class(clsid): +def inproc_find_class(clsid: GUID) -> Type[COMObject]: if _clsid_to_class: return _clsid_to_class[clsid] From ed36294236f3d277911fd9cb1bd2690750aeb464 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 06/20] Add type hints to `inprocserver.ClassFactory.IClassFactory_LockServer`. --- comtypes/server/inprocserver.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/comtypes/server/inprocserver.py b/comtypes/server/inprocserver.py index cfc00a8d..322b5457 100644 --- a/comtypes/server/inprocserver.py +++ b/comtypes/server/inprocserver.py @@ -2,7 +2,7 @@ import logging import sys import winreg -from typing import Literal, Type +from typing import Any, Literal, Type from comtypes import GUID, COMObject from comtypes.hresult import * @@ -28,7 +28,7 @@ def IClassFactory_CreateInstance(self, this, punkOuter, riid, ppv): _debug("CreateInstance() -> %s", result) return result - def IClassFactory_LockServer(self, this, fLock): + def IClassFactory_LockServer(self, this: Any, fLock: bool) -> Literal[0]: if fLock: COMObject.__server__.Lock() else: From a296d11a1ddc7c93036d74eae62f43cd7f414247 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 07/20] Add type hints to `inprocserver.ClassFactory.IClassFactory_CreateInstance`. --- comtypes/server/inprocserver.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/comtypes/server/inprocserver.py b/comtypes/server/inprocserver.py index 322b5457..eb9d5598 100644 --- a/comtypes/server/inprocserver.py +++ b/comtypes/server/inprocserver.py @@ -2,9 +2,9 @@ import logging import sys import winreg -from typing import Any, Literal, Type +from typing import Any, Literal, Optional, Type -from comtypes import GUID, COMObject +from comtypes import GUID, COMObject, IUnknown from comtypes.hresult import * from comtypes.server import IClassFactory @@ -22,7 +22,13 @@ def __init__(self, cls): super(ClassFactory, self).__init__() self._cls = cls - def IClassFactory_CreateInstance(self, this, punkOuter, riid, ppv): + def IClassFactory_CreateInstance( + self, + this: Any, + punkOuter: Optional[Type["ctypes._Pointer[IUnknown]"]], + riid: "ctypes._Pointer[GUID]", + ppv: ctypes.c_void_p, + ) -> int: _debug("ClassFactory.CreateInstance(%s)", riid[0]) result = self._cls().IUnknown_QueryInterface(None, riid, ppv) _debug("CreateInstance() -> %s", result) From a073df48b892d35a5f9bcfff3dac8734c6455aac Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 08/20] Add type hints to `inprocserver.ClassFactory.__init__`. --- comtypes/server/inprocserver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comtypes/server/inprocserver.py b/comtypes/server/inprocserver.py index eb9d5598..cff23eda 100644 --- a/comtypes/server/inprocserver.py +++ b/comtypes/server/inprocserver.py @@ -18,7 +18,7 @@ class ClassFactory(COMObject): _com_interfaces_ = [IClassFactory] - def __init__(self, cls): + def __init__(self, cls: Type[COMObject]) -> None: super(ClassFactory, self).__init__() self._cls = cls From 88edb9aff220e8bf1a76cc85d1d3dcaa5debb0ef Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 09/20] Improve import section. --- comtypes/server/localserver.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/comtypes/server/localserver.py b/comtypes/server/localserver.py index 1f50d21a..bf90cc3f 100644 --- a/comtypes/server/localserver.py +++ b/comtypes/server/localserver.py @@ -1,10 +1,11 @@ +import logging +import queue import sys from ctypes import * + import comtypes from comtypes.hresult import * from comtypes.server import IClassFactory -import logging -import queue logger = logging.getLogger(__name__) _debug = logger.debug From 39203243da611780a98babdccc70f81320862245 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 10/20] Add type hints to `localserver.run`. --- comtypes/server/localserver.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/comtypes/server/localserver.py b/comtypes/server/localserver.py index bf90cc3f..1e68cc99 100644 --- a/comtypes/server/localserver.py +++ b/comtypes/server/localserver.py @@ -2,6 +2,7 @@ import queue import sys from ctypes import * +from typing import Sequence, Type import comtypes from comtypes.hresult import * @@ -17,7 +18,7 @@ REGCLS_SURROGATE = 8 # must be used when a surrogate process -def run(classes): +def run(classes: Sequence[Type[comtypes.COMObject]]) -> None: classobjects = [ClassFactory(cls) for cls in classes] comtypes.COMObject.__run_localserver__(classobjects) From c32983f68faa12ee4ef6843f0389dab096fa24ab Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 11/20] Add type hints to class variables and instance variables of `localserver.ClassFactory`. --- comtypes/server/localserver.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/comtypes/server/localserver.py b/comtypes/server/localserver.py index 1e68cc99..6a740312 100644 --- a/comtypes/server/localserver.py +++ b/comtypes/server/localserver.py @@ -2,7 +2,7 @@ import queue import sys from ctypes import * -from typing import Sequence, Type +from typing import Optional, Sequence, Type import comtypes from comtypes.hresult import * @@ -25,9 +25,9 @@ def run(classes: Sequence[Type[comtypes.COMObject]]) -> None: class ClassFactory(comtypes.COMObject): _com_interfaces_ = [IClassFactory] - _locks = 0 - _queue = None - regcls = REGCLS_MULTIPLEUSE + _locks: int = 0 + _queue: Optional[queue.Queue] = None + regcls: int = REGCLS_MULTIPLEUSE def __init__(self, cls, *args, **kw): super(ClassFactory, self).__init__() From 9c0a1b9aa1a9be9b4896519d5942ea477860f704 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 12/20] Add type hints to `localserver.ClassFactory.__init__`. --- comtypes/server/localserver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comtypes/server/localserver.py b/comtypes/server/localserver.py index 6a740312..83acc009 100644 --- a/comtypes/server/localserver.py +++ b/comtypes/server/localserver.py @@ -29,7 +29,7 @@ class ClassFactory(comtypes.COMObject): _queue: Optional[queue.Queue] = None regcls: int = REGCLS_MULTIPLEUSE - def __init__(self, cls, *args, **kw): + def __init__(self, cls: Type[comtypes.COMObject], *args, **kw) -> None: super(ClassFactory, self).__init__() self._cls = cls self._register_class() From 8515fb41085abf4825f7b31fd6de9dee775fa250 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 13/20] Add type hints to `localserver.ClassFactory.IUnknown_AddRef`. --- comtypes/server/localserver.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/comtypes/server/localserver.py b/comtypes/server/localserver.py index 83acc009..efe26f7a 100644 --- a/comtypes/server/localserver.py +++ b/comtypes/server/localserver.py @@ -2,7 +2,7 @@ import queue import sys from ctypes import * -from typing import Optional, Sequence, Type +from typing import Any, Optional, Sequence, Type import comtypes from comtypes.hresult import * @@ -36,7 +36,7 @@ def __init__(self, cls: Type[comtypes.COMObject], *args, **kw) -> None: self._args = args self._kw = kw - def IUnknown_AddRef(self, this): + def IUnknown_AddRef(self, this: Any) -> int: return 2 def IUnknown_Release(self, this): From 70e350d9caadcc8ccda62965de4bd83ca476b5a3 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 14/20] Add type hints to `localserver.ClassFactory.IUnknown_Release`. --- comtypes/server/localserver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comtypes/server/localserver.py b/comtypes/server/localserver.py index efe26f7a..9601e36b 100644 --- a/comtypes/server/localserver.py +++ b/comtypes/server/localserver.py @@ -39,7 +39,7 @@ def __init__(self, cls: Type[comtypes.COMObject], *args, **kw) -> None: def IUnknown_AddRef(self, this: Any) -> int: return 2 - def IUnknown_Release(self, this): + def IUnknown_Release(self, this: Any) -> int: return 1 def _register_class(self): From a876fc23a540e8e6db1fe442c1842ae645b7a22c Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 15/20] Add type hints to `localserver.ClassFactory._register_class`. --- comtypes/server/localserver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comtypes/server/localserver.py b/comtypes/server/localserver.py index 9601e36b..967cbbc4 100644 --- a/comtypes/server/localserver.py +++ b/comtypes/server/localserver.py @@ -42,7 +42,7 @@ def IUnknown_AddRef(self, this: Any) -> int: def IUnknown_Release(self, this: Any) -> int: return 1 - def _register_class(self): + def _register_class(self) -> None: regcls = getattr(self._cls, "_regcls_", self.regcls) cookie = c_ulong() ptr = self._com_pointers_[comtypes.IUnknown._iid_] From 61f49fdcf36bebcc0c0ab6d91dc044512f562839 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 16/20] Add type hints to `localserver.ClassFactory._revoke_class`. --- comtypes/server/localserver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comtypes/server/localserver.py b/comtypes/server/localserver.py index 967cbbc4..82d2fdf0 100644 --- a/comtypes/server/localserver.py +++ b/comtypes/server/localserver.py @@ -57,7 +57,7 @@ def _register_class(self) -> None: ) self.cookie = cookie - def _revoke_class(self): + def _revoke_class(self) -> None: oledll.ole32.CoRevokeClassObject(self.cookie) def CreateInstance(self, this, punkOuter, riid, ppv): From 5a7b918da36d1cb50df7c5ee7d82ad74cba5943b Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 17/20] Add type hints to `localserver.ClassFactory.CreateInstance`. --- comtypes/server/localserver.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/comtypes/server/localserver.py b/comtypes/server/localserver.py index 82d2fdf0..5c21a3d9 100644 --- a/comtypes/server/localserver.py +++ b/comtypes/server/localserver.py @@ -2,12 +2,16 @@ import queue import sys from ctypes import * -from typing import Any, Optional, Sequence, Type +from typing import TYPE_CHECKING, Any, Optional, Sequence, Type import comtypes from comtypes.hresult import * from comtypes.server import IClassFactory +if TYPE_CHECKING: + from ctypes import _Pointer + + logger = logging.getLogger(__name__) _debug = logger.debug @@ -60,7 +64,13 @@ def _register_class(self) -> None: def _revoke_class(self) -> None: oledll.ole32.CoRevokeClassObject(self.cookie) - def CreateInstance(self, this, punkOuter, riid, ppv): + def CreateInstance( + self, + this: Any, + punkOuter: Optional[Type["_Pointer[comtypes.IUnknown]"]], + riid: "_Pointer[comtypes.GUID]", + ppv: c_void_p, + ) -> int: _debug("ClassFactory.CreateInstance(%s)", riid[0]) obj = self._cls(*self._args, **self._kw) result = obj.IUnknown_QueryInterface(None, riid, ppv) From 0ee20fb20edb2b02a3fead1432c018d9af91ffcd Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 18/20] Add type hints to `localserver.ClassFactory.LockServer`. --- comtypes/server/localserver.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/comtypes/server/localserver.py b/comtypes/server/localserver.py index 5c21a3d9..d1143504 100644 --- a/comtypes/server/localserver.py +++ b/comtypes/server/localserver.py @@ -2,7 +2,7 @@ import queue import sys from ctypes import * -from typing import TYPE_CHECKING, Any, Optional, Sequence, Type +from typing import TYPE_CHECKING, Any, Literal, Optional, Sequence, Type import comtypes from comtypes.hresult import * @@ -77,7 +77,7 @@ def CreateInstance( _debug("CreateInstance() -> %s", result) return result - def LockServer(self, this, fLock): + def LockServer(self, this: Any, fLock: bool) -> Literal[0]: if fLock: comtypes.COMObject.__server__.Lock() else: From 14f72417dcac36e882545f55a570a91586fb791b Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 19/20] Remove wildcard imports (`from comtypes.hresult import *`). --- comtypes/server/inprocserver.py | 11 +++++------ comtypes/server/localserver.py | 4 ++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/comtypes/server/inprocserver.py b/comtypes/server/inprocserver.py index cff23eda..33f7e8c2 100644 --- a/comtypes/server/inprocserver.py +++ b/comtypes/server/inprocserver.py @@ -4,8 +4,7 @@ import winreg from typing import Any, Literal, Optional, Type -from comtypes import GUID, COMObject, IUnknown -from comtypes.hresult import * +from comtypes import GUID, COMObject, IUnknown, hresult from comtypes.server import IClassFactory logger = logging.getLogger(__name__) @@ -39,7 +38,7 @@ def IClassFactory_LockServer(self, this: Any, fLock: bool) -> Literal[0]: COMObject.__server__.Lock() else: COMObject.__server__.Unlock() - return S_OK + return hresult.S_OK # will be set by py2exe boot script 'from outside' @@ -133,7 +132,7 @@ def DllGetClassObject(rclsid: int, riid: int, ppv: int) -> int: cls = inproc_find_class(clsid) if not cls: - return CLASS_E_CLASSNOTAVAILABLE + return hresult.CLASS_E_CLASSNOTAVAILABLE result = ClassFactory(cls).IUnknown_QueryInterface( None, ctypes.pointer(iid), ctypes.c_void_p(ppv) @@ -142,7 +141,7 @@ def DllGetClassObject(rclsid: int, riid: int, ppv: int) -> int: return result except Exception: _critical("DllGetClassObject", exc_info=True) - return E_FAIL + return hresult.E_FAIL def DllCanUnloadNow() -> Literal[1]: # S_FALSE @@ -150,4 +149,4 @@ def DllCanUnloadNow() -> Literal[1]: # S_FALSE result = COMObject.__server__.DllCanUnloadNow() # To avoid a memory leak when PyInitialize()/PyUninitialize() are # called several times, we refuse to unload the dll. - return S_FALSE + return hresult.S_FALSE diff --git a/comtypes/server/localserver.py b/comtypes/server/localserver.py index d1143504..5d4d89a5 100644 --- a/comtypes/server/localserver.py +++ b/comtypes/server/localserver.py @@ -5,7 +5,7 @@ from typing import TYPE_CHECKING, Any, Literal, Optional, Sequence, Type import comtypes -from comtypes.hresult import * +from comtypes import hresult from comtypes.server import IClassFactory if TYPE_CHECKING: @@ -82,4 +82,4 @@ def LockServer(self, this: Any, fLock: bool) -> Literal[0]: comtypes.COMObject.__server__.Lock() else: comtypes.COMObject.__server__.Unlock() - return S_OK + return hresult.S_OK From 864bebe5527fce9375b1fe61f661ed1c0da1f19f Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:57:47 +0000 Subject: [PATCH 20/20] Remove wildcard imports (`from ctypes import *`). --- comtypes/server/localserver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comtypes/server/localserver.py b/comtypes/server/localserver.py index 5d4d89a5..62907792 100644 --- a/comtypes/server/localserver.py +++ b/comtypes/server/localserver.py @@ -1,7 +1,7 @@ import logging import queue import sys -from ctypes import * +from ctypes import byref, c_ulong, c_void_p, oledll from typing import TYPE_CHECKING, Any, Literal, Optional, Sequence, Type import comtypes