-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
gh-125783: Add more tests to prevent regressions with the combination of ctypes and metaclasses. #126126
Conversation
# Test for modern metaclass initialization that does not | ||
# require recursion avoidance. | ||
|
||
class ct_meta(type): | ||
def __init__(self, name, bases, namespace): |
There was a problem hiding this comment.
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__
.
# 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! thank you.
I realized that with |
I built In #125783, I said:
However, this was because I had overlooked |
…nation of ctypes and metaclasses. (pythonGH-126126) (cherry picked from commit 6c67446) Co-authored-by: Jun Komoda <45822440+junkmd@users.noreply.github.com>
GH-126275 is a backport of this pull request to the 3.13 branch. |
…nation of ctypes and metaclasses. (pythonGH-126126) (cherry picked from commit 6c67446) Co-authored-by: Jun Komoda <45822440+junkmd@users.noreply.github.com>
GH-126276 is a backport of this pull request to the 3.12 branch. |
…nation of ctypes and metaclasses. (pythonGH-126126)
…nation of ctypes and metaclasses. (pythonGH-126126)
In #125881, tests were added for generating pointer types in
__new__
of metaclasses.This time, I've added tests for generating pointer types in
__init__
of metaclasses.This is internal-only, so I don’t think it needs a NEWS entry.
I would like to backport this to 3.13 as well.
Please see also #125783 (comment).
ctypes
and metaclasses. #125783