Skip to content

Commit d12fe1d

Browse files
committed
Clarify how vectorcall works with subclassing
This is not described in detail in PEP 590; see python#13699 for the practical issue.
1 parent 438b1d6 commit d12fe1d

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

Doc/c-api/typeobj.rst

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -688,18 +688,31 @@ and :c:type:`PyType_Type` effectively act as defaults.)
688688

689689
Any class that sets ``_Py_TPFLAGS_HAVE_VECTORCALL`` must also set
690690
:c:member:`~PyTypeObject.tp_call` and make sure its behaviour is consistent
691-
with the *vectorcallfunc* function. This can be done by setting *tp_call* to
692-
``PyVectorcall_Call``:
691+
with the *vectorcallfunc* function.
692+
This can be done by setting *tp_call* to ``PyVectorcall_Call``:
693693

694694
.. c:function:: PyObject *PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *dict)
695695
696696
Call *callable*'s *vectorcallfunc* with positional and keyword
697697
arguments given in a tuple and dict, respectively.
698698
699+
This function is intended to be used in the ``tp_call`` slot.
700+
It does not fall back to ``tp_call`` and it currently does not check the
701+
``_Py_TPFLAGS_HAVE_VECTORCALL`` flag.
702+
To call an object, use one of the :c:func:`PyObject_Call <PyObject_Call>`
703+
functions instead.
704+
699705
.. note::
700706
701-
The semantics of slot are provisional and expected to be finalized
702-
in Python 3.9.
707+
It is not recommended for :ref:`heap types <heap-types>` to implement
708+
the vectorcall protocol.
709+
When a user sets ``__call__`` in Python code, only ``tp_call`` is updated,
710+
possibly making it inconsistent with the vectorcall function.
711+
712+
.. note::
713+
714+
The semantics of the ``tp_vectorcall_offset`` slot are provisional and
715+
expected to be finalized in Python 3.9.
703716
If you use vectorcall, plan for updating your code for Python 3.9.
704717
705718
.. versionchanged:: 3.8
@@ -709,14 +722,13 @@ and :c:type:`PyType_Type` effectively act as defaults.)
709722
710723
**Inheritance:**
711724
712-
This field is inherited by *static* subtypes together with
713-
:c:member:`~PyTypeObject.tp_call`: a subtype inherits both
714-
:c:member:`~PyTypeObject.tp_vectorcall_offset` and
715-
:c:member:`~PyTypeObject.tp_call` from its base type when the subtype’s
716-
:c:member:`~PyTypeObject.tp_call` and :c:member:`~PyTypeObject.tp_vectorcall_offset`
717-
are both NULL.
725+
This field is inherited by subtypes together with
726+
:c:member:`~PyTypeObject.tp_call`: a subtype inherits
727+
:c:member:`~PyTypeObject.tp_vectorcall_offset` from its base type when
728+
the subtype’s :c:member:`~PyTypeObject.tp_call` is NULL.
718729
719-
`Heap types`_ never inherit :c:member:`~PyTypeObject.tp_vectorcall_offset`.
730+
Note that `heap types`_ (including subclasses defined in Python) do not
731+
inherit the :const:`_Py_TPFLAGS_HAVE_VECTORCALL` flag.
720732
721733
722734
.. c:member:: getattrfunc PyTypeObject.tp_getattr
@@ -1148,6 +1160,15 @@ and :c:type:`PyType_Type` effectively act as defaults.)
11481160
This bit is set when the class implements the vectorcall protocol.
11491161
See :c:member:`~PyTypeObject.tp_vectorcall_offset` for details.
11501162

1163+
**Inheritance:**
1164+
1165+
This bit is set on *static* subtypes if ``tp_flags`` is not overridden:
1166+
a subtype inherits ``_Py_TPFLAGS_HAVE_VECTORCALL`` from its base type
1167+
when the subtype’s :c:member:`~PyTypeObject.tp_call` is NULL
1168+
and the subtype's ``Py_TPFLAGS_HEAPTYPE`` is not set.
1169+
1170+
`Heap types`_ do not inherit ``_Py_TPFLAGS_HAVE_VECTORCALL``.
1171+
11511172
.. note::
11521173

11531174
This flag is provisional and expected to become public in Python 3.9,

0 commit comments

Comments
 (0)