Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-91049: Introduce set vectorcall field API for PyFunctionObject#92257
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from21 commits
9cf96ec282e3dcedcdcf1debf5a9473d18d76e8c9d33374591543f4408082bded9332724ffd3089cb4b23387d4ab0cc28a99e408524353c860f7769376ee7556ffc705340e87a5e9d1312faf5266234709149f14e732d7e84874fbFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -760,6 +760,16 @@ def __call__(self, *args): | ||
| self.assertEqual(expected, meth(*args1, **kwargs)) | ||
| self.assertEqual(expected, wrapped(*args, **kwargs)) | ||
| def test_setvectorcall(self): | ||
| from _testcapi import function_setvectorcall | ||
| def f(num): return num + 1 | ||
| num = 10 | ||
| self.assertEqual(11, f(num)) | ||
| function_setvectorcall(f) | ||
| # make sure specializer is triggered by running > 50 times | ||
| for _ in range(51): | ||
Fidget-Spinner marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| self.assertEqual("overridden", f(num)) | ||
| @requires_limited_api | ||
| def test_vectorcall_limited(self): | ||
| from _testcapi import pyobject_vectorcall | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Add new function:c:func:`PyFunction_SetVectorcall` to the C API | ||
| which sets the vectorcall field of a given:c:type:`PyFunctionObject`. | ||
| Warning: extensions using this API must preserve the behavior | ||
| of the unaltered function! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -102,6 +102,23 @@ test_pyobject_vectorcall(PyObject *self, PyObject *args) | ||
| return PyObject_Vectorcall(func, stack, nargs, kwnames); | ||
| } | ||
| static PyObject * | ||
| override_vectorcall( | ||
| PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames) { | ||
erlend-aasland marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| return PyUnicode_FromString("overridden"); | ||
| } | ||
| static PyObject * | ||
| function_setvectorcall(PyObject *self, PyObject *func) | ||
| { | ||
| if (!PyFunction_Check(func)) { | ||
| PyErr_BadInternalCall(); | ||
erlend-aasland marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| return NULL; | ||
| } | ||
| PyFunction_SetVectorcall((PyFunctionObject *)func, (vectorcallfunc)override_vectorcall); | ||
| Py_RETURN_NONE; | ||
| } | ||
| static PyObject * | ||
| test_pyvectorcall_call(PyObject *self, PyObject *args) | ||
| { | ||
| @@ -244,6 +261,7 @@ static PyMethodDef TestMethods[] = { | ||
| {"pyobject_fastcall", test_pyobject_fastcall, METH_VARARGS}, | ||
| {"pyobject_fastcalldict", test_pyobject_fastcalldict, METH_VARARGS}, | ||
| {"pyobject_vectorcall", test_pyobject_vectorcall, METH_VARARGS}, | ||
| {"function_setvectorcall", function_setvectorcall, METH_O}, | ||
| {"pyvectorcall_call", test_pyvectorcall_call, METH_VARARGS}, | ||
| _TESTCAPI_MAKE_VECTORCALL_CLASS_METHODDEF | ||
| _TESTCAPI_HAS_VECTORCALL_FLAG_METHODDEF | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -134,6 +134,9 @@ uint32_t _PyFunction_GetVersionForCurrentState(PyFunctionObject *func) | ||
| if (func->func_version != 0) { | ||
| return func->func_version; | ||
| } | ||
| if (func->vectorcall != _PyFunction_Vectorcall) { | ||
| return 0; | ||
markshannon marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| } | ||
| if (next_func_version == 0) { | ||
| return 0; | ||
| } | ||
| @@ -209,6 +212,13 @@ PyFunction_SetDefaults(PyObject *op, PyObject *defaults) | ||
| return 0; | ||
| } | ||
| void | ||
| PyFunction_SetVectorcall(PyFunctionObject *func, vectorcallfunc vectorcall) | ||
| { | ||
| func->func_version = 0; | ||
erlend-aasland marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| func->vectorcall = vectorcall; | ||
| } | ||
| PyObject * | ||
| PyFunction_GetKwDefaults(PyObject *op) | ||
| { | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3135,8 +3135,11 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int | ||
| PyObject *getattribute = read_obj(cache->descr); | ||
| assert(Py_IS_TYPE(getattribute, &PyFunction_Type)); | ||
| PyFunctionObject *f = (PyFunctionObject *)getattribute; | ||
| uint32_t func_version = read_u32(cache->keys_version); | ||
| assert(func_version != 0); | ||
| DEOPT_IF(f->func_version != func_version, LOAD_ATTR); | ||
erlend-aasland marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| PyCodeObject *code = (PyCodeObject *)f->func_code; | ||
| assert(code->co_argcount== 2); | ||
| DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize), CALL); | ||
| STAT_INC(LOAD_ATTR, hit); | ||
| @@ -4138,7 +4141,9 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int | ||
| function = PEEK(total_args + 1); | ||
| int positional_args = total_args - KWNAMES_LEN(); | ||
| // Check if the call can be inlined or not | ||
| if (Py_TYPE(function) == &PyFunction_Type && | ||
| tstate->interp->eval_frame == NULL && | ||
| ((PyFunctionObject *)function)->vectorcall == _PyFunction_Vectorcall) { | ||
markshannon marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page.
markshannon marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(function))->co_flags; | ||
| PyObject *locals = code_flags & CO_OPTIMIZED ? NULL : Py_NewRef(PyFunction_GET_GLOBALS(function)); | ||
| STACK_SHRINK(total_args); | ||