Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-103968: Deprecate creating heap types whose metaclass has custom tp_new.#103972
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4 Misc/NEWS.d/next/C API/2023-04-28-18-04-38.gh-issue-103968.EnVvOx.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| :c:func:`PyType_FromSpec` and its variants now allow creating classes whose | ||
| metaclass overrides :c:member:`~PyTypeObject.tp_new`. The ``tp_new`` is | ||
| ignored. This behavior is deprecated and will be disallowed in 3.14+. The | ||
| new :c:func:`PyType_FromMetaclass` already disallows it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3830,9 +3830,10 @@ check_basicsize_includes_size_and_offsets(PyTypeObject* type) | ||
| return 1; | ||
| } | ||
| PyObject * | ||
| PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module, | ||
| PyType_Spec *spec, PyObject *bases_in) | ||
| static PyObject * | ||
| _PyType_FromMetaclass_impl( | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| PyTypeObject *metaclass, PyObject *module, | ||
| PyType_Spec *spec, PyObject *bases_in, int _allow_tp_new) | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| { | ||
| /* Invariant: A non-NULL value in one of these means this function holds | ||
| * a strong reference or owns allocated memory. | ||
| @@ -4007,9 +4008,21 @@ PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module, | ||
| goto finally; | ||
| } | ||
| if (metaclass->tp_new != PyType_Type.tp_new) { | ||
| PyErr_SetString(PyExc_TypeError, | ||
| "Metaclasses with custom tp_new are not supported."); | ||
| goto finally; | ||
| if (_allow_tp_new) { | ||
| if (PyErr_WarnFormat( | ||
| PyExc_DeprecationWarning, 1, | ||
| "Using PyType_Spec with metaclasses that have custom " | ||
| "tp_new is deprecated and will no longer be allowed in " | ||
| "Python 3.14.") < 0) { | ||
| goto finally; | ||
| } | ||
| } | ||
| else { | ||
| PyErr_SetString( | ||
| PyExc_TypeError, | ||
| "Metaclasses with custom tp_new are not supported."); | ||
| goto finally; | ||
| } | ||
| } | ||
| /* Calculate best base, and check that all bases are type objects */ | ||
| @@ -4196,22 +4209,29 @@ PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module, | ||
| return (PyObject*)res; | ||
| } | ||
| PyObject * | ||
| PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module, | ||
| PyType_Spec *spec, PyObject *bases_in) | ||
| { | ||
| return _PyType_FromMetaclass_impl(metaclass, module, spec, bases_in, 0); | ||
| } | ||
| PyObject * | ||
| PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases) | ||
| { | ||
| return PyType_FromMetaclass(NULL, module, spec, bases); | ||
| return _PyType_FromMetaclass_impl(NULL, module, spec, bases, 1); | ||
| } | ||
| PyObject * | ||
| PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases) | ||
| { | ||
| return PyType_FromMetaclass(NULL, NULL, spec, bases); | ||
| return _PyType_FromMetaclass_impl(NULL, NULL, spec, bases, 1); | ||
| } | ||
| PyObject * | ||
| PyType_FromSpec(PyType_Spec *spec) | ||
| { | ||
| return PyType_FromMetaclass(NULL, NULL, spec, NULL); | ||
| return _PyType_FromMetaclass_impl(NULL, NULL, spec, NULL, 1); | ||
| } | ||
| PyObject * | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
This could be nested in a
.. deprecated:: 3.12block I suppose? And below.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.
It's mentioned in
.. versionchanged:: 3.12in the affected functions.Their docs are just
Equivalent to ``PyType_FromMetaclass(NULL, NULL, arg, NULL, NULL)``and a bunch of change notes, so thePyType_FromMetaclassdocs need this parenthetical to be complete.