From 47ec482cd96c8c8f1c0c4596eb16055568ec58d4 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Sat, 26 Oct 2024 00:23:16 +0900 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Petr Viktorin --- .../test_ctypes/test_c_simple_type_meta.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_ctypes/test_c_simple_type_meta.py b/Lib/test/test_ctypes/test_c_simple_type_meta.py index 5dddad81bdfaf3..b76238a5c530cd 100644 --- a/Lib/test/test_ctypes/test_c_simple_type_meta.py +++ b/Lib/test/test_ctypes/test_c_simple_type_meta.py @@ -10,16 +10,26 @@ def tearDown(self): # to not leak references, we must clean _pointer_type_cache ctypes._reset_cache() - def test_early_return_in_dunder_new_1(self): - # Such an implementation is used in `IUnknown` of `comtypes`. + def test_creating_pointer_in_dunder_new_1(self): + # Test metaclass whose instances are C types; when the type is + # created it automatically creates a pointer type for itself. + # The pointer type is also an instance of the metaclass. + # Such an implementation is used in `IUnknown` of the `comtypes` + # project. See gh-124520. class ct_meta(type): def __new__(cls, name, bases, namespace): self = super().__new__(cls, name, bases, namespace) + + # Avoid recursion: don't set up a pointer to + # a pointer (to a pointer...) if bases == (c_void_p,): + # When creating PtrBase itself, the name + # is not yet available return self if issubclass(self, PtrBase): return self + if bases == (object,): ptr_bases = (self, PtrBase) else: @@ -48,8 +58,8 @@ class Sub2(Sub): self.assertTrue(issubclass(POINTER(Sub2), POINTER(Sub))) self.assertTrue(issubclass(POINTER(Sub), POINTER(CtBase))) - def test_early_return_in_dunder_new_2(self): - # Such an implementation is used in `CoClass` of `comtypes`. + def test_creating_pointer_in_dunder_new_2(self): + # A simpler variant of the above, used in `CoClass` of `comtypes`. class ct_meta(type): def __new__(cls, name, bases, namespace):