Skip to content

Commit

Permalink
Use self instead of klass/new_cls in the __new__ of metaclass…
Browse files Browse the repository at this point in the history
…es. (#630)

* Use `self` instead of `new_cls`
in `_cominterface_meta.__new__`.

* To f-string.

* Use `self` instead of `klass`
in `_coclass_meta.__new__`.

* To f-string.

* Add a `type: ignore` comment.
  • Loading branch information
junkmd authored Oct 1, 2024
1 parent ca9b0f5 commit 711a9d6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
16 changes: 8 additions & 8 deletions comtypes/_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,26 @@ class _coclass_meta(type):
# will QueryInterface for the default interface: the first one on
# the coclass' _com_interfaces_ list.
def __new__(cls, name, bases, namespace):
klass = type.__new__(cls, name, bases, namespace)
self = type.__new__(cls, name, bases, namespace)
if bases == (object,):
return klass
return self
# XXX We should insist that a _reg_clsid_ is present.
if "_reg_clsid_" in namespace:
clsid = namespace["_reg_clsid_"]
comtypes.com_coclass_registry[str(clsid)] = klass
comtypes.com_coclass_registry[str(clsid)] = self
PTR = _coclass_pointer_meta(
"POINTER(%s)" % klass.__name__,
(klass, c_void_p),
f"POINTER({self.__name__})",
(self, c_void_p),
{
"__ctypes_from_outparam__": _wrap_coclass,
"from_param": classmethod(_coclass_from_param),
},
)
from ctypes import _pointer_type_cache
from ctypes import _pointer_type_cache # type: ignore

_pointer_type_cache[klass] = PTR
_pointer_type_cache[self] = PTR

return klass
return self


# will not work if we change the order of the two base classes!
Expand Down
26 changes: 13 additions & 13 deletions comtypes/_post_coinit/unknwn.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ class _cominterface_meta(type):
def __new__(cls, name, bases, namespace):
methods = namespace.pop("_methods_", None)
dispmethods = namespace.pop("_disp_methods_", None)
new_cls = type.__new__(cls, name, bases, namespace)
self = type.__new__(cls, name, bases, namespace)

if methods is not None:
new_cls._methods_ = methods
self._methods_ = methods
if dispmethods is not None:
new_cls._disp_methods_ = dispmethods
self._disp_methods_ = dispmethods

# If we sublass a COM interface, for example:
#
Expand All @@ -85,26 +85,26 @@ def __new__(cls, name, bases, namespace):
# subclass of POINTER(IUnknown) because of the way ctypes
# typechecks work.
if bases == (object,):
_ptr_bases = (new_cls, _compointer_base)
_ptr_bases = (self, _compointer_base)
else:
_ptr_bases = (new_cls, POINTER(bases[0]))
_ptr_bases = (self, POINTER(bases[0]))

# The interface 'new_cls' is used as a mixin.
# The interface 'self' is used as a mixin.
p = type(_compointer_base)(
"POINTER(%s)" % new_cls.__name__,
f"POINTER({self.__name__})",
_ptr_bases,
{"__com_interface__": new_cls, "_needs_com_addref_": None},
{"__com_interface__": self, "_needs_com_addref_": None},
)

from ctypes import _pointer_type_cache # type: ignore

_pointer_type_cache[new_cls] = p
_pointer_type_cache[self] = p

if new_cls._case_insensitive_:
new_cls._patch_case_insensitive_to_ptr_type(p)
new_cls._patch_reference_fix_to_ptrptr_type(p)
if self._case_insensitive_:
self._patch_case_insensitive_to_ptr_type(p)
self._patch_reference_fix_to_ptrptr_type(p)

return new_cls
return self

@staticmethod
def _patch_case_insensitive_to_ptr_type(p: Type) -> None:
Expand Down

0 comments on commit 711a9d6

Please sign in to comment.