Skip to content
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-103968: What's New: Add porting hints for PyType_From with metaclasses #105698

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Changes from all 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
26 changes: 25 additions & 1 deletion Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1776,7 +1776,31 @@ Porting to Python 3.12
allowing incomplete initialization.

Note that :c:func:`PyType_FromMetaclass` (added in Python 3.12)
already disallows creating classes whose metaclass overrides ``tp_new``.
already disallows creating classes whose metaclass overrides ``tp_new``
(:meth:`~object.__new__` in Python).

Since ``tp_new`` overrides almost everything ``PyType_From*`` functions do,
the two are incompatible with each other.
The existing behavior -- ignoring the metaclass for several steps
of type creation -- is unsafe in general, since (meta)classes assume that
``tp_new`` was called.
There is no simple general workaround. One of the following may work for you:

- If you control the metaclass, avoid using ``tp_new`` in it:

- If initialization can be skipped, it can be done in
:c:member:`~PyTypeObject.tp_init` instead.
- If the metaclass doesn't need to be instantiated from Python,
set its ``tp_new`` to ``NULL`` using
the :const:`Py_TPFLAGS_DISALLOW_INSTANTIATION` flag.
This makes it acceptable for ``PyType_From*`` functions.

- Avoid ``PyType_From*`` functions: if you don't need C-specific features
(slots or setting the instance size), create types by :ref:`calling <call>`
the metaclass.

- If you *know* the ``tp_new`` can be skipped safely, filter the deprecation
warning out using :func:`warnings.catch_warnings` from Python.

* :c:var:`PyOS_InputHook` and :c:var:`PyOS_ReadlineFunctionPointer` are no
longer called in :ref:`subinterpreters <sub-interpreter-support>`. This is
Expand Down