-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-100554: Add Py_tp_vectorcall
slot to set PyTypeObject.tp_vectorcall
using the PyType_FromSpec
function family.
#123332
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
Changes from 1 commit
85c60d9
e9c07c0
3ef669d
d4b67de
ef81dc0
58f6759
dffd2a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Added a slot ``Py_tp_vectorcall`` to set | ||
:c:member:`~PyTypeObject.tp_vectorcall` via the :c:func:`PyType_FromSpec` | ||
function family. Limited API extensions can use this feature to provide more | ||
efficient vector call-based implementation of ``__new__`` and ``__init__``. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2510,3 +2510,5 @@ | |
added = '3.14' | ||
[function.PyIter_NextItem] | ||
added = '3.14' | ||
[const.Py_tp_vectorcall] | ||
added = '3.14' |
encukou marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1008,6 +1008,24 @@ static PyType_Spec HeapCTypeSetattr_spec = { | |
HeapCTypeSetattr_slots | ||
}; | ||
|
||
static PyObject *heapctype_vectorcall(PyObject *self, PyObject *const *args_in, | ||
size_t nargsf, PyObject *kwargs_in) { | ||
return PyLong_FromLong(123); | ||
} | ||
|
||
static PyType_Slot HeapCTypeVectorcall_slots[] = { | ||
{Py_tp_vectorcall, heapctype_vectorcall}, | ||
{0, 0}, | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That looks like a good example of how not to do it, since it leaves >>> import _testcapi
>>> _testcapi.HeapCTypeVectorcall.__call__(_testcapi.HeapCTypeVectorcall, 'Sub', (), {})
<class '__m
10000
ain__.Sub'> As per the docs, a class supporting vectorcall must also implement So, you do need to override There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general, the point of this PR is to be able to do exactly the same as what But that is less straightforward to test from the outside, hence the the contrived example of returning (To keep the test reasonably compact, I would prefer not to deal with metaclasses on top) I will override tp_new and make the type non-inheritable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm worried about leading users to a trap, and I'd prefer to see a full solution for the use case, to get an idea about where in the docs to put warnings, and what kinds of issues this opens. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm happy to implement a more representative version but wonder about the following: how should the vectorcall version communicate to the test suite that it was used? If the equivalence requirement you stated is interpreted strictly, it should not be possible to detect any difference. For example, would it be OK I return two different instances from each mode of construction and then add a comment saying that these are only different for test-related purposes, and that the contract of these operations is that they should be interchangeable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's fine for tests. |
||
|
||
static PyType_Spec HeapCTypeVectorcall_spec = { | ||
"_testcapi.HeapCTypeVectorcall", | ||
0, | ||
0, | ||
Py_TPFLAGS_DEFAULT, | ||
HeapCTypeVectorcall_slots | ||
}; | ||
|
||
PyDoc_STRVAR(HeapCCollection_doc, | ||
"Tuple-like heap type that uses PyObject_GetItemData for items."); | ||
|
||
|
@@ -1180,6 +1198,10 @@ _PyTestCapi_Init_Heaptype(PyObject *m) { | |
PyObject *HeapCTypeSetattr = PyType_FromSpec(&HeapCTypeSetattr_spec); | ||
ADD("HeapCTypeSetattr", HeapCTypeSetattr); | ||
|
||
PyObject *HeapCTypeVectorcall = PyType_FromSpecWithBases( | ||
&HeapCTypeVectorcall_spec, (PyObject *) &PyType_Type); | ||
ADD("HeapCTypeVectorcall", HeapCTypeVectorcall); | ||
|
||
PyObject *subclass_with_finalizer_bases = PyTuple_Pack(1, HeapCTypeSubclass); | ||
if (subclass_with_finalizer_bases == NULL) { | ||
return -1; | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use
.. versionchanged
, like the 3.9 & 3.11PyBufferProcs
entries a few lines down.