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

Conversation

junkmd
Copy link
Contributor
@junkmd junkmd commented Oct 29, 2024

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

Comment on lines 90 to 94
# 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.

@junkmd
Copy link
Contributor Author
junkmd commented Oct 30, 2024

I realized that with super().__init__ and recursion limits, this test might pass in Python 3.12 as well.

@junkmd junkmd requested a review from encukou October 30, 2024 15:09
@junkmd
Copy link
Contributor Author
junkmd commented Oct 31, 2024

I built 3.12 locally and ran this test, and it passed without any errors.

In #125783, I said:

Since this approach cannot be used in 3.12, the backport would be limited to 3.13.

However, this was because I had overlooked super().__init__ and the recursion limits.

@encukou encukou added needs backport to 3.12 only security fixes needs backport to 3.13 bugs and security fixes labels Oct 31, 2024
@encukou encukou merged commit 6c67446 into python:main Nov 1, 2024
34 checks passed
@miss-islington-app
Copy link

Thanks @junkmd for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 1, 2024
…nation of ctypes and metaclasses. (pythonGH-126126)

(cherry picked from commit 6c67446)

Co-authored-by: Jun Komoda <45822440+junkmd@users.noreply.github.com>
@bedevere-app
Copy link
bedevere-app bot commented Nov 1, 2024

GH-126275 is a backport of this pull request to the 3.13 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 1, 2024
…nation of ctypes and metaclasses. (pythonGH-126126)

(cherry picked from commit 6c67446)

Co-authored-by: Jun Komoda <45822440+junkmd@users.noreply.github.com>
@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Nov 1, 2024
@bedevere-app
Copy link
bedevere-app bot commented Nov 1, 2024

GH-126276 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 only security fixes label Nov 1, 2024
@junkmd junkmd deleted the test_metacls_PyCSimpleType_dunder_init branch November 1, 2024 13:30
encukou pushed a commit that referenced this pull request Nov 4, 2024
…ination of ctypes and metaclasses. (GH-126126) (GH-126275)

gh-125783: Add more tests to prevent regressions with the combination of ctypes and metaclasses. (GH-126126)
(cherry picked from commit 6c67446)

Co-authored-by: Jun Komoda <45822440+junkmd@users.noreply.github.com>
encukou pushed a commit that referenced this pull request Nov 4, 2024
…ination of ctypes and metaclasses. (GH-126126) (GH-126276)

gh-125783: Add more tests to prevent regressions with the combination of ctypes and metaclasses. (GH-126126)
(cherry picked from commit 6c67446)

Co-authored-by: Jun Komoda <45822440+junkmd@users.noreply.github.com>
picnixz pushed a commit to picnixz/cpython that referenced this pull request Dec 8, 2024
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0