Skip to content

Documentation changes for Py_tp_vectorcall #1

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 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
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
13 changes: 6 additions & 7 deletions Doc/c-api/type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -492,13 +492,6 @@ The following functions and structs are used to create
See :ref:`PyMemberDef documentation <pymemberdef-offsets>`
for details.

.. versionchanged:: 3.14

The field :c:member:`~PyTypeObject.tp_vectorcall` can only be set
since Python 3.14. On older versions, use
:c:member:`~PyTypeObject.tp_new` and/or
:c:member:`~PyTypeObject.tp_init`.

The following internal fields cannot be set at all when creating a heap
type:

Expand All @@ -523,6 +516,12 @@ The following functions and structs are used to create
:c:member:`~PyBufferProcs.bf_releasebuffer` are now available
under the :ref:`limited API <limited-c-api>`.

.. versionchanged:: 3.14

The field :c:member:`~PyTypeObject.tp_vectorcall` can now set
using ``Py_tp_vectorcall``. See the field's documentation
for details.

.. c:member:: void *pfunc

The desired value of the slot. In most cases, this is a pointer
Expand Down
39 changes: 34 additions & 5 deletions Doc/c-api/typeobj.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2137,11 +2137,40 @@ and :c:data:`PyType_Type` effectively act as defaults.)

.. c:member:: vectorcallfunc PyTypeObject.tp_vectorcall

Vectorcall function to use for calls of this type object.
In other words, it is used to implement
:ref:`vectorcall <vectorcall>` for ``type.__call__``.
If ``tp_vectorcall`` is ``NULL``, the default call implementation
using :meth:`~object.__new__` and :meth:`~object.__init__` is used.
A :ref:`vectorcall function <vectorcall>` to use for calls of this type
object (rather than instances).
In other words, ``tp_vectorcall`` can be used to optimize ``type.__call__``,
which typically returns a new instance of *type*.

As with any vectorcall function, if ``tp_vectorcall`` is ``NULL``,
the *tp_call* protocol (``Py_TYPE(type)->tp_call``) is used instead.

.. note::

The :ref:`vectorcall protocol <vectorcall>` requires that the vectorcall
function has the same behavior as the corresponding ``tp_call``.
This means that ``type->tp_vectorcall`` must match the behavior of
:c:expr:`Py_TYPE(type)->tp_call`.

Specifically, if *type* that uses the default metaclass,
``type->tp_vectorcall`` must behave the same as
:c:expr:`PyType_Type->tp_call`, which:

- calls :c:expr:`type->tp_new`,

- if the result is a subclass of *type*, calls :c:expr:`type->tp_init`
on the result of ``tp_new``, and

- returns the result of ``tp_new``.

Typically, ``tp_vectorcall`` is overridden to optimize this process
for specific :c:member:`~PyTypeObject.tp_new` and
:c:member:`~PyTypeObject.tp_init`.
When doing this for user-subclassable types, note that both can be
overridden (using :py:func:`~object.__new__` and
:py:func:`~object.__init__`, respectively).



**Inheritance:**

Expand Down
Loading