From a9a54429e301d56570b7d229ddbff47bcb1e3bfd Mon Sep 17 00:00:00 2001 From: junkmd Date: Mon, 7 Oct 2024 22:10:24 +0900 Subject: [PATCH 1/6] Add a comment and `pass` to `_compointer_meta`. --- comtypes/_post_coinit/unknwn.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/comtypes/_post_coinit/unknwn.py b/comtypes/_post_coinit/unknwn.py index 85e23d2f..81b7ab97 100644 --- a/comtypes/_post_coinit/unknwn.py +++ b/comtypes/_post_coinit/unknwn.py @@ -369,10 +369,11 @@ def _make_methods(self, methods: List[_ComMemberSpec]) -> None: ################################################################ +# will not work if we change the order of the two base classes! class _compointer_meta(type(c_void_p), _cominterface_meta): - "metaclass for COM interface pointer classes" + """metaclass for COM interface pointer classes""" - # no functionality, but needed to avoid a metaclass conflict + pass # no functionality, but needed to avoid a metaclass conflict class _compointer_base(c_void_p, metaclass=_compointer_meta): From a5990ebf0842ba0a69a5ba99a243de2d425d12ef Mon Sep 17 00:00:00 2001 From: junkmd Date: Mon, 7 Oct 2024 22:10:24 +0900 Subject: [PATCH 2/6] Add comments to `_coclass_pointer_meta`. --- comtypes/_meta.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/comtypes/_meta.py b/comtypes/_meta.py index 14ef4c78..e7718a1d 100644 --- a/comtypes/_meta.py +++ b/comtypes/_meta.py @@ -67,4 +67,6 @@ def __new__(cls, name, bases, namespace): # will not work if we change the order of the two base classes! class _coclass_pointer_meta(type(c_void_p), _coclass_meta): - pass + # metaclass for CoClass pointer + + pass # no functionality, but needed to avoid a metaclass conflict From 181a6c835ababd1e733420af9626d2c236c384db Mon Sep 17 00:00:00 2001 From: junkmd Date: Tue, 8 Oct 2024 00:43:17 +0900 Subject: [PATCH 3/6] Add a `HACK` comment to `_coclass_meta.__new__`. --- comtypes/_meta.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/comtypes/_meta.py b/comtypes/_meta.py index e7718a1d..5e4659ac 100644 --- a/comtypes/_meta.py +++ b/comtypes/_meta.py @@ -45,6 +45,17 @@ class _coclass_meta(type): def __new__(cls, name, bases, namespace): self = type.__new__(cls, name, bases, namespace) if bases == (object,): + # HACK: Could this conditional branch be removed since it is never reached? + # Since definition is `class CoClass(COMObject, metaclass=_coclass_meta)`, + # the `bases` parameter passed to `_coclass_meta.__new__` would be + # `(COMObject,)`. + # Moreover, since `COMObject` derives from `object` and does not specify + # a metaclass, `(object,)` will not be passed as the `bases` parameter + # to `_coclass_meta.__new__`. + # The reason for this implementation might be a remnant of the differences + # in how metaclasses work between Python 3.x and Python 2.x. + # If there are no problems with the versions of Python that `comtypes` + # supports, this removal could make the process flow easier to understand. return self # XXX We should insist that a _reg_clsid_ is present. if "_reg_clsid_" in namespace: From 7289901f8f41436d1155bf85ea3264acec04aefa Mon Sep 17 00:00:00 2001 From: junkmd Date: Tue, 8 Oct 2024 00:43:17 +0900 Subject: [PATCH 4/6] Add a `HACK` comment to `_cominterface_meta.__new__`. --- comtypes/_post_coinit/unknwn.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/comtypes/_post_coinit/unknwn.py b/comtypes/_post_coinit/unknwn.py index 81b7ab97..1c2527ab 100644 --- a/comtypes/_post_coinit/unknwn.py +++ b/comtypes/_post_coinit/unknwn.py @@ -90,6 +90,14 @@ def __new__(cls, name, bases, namespace): _ptr_bases = (self, POINTER(bases[0])) # The interface 'self' is used as a mixin. + # HACK: Could `type(_compointer_base)` be replaced with `_compointer_meta`? + # `type(klass)` returns its metaclass. + # Since this specification, `type(_compointer_base)` will return + # `_compointer_meta` as per the class definition. + # The reason for this implementation might be a remnant of the differences in + # how metaclasses work between Python 3.x and Python 2.x. + # If there are no problems with the versions of Python that `comtypes` + # supports, this replacement could make the process flow easier to understand. p = type(_compointer_base)( f"POINTER({self.__name__})", _ptr_bases, From 4900009414487e8575bf9020563378d22ad672b2 Mon Sep 17 00:00:00 2001 From: junkmd Date: Tue, 8 Oct 2024 00:43:17 +0900 Subject: [PATCH 5/6] Add comments for `_ptr_bases`. --- comtypes/_post_coinit/unknwn.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/comtypes/_post_coinit/unknwn.py b/comtypes/_post_coinit/unknwn.py index 1c2527ab..6fc30331 100644 --- a/comtypes/_post_coinit/unknwn.py +++ b/comtypes/_post_coinit/unknwn.py @@ -85,8 +85,10 @@ def __new__(cls, name, bases, namespace): # subclass of POINTER(IUnknown) because of the way ctypes # typechecks work. if bases == (object,): + # `self` is `IUnknown` type. _ptr_bases = (self, _compointer_base) else: + # `self` is an interface type derived from `IUnknown`. _ptr_bases = (self, POINTER(bases[0])) # The interface 'self' is used as a mixin. From fe1ed82e76d01c5ba1d0c0ec52e4256860440309 Mon Sep 17 00:00:00 2001 From: junkmd Date: Tue, 8 Oct 2024 08:23:03 +0900 Subject: [PATCH 6/6] Small fixes for comments. --- comtypes/_meta.py | 6 +++--- comtypes/_post_coinit/unknwn.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/comtypes/_meta.py b/comtypes/_meta.py index 5e4659ac..5f74109a 100644 --- a/comtypes/_meta.py +++ b/comtypes/_meta.py @@ -47,11 +47,11 @@ def __new__(cls, name, bases, namespace): if bases == (object,): # HACK: Could this conditional branch be removed since it is never reached? # Since definition is `class CoClass(COMObject, metaclass=_coclass_meta)`, - # the `bases` parameter passed to `_coclass_meta.__new__` would be + # the `bases` parameter passed to the `_coclass_meta.__new__` would be # `(COMObject,)`. - # Moreover, since `COMObject` derives from `object` and does not specify + # Moreover, since the `COMObject` derives from `object` and does not specify # a metaclass, `(object,)` will not be passed as the `bases` parameter - # to `_coclass_meta.__new__`. + # to the `_coclass_meta.__new__`. # The reason for this implementation might be a remnant of the differences # in how metaclasses work between Python 3.x and Python 2.x. # If there are no problems with the versions of Python that `comtypes` diff --git a/comtypes/_post_coinit/unknwn.py b/comtypes/_post_coinit/unknwn.py index 6fc30331..7d10b47e 100644 --- a/comtypes/_post_coinit/unknwn.py +++ b/comtypes/_post_coinit/unknwn.py @@ -85,7 +85,7 @@ def __new__(cls, name, bases, namespace): # subclass of POINTER(IUnknown) because of the way ctypes # typechecks work. if bases == (object,): - # `self` is `IUnknown` type. + # `self` is the `IUnknown` type. _ptr_bases = (self, _compointer_base) else: # `self` is an interface type derived from `IUnknown`. @@ -94,8 +94,8 @@ def __new__(cls, name, bases, namespace): # The interface 'self' is used as a mixin. # HACK: Could `type(_compointer_base)` be replaced with `_compointer_meta`? # `type(klass)` returns its metaclass. - # Since this specification, `type(_compointer_base)` will return - # `_compointer_meta` as per the class definition. + # Since this specification, `type(_compointer_base)` will return the + # `_compointer_meta` type as per the class definition. # The reason for this implementation might be a remnant of the differences in # how metaclasses work between Python 3.x and Python 2.x. # If there are no problems with the versions of Python that `comtypes`