From a276f47aa4a0ce0fd6661d119f2357724b0e56b5 Mon Sep 17 00:00:00 2001 From: junkmd Date: Wed, 9 Oct 2024 22:52:53 +0900 Subject: [PATCH] Add early returns with `self` based on the conditions to `_coclass_meta.__new__`. --- comtypes/_meta.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/comtypes/_meta.py b/comtypes/_meta.py index 5f74109a..7c1fa24e 100644 --- a/comtypes/_meta.py +++ b/comtypes/_meta.py @@ -61,6 +61,18 @@ def __new__(cls, name, bases, namespace): if "_reg_clsid_" in namespace: clsid = namespace["_reg_clsid_"] comtypes.com_coclass_registry[str(clsid)] = self + + # `_coclass_pointer_meta` is a subclass inherited from `_coclass_meta`. + # In other words, when the `__new__` method of this metaclass is called, an + # instance of `_coclass_pointer_meta` might be created and assigned to `self`. + if isinstance(self, _coclass_pointer_meta): + # `self` is the `_coclass_pointer_meta` type or a `POINTER(coclass)` type. + # Prevent creating/registering a pointer to a pointer (to a pointer...), + # or specifying the metaclass type instance in the `bases` parameter when + # instantiating it, which would lead to infinite recursion. + # Depending on a version or revision of Python, this may be essential. + return self + PTR = _coclass_pointer_meta( f"POINTER({self.__name__})", (self, c_void_p),