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

Skip to content

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

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
Nov 1, 2024
Merged
Changes from 2 commits
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
56 changes: 56 additions & 0 deletions Lib/test/test_ctypes/test_c_simple_type_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,59 @@ class Sub(CtBase):

self.assertIsInstance(POINTER(Sub), p_meta)
self.assertTrue(issubclass(POINTER(Sub), Sub))

def test_creating_pointer_in_dunder_init_1(self):
# Test for modern metaclass initialization that does not
# require recursion avoidance.

class ct_meta(type):
def __init__(self, name, bases, namespace):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Please don't leave out the super().__init__ call when subclassing.
  • Please do limit recursion, as with __new__.
Suggested change
# Test for modern metaclass initialization that does not
# require recursion avoidance.
class ct_meta(type):
def __init__(self, name, bases, namespace):
class ct_meta(type):
def __init__(self, name, bases, namespace):
super().__init__(name, bases, namespace)
# Avoid recursion.
# (See test_creating_pointer_in_dunder_new_1)
if bases == (c_void_p,):
return
if issubclass(self, PtrBase):
return

(And similar in test_creating_pointer_in_dunder_init_2.)

The reason you don't need recursion avoidance here is that the internal PyCSimpleType is not set up for diamond inheritance patterns, and doesn't call super().__init__ (that is, ct_meta.__init__ here). That's arguably a bug. (It definitely would be one if we wanted to support such subclassing.)
If you switch the metaclass bases, writing class p_meta(ct_meta, PyCSimpleType), recursion avoidance becomes necessary again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! thank you.

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

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)))

def test_creating_pointer_in_dunder_init_2(self):
# A simpler variant of the above.

class ct_meta(type):
def __init__(self, name, bases, namespace):
p = p_meta(f"POINTER({self.__name__})", (self, c_void_p), {})
ctypes._pointer_type_cache[self] = p

class p_meta(PyCSimpleType, ct_meta):
pass

class Core(object):
pass

class CtBase(Core, metaclass=ct_meta):
pass

class Sub(CtBase):
pass

self.assertIsInstance(POINTER(Sub), p_meta)
self.assertTrue(issubclass(POINTER(Sub), Sub))
Loading
0