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 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
Prev Previous commit
Next Next commit
Add super().__init__ calls and early returns to limit recursion.
  • Loading branch information
junkmd committed Oct 30, 2024
commit 59968701ad75526bc69c502cb5ef40079f723f53
19 changes: 14 additions & 5 deletions Lib/test/test_ctypes/test_c_simple_type_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,16 @@ class Sub(CtBase):
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):
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
if bases == (object,):
ptr_bases = (self, PtrBase)
else:
Expand Down Expand Up @@ -120,10 +125,14 @@ class Sub2(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):
super().__init__(name, bases, namespace)

# Avoid recursion.
# (See test_creating_pointer_in_dunder_new_2)
if isinstance(self, p_meta):
return self
p = p_meta(f"POINTER({self.__name__})", (self, c_void_p), {})
ctypes._pointer_type_cache[self] = p

Expand Down
Loading
0