10000 gh-125783: Add tests to prevent regressions with the combination of `ctypes` and metaclasses. by junkmd · Pull Request #125881 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-125783: Add tests to prevent regressions with the combination of ctypes and metaclasses. #125881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add a test.
Such an implementation is used in `IUnknown` of `comtypes`.
  • Loading branch information
junkmd committed Oct 24, 2024
commit 80deddbc9f3720c1ca49afaec1bb801728b9ffa3
49 changes: 49 additions & 0 deletions Lib/test/test_ctypes/test_c_simple_type_meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import unittest
import ctypes
from ctypes import POINTER, c_void_p

from ._support import PyCSimpleType


class PyCSimpleTypeAsMetaclassTest(unittest.TestCase):
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`.

class _ct_meta(type):
def __new__(cls, name, bases, namespace):
self = super().__new__(cls, name, bases, namespace)
if bases == (c_void_p,):
return self
if issubclass(self, _PtrBase):
return self
if bases == (object,):
_ptr_bases = (self, _PtrBase)
else:
_ptr_bases = (self, POINTER(bases[0]))
p = _p_meta(f"POINTER({self.__name__})", _ptr_bases, {})
ctypes._pointer_type_cache[self] = p
return self

class _p_meta(PyCSimpleType, _ct_meta):
pass

class _PtrBase(c_void_p, metaclass=_p_meta):
pass

class _CtBase(object, metaclass=_ct_meta):
pass

class _Sub(_CtBase):
pass

class _Sub2(_Sub):
pass

self.assertIsInstance(POINTER(_Sub2), _p_meta)
self.assertTrue(issubclass(POINTER(_Sub2), _Sub2))
self.assertTrue(issubclass(POINTER(_Sub2), POINTER(_Sub)))
self.assertTrue(issubclass(POINTER(_Sub), POINTER(_CtBase)))
0