diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-07-11 11:53:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-11 11:53:40 (GMT) |
commit | 2eb9fe92b4180af0a9e51b1232df5a3d7d93788f (patch) | |
tree | 560446e3a0ac9983a47bbf18bddc1a9b3fb98daf | |
parent | 30870c834ce0e47cc689b56aed7ac468f6e1c1ad (diff) | |
download | cpython-2eb9fe92b4180af0a9e51b1232df5a3d7d93788f.zip cpython-2eb9fe92b4180af0a9e51b1232df5a3d7d93788f.tar.gz cpython-2eb9fe92b4180af0a9e51b1232df5a3d7d93788f.tar.bz2 |
[3.12] gh-103968: What's New: Add porting hints for PyType_From with metaclasses (GH-105698) (GH-106619)
gh-103968: What's New: Add porting hints for PyType_From with metaclasses (GH-105698)
(cherry picked from commit af5cf1e75136fcef967d4ebe1bc45f29e6dc1bcf)
Co-authored-by: Petr Viktorin <encukou@gmail.com>
-rw-r--r-- | Doc/whatsnew/3.12.rst | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index ef00f09..d6d7b7d 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -1836,7 +1836,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 |