Skip to content

gh-93274: Make vectorcall safe on mutable classes #95437

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 9 commits into from
Aug 4, 2022
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
9 changes: 9 additions & 0 deletions Doc/c-api/call.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ This bears repeating:
A class supporting vectorcall **must** also implement
:c:member:`~PyTypeObject.tp_call` with the same semantics.

.. versionchanged:: 3.12

The :const:`Py_TPFLAGS_HAVE_VECTORCALL` flag is now removed from a class
when the class's :py:meth:`~object.__call__` method is reassigned.
(This internally sets :c:member:`~PyTypeObject.tp_call` only, and thus
may make it behave differently than the vectorcall function.)
In earlier Python versions, vectorcall should only be used with
:const:`immutable <Py_TPFLAGS_IMMUTABLETYPE>` or static types.

A class should not implement vectorcall if that would be slower
than *tp_call*. For example, if the callee needs to convert
the arguments to an args tuple and kwargs dict anyway, then there is no point
Expand Down
34 changes: 20 additions & 14 deletions Doc/c-api/typeobj.rst
Original file line number Diff line number Diff line change
Expand Up @@ -720,29 +720,29 @@ and :c:type:`PyType_Type` effectively act as defaults.)
with the *vectorcallfunc* function.
This can be done by setting *tp_call* to :c:func:`PyVectorcall_Call`.

.. warning::

It is not recommended for :ref:`mutable heap types <heap-types>` to implement
the vectorcall protocol.
When a user sets :attr:`__call__` in Python code, only *tp_call* is updated,
likely making it inconsistent with the vectorcall function.

.. versionchanged:: 3.8

Before version 3.8, this slot was named ``tp_print``.
In Python 2.x, it was used for printing to a file.
In Python 3.0 to 3.7, it was unused.

.. versionchanged:: 3.12

Before version 3.12, it was not recommended for
:ref:`mutable heap types <heap-types>` to implement the vectorcall
protocol.
When a user sets :attr:`~type.__call__` in Python code, only *tp_call* is
updated, likely making it inconsistent with the vectorcall function.
Since 3.12, setting ``__call__`` will disable vectorcall optimization
by clearing the :const:`Py_TPFLAGS_HAVE_VECTORCALL` flag.

**Inheritance:**

This field is always inherited.
However, the :const:`Py_TPFLAGS_HAVE_VECTORCALL` flag is not
always inherited. If it's not, then the subclass won't use
always inherited. If it's not set, then the subclass won't use
:ref:`vectorcall <vectorcall>`, except when
:c:func:`PyVectorcall_Call` is explicitly called.
This is in particular the case for types without the
:const:`Py_TPFLAGS_IMMUTABLETYPE` flag set (including subclasses defined in
Python).


.. c:member:: getattrfunc PyTypeObject.tp_getattr
Expand Down Expand Up @@ -1178,12 +1178,18 @@ and :c:type:`PyType_Type` effectively act as defaults.)

**Inheritance:**

This bit is inherited for types with the
:const:`Py_TPFLAGS_IMMUTABLETYPE` flag set, if
:c:member:`~PyTypeObject.tp_call` is also inherited.
This bit is inherited if :c:member:`~PyTypeObject.tp_call` is also
inherited.

.. versionadded:: 3.9

.. versionchanged:: 3.12

This flag is now removed from a class when the class's
:py:meth:`~object.__call__` method is reassigned.

This flag can now be inherited by mutable classes.

.. data:: Py_TPFLAGS_IMMUTABLETYPE

This bit is set for type objects that are immutable: type attributes cannot be set nor deleted.
Expand Down
9 changes: 9 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,15 @@ New Features
an additional metaclass argument.
(Contributed by Wenzel Jakob in :gh:`93012`.)

* (XXX: this should be combined with :gh:`93274` when that is done)
The :const:`Py_TPFLAGS_HAVE_VECTORCALL` flag is now removed from a class
when the class's :py:meth:`~object.__call__` method is reassigned.
This makes vectorcall safe to use with mutable types (i.e. heap types
without the :const:`immutable <Py_TPFLAGS_IMMUTABLETYPE>` flag).
Mutable types that do not override :c:member:`~PyTypeObject.tp_call` now
inherit the :const:`Py_TPFLAGS_HAVE_VECTORCALL` flag.
(Contributed by Petr Viktorin in :gh:`93012`.)

Porting to Python 3.12
----------------------

Expand Down
64 changes: 63 additions & 1 deletion Lib/test/test_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,19 @@ def test_vectorcall_flag(self):
self.assertFalse(_testcapi.MethodDescriptorNopGet.__flags__ & Py_TPFLAGS_HAVE_VECTORCALL)
self.assertTrue(_testcapi.MethodDescriptor2.__flags__ & Py_TPFLAGS_HAVE_VECTORCALL)

# Mutable heap types should not inherit Py_TPFLAGS_HAVE_VECTORCALL
# Mutable heap types should inherit Py_TPFLAGS_HAVE_VECTORCALL,
# but should lose it when __call__ is overridden
class MethodDescriptorHeap(_testcapi.MethodDescriptorBase):
pass
self.assertTrue(MethodDescriptorHeap.__flags__ & Py_TPFLAGS_HAVE_VECTORCALL)
MethodDescriptorHeap.__call__ = print
self.assertFalse(MethodDescriptorHeap.__flags__ & Py_TPFLAGS_HAVE_VECTORCALL)

# Mutable heap types should not inherit Py_TPFLAGS_HAVE_VECTORCALL if
# they define __call__ directly
class MethodDescriptorHeap(_testcapi.MethodDescriptorBase):
def __call__(self):
pass
self.assertFalse(MethodDescriptorHeap.__flags__ & Py_TPFLAGS_HAVE_VECTORCALL)

def test_vectorcall_override(self):
Expand All @@ -626,6 +636,58 @@ def test_vectorcall_override(self):
f = _testcapi.MethodDescriptorNopGet()
self.assertIs(f(*args), args)

def test_vectorcall_override_on_mutable_class(self):
"""Setting __call__ should disable vectorcall"""
TestType = _testcapi.make_vectorcall_class()
instance = TestType()
self.assertEqual(instance(), "tp_call")
instance.set_vectorcall(TestType)
self.assertEqual(instance(), "vectorcall") # assume vectorcall is used
TestType.__call__ = lambda self: "custom"
self.assertEqual(instance(), "custom")

def test_vectorcall_override_with_subclass(self):
"""Setting __call__ on a superclass should disable vectorcall"""
SuperType = _testcapi.make_vectorcall_class()
class DerivedType(SuperType):
pass

instance = DerivedType()

# Derived types with its own vectorcall should be unaffected
UnaffectedType1 = _testcapi.make_vectorcall_class(DerivedType)
UnaffectedType2 = _testcapi.make_vectorcall_class(SuperType)

# Aside: Quickly check that the C helper actually made derived types
self.assertTrue(issubclass(UnaffectedType1, DerivedType))
self.assertTrue(issubclass(UnaffectedType2, SuperType))

# Initial state: tp_call
self.assertEqual(instance(), "tp_call")
self.assertEqual(_testcapi.has_vectorcall_flag(SuperType), True)
self.assertEqual(_testcapi.has_vectorcall_flag(DerivedType), True)
self.assertEqual(_testcapi.has_vectorcall_flag(UnaffectedType1), True)
self.assertEqual(_testcapi.has_vectorcall_flag(UnaffectedType2), True)

# Setting the vectorcall function
instance.set_vectorcall(SuperType)

self.assertEqual(instance(), "vectorcall")
self.assertEqual(_testcapi.has_vectorcall_flag(SuperType), True)
self.assertEqual(_testcapi.has_vectorcall_flag(DerivedType), True)
self.assertEqual(_testcapi.has_vectorcall_flag(UnaffectedType1), True)
self.assertEqual(_testcapi.has_vectorcall_flag(UnaffectedType2), True)

# Setting __call__ should remove vectorcall from all subclasses
SuperType.__call__ = lambda self: "custom"

self.assertEqual(instance(), "custom")
self.assertEqual(_testcapi.has_vectorcall_flag(SuperType), False)
self.assertEqual(_testcapi.has_vectorcall_flag(DerivedType), False)
self.assertEqual(_testcapi.has_vectorcall_flag(UnaffectedType1), True)
self.assertEqual(_testcapi.has_vectorcall_flag(UnaffectedType2), True)


def test_vectorcall(self):
# Test a bunch of different ways to call objects:
# 1. vectorcall using PyVectorcall_Call()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
The :const:`Py_TPFLAGS_HAVE_VECTORCALL` flag is now removed from a class
when the class's :py:meth:`~object.__call__` method is reassigned. This
makes vectorcall safe to use with mutable types (i.e. heap types without the
:const:`immutable <Py_TPFLAGS_IMMUTABLETYPE>` flag). Mutable types that do
not override :c:member:`~PyTypeObject.tp_call` now inherit the
:const:`Py_TPFLAGS_HAVE_VECTORCALL` flag.
107 changes: 107 additions & 0 deletions Modules/_testcapi/clinic/vectorcall.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading