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 from24 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,32 @@ 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)) | ||
| def test_setvectorcall_load_attr_specialization(self): | ||
| from _testcapi import function_setvectorcall | ||
| class X: | ||
| def __getattribute__(self, attr): | ||
| return attr | ||
| x = X() | ||
| # trigger LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN specialization | ||
| for _ in range(8): | ||
| self.assertEqual("a", x.a) | ||
Fidget-Spinner marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| function_setvectorcall(X.__getattribute__) | ||
| # make sure specialization doesn't override the override | ||
| for _ in range(8): | ||
Fidget-Spinner marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| self.assertEqual("overridden", x.a) | ||
| @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 |
|---|---|---|
| @@ -134,6 +134,9 @@ uint32_t _PyFunction_GetVersionForCurrentState(PyFunctionObject *func) | ||
| if (func->func_version!=0) { | ||
| returnfunc->func_version; | ||
| } | ||
| if (func->vectorcall!=_PyFunction_Vectorcall) { | ||
| return0; | ||
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) { | ||
| return0; | ||
| } | ||
| @@ -209,6 +212,14 @@ PyFunction_SetDefaults(PyObject *op, PyObject *defaults) | ||
| return0; | ||
| } | ||
| void | ||
| PyFunction_SetVectorcall(PyFunctionObject*func,vectorcallfuncvectorcall) | ||
| { | ||
| assert(func!=NULL); | ||
| 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,10 @@ _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) | ||
| { | ||
| 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); | ||