Skip to content

Commit

Permalink
gh-106023: Update code using _PyObject_FastCall() (#106257)
Browse files Browse the repository at this point in the history
Replace _PyObject_FastCall() calls with PyObject_Vectorcall().
  • Loading branch information
vstinner committed Jun 30, 2023
1 parent e7bc8d1 commit 8c5f74f
Show file tree
Hide file tree
Showing 10 changed files with 434 additions and 453 deletions.
22 changes: 8 additions & 14 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,6 @@ LOCAL(PyObject *)
deepcopy(elementtreestate *st, PyObject *object, PyObject *memo)
{
/* do a deep copy of the given object */
PyObject *stack[2];

/* Fast paths */
if (object == Py_None || PyUnicode_CheckExact(object)) {
Expand Down Expand Up @@ -917,9 +916,8 @@ deepcopy(elementtreestate *st, PyObject *object, PyObject *memo)
return NULL;
}

stack[0] = object;
stack[1] = memo;
return _PyObject_FastCall(st->deepcopy_obj, stack, 2);
PyObject *args[2] = {object, memo};
return PyObject_Vectorcall(st->deepcopy_obj, args, 2, NULL);
}


Expand Down Expand Up @@ -2852,14 +2850,14 @@ treebuilder_handle_pi(TreeBuilderObject* self, PyObject* target, PyObject* text)
{
PyObject* pi;
PyObject* this;
PyObject* stack[2] = {target, text};

if (treebuilder_flush_data(self) < 0) {
return NULL;
}

if (self->pi_factory) {
pi = _PyObject_FastCall(self->pi_factory, stack, 2);
PyObject* args[2] = {target, text};
pi = PyObject_Vectorcall(self->pi_factory, args, 2, NULL);
if (!pi) {
return NULL;
}
Expand Down Expand Up @@ -3372,7 +3370,6 @@ expat_start_ns_handler(XMLParserObject* self, const XML_Char* prefix_in,
PyObject* res = NULL;
PyObject* uri;
PyObject* prefix;
PyObject* stack[2];

if (PyErr_Occurred())
return;
Expand Down Expand Up @@ -3411,9 +3408,8 @@ expat_start_ns_handler(XMLParserObject* self, const XML_Char* prefix_in,
return;
}

stack[0] = prefix;
stack[1] = uri;
res = _PyObject_FastCall(self->handle_start_ns, stack, 2);
PyObject* args[2] = {prefix, uri};
res = PyObject_Vectorcall(self->handle_start_ns, args, 2, NULL);
Py_DECREF(uri);
Py_DECREF(prefix);
}
Expand Down Expand Up @@ -3551,7 +3547,6 @@ expat_pi_handler(XMLParserObject* self, const XML_Char* target_in,
PyObject* pi_target;
PyObject* data;
PyObject* res;
PyObject* stack[2];

if (PyErr_Occurred())
return;
Expand Down Expand Up @@ -3581,9 +3576,8 @@ expat_pi_handler(XMLParserObject* self, const XML_Char* target_in,
if (!data)
goto error;

stack[0] = pi_target;
stack[1] = data;
res = _PyObject_FastCall(self->handle_pi, stack, 2);
PyObject* args[2] = {pi_target, data};
res = PyObject_Vectorcall(self->handle_pi, args, 2, NULL);
Py_XDECREF(res);
Py_DECREF(data);
Py_DECREF(pi_target);
Expand Down
21 changes: 7 additions & 14 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,21 +594,15 @@ keyobject_call(keyobject *ko, PyObject *args, PyObject *kwds)
static PyObject *
keyobject_richcompare(PyObject *ko, PyObject *other, int op)
{
PyObject *res;
PyObject *x;
PyObject *y;
PyObject *compare;
PyObject *answer;
PyObject* stack[2];

if (!Py_IS_TYPE(other, Py_TYPE(ko))) {
PyErr_Format(PyExc_TypeError, "other argument must be K instance");
return NULL;
}
compare = ((keyobject *) ko)->cmp;

PyObject *compare = ((keyobject *) ko)->cmp;
assert(compare != NULL);
x = ((keyobject *) ko)->object;
y = ((keyobject *) other)->object;
PyObject *x = ((keyobject *) ko)->object;
PyObject *y = ((keyobject *) other)->object;
if (!x || !y){
PyErr_Format(PyExc_AttributeError, "object");
return NULL;
Expand All @@ -617,14 +611,13 @@ keyobject_richcompare(PyObject *ko, PyObject *other, int op)
/* Call the user's comparison function and translate the 3-way
* result into true or false (or error).
*/
stack[0] = x;
stack[1] = y;
res = _PyObject_FastCall(compare, stack, 2);
PyObject* args[2] = {x, y};
PyObject *res = PyObject_Vectorcall(compare, args, 2, NULL);
if (res == NULL) {
return NULL;
}

answer = PyObject_RichCompare(res, _PyLong_GetZero(), op);
PyObject *answer = PyObject_RichCompare(res, _PyLong_GetZero(), op);
Py_DECREF(res);
return answer;
}
Expand Down
3 changes: 2 additions & 1 deletion Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -10013,7 +10013,8 @@ static int
type_new_init_subclass(PyTypeObject *type, PyObject *kwds)
{
PyObject *args[2] = {(PyObject *)type, (PyObject *)type};
PyObject *super = _PyObject_FastCall((PyObject *)&PySuper_Type, args, 2);
PyObject *super = PyObject_Vectorcall((PyObject *)&PySuper_Type,
args, 2, NULL);
if (super == NULL) {
return -1;
}
Expand Down
4 changes: 2 additions & 2 deletions Parser/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,6 @@ _PyPegen_new_identifier(Parser *p, const char *n)
identifier; if so, normalize to NFKC. */
if (!PyUnicode_IS_ASCII(id))
{
PyObject *id2;
if (!init_normalization(p))
{
Py_DECREF(id);
Expand All @@ -478,12 +477,13 @@ _PyPegen_new_identifier(Parser *p, const char *n)
goto error;
}
PyObject *args[2] = {form, id};
id2 = _PyObject_FastCall(p->normalize, args, 2);
PyObject *id2 = PyObject_Vectorcall(p->normalize, args, 2, NULL);
Py_DECREF(id);
Py_DECREF(form);
if (!id2) {
goto error;
}

if (!PyUnicode_Check(id2))
{
PyErr_Format(PyExc_TypeError,
Expand Down
1 change: 0 additions & 1 deletion Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_call.h" // _PyObject_FastCallDictTstate()
#include "pycore_ceval.h" // _PyEval_SignalAsyncExc()
#include "pycore_code.h"
#include "pycore_function.h"
Expand Down
27 changes: 12 additions & 15 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_call.h" // _PyObject_FastCallDictTstate()
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_ceval.h" // _PyEval_SignalAsyncExc()
#include "pycore_code.h"
#include "pycore_function.h"
Expand Down Expand Up @@ -2431,40 +2431,37 @@ static PyObject *
import_name(PyThreadState *tstate, _PyInterpreterFrame *frame,
PyObject *name, PyObject *fromlist, PyObject *level)
{
PyObject *import_func, *res;
PyObject* stack[5];

import_func = _PyDict_GetItemWithError(frame->f_builtins, &_Py_ID(__import__));
PyObject *import_func = _PyDict_GetItemWithError(frame->f_builtins,
&_Py_ID(__import__));
if (import_func == NULL) {
if (!_PyErr_Occurred(tstate)) {
_PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
}
return NULL;
}

PyObject *locals = frame->f_locals;
if (locals == NULL) {
locals = Py_None;
}

/* Fast path for not overloaded __import__. */
if (_PyImport_IsDefaultImportFunc(tstate->interp, import_func)) {
int ilevel = _PyLong_AsInt(level);
if (ilevel == -1 && _PyErr_Occurred(tstate)) {
return NULL;
}
res = PyImport_ImportModuleLevelObject(
return PyImport_ImportModuleLevelObject(
name,
frame->f_globals,
locals == NULL ? Py_None :locals,
locals,
fromlist,
ilevel);
return res;
}

PyObject* args[5] = {name, frame->f_globals, locals, fromlist, level};
Py_INCREF(import_func);

stack[0] = name;
stack[1] = frame->f_globals;
stack[2] = locals == NULL ? Py_None : locals;
stack[3] = fromlist;
stack[4] = level;
res = _PyObject_FastCall(import_func, stack, 5);
PyObject *res = PyObject_Vectorcall(import_func, args, 5, NULL);
Py_DECREF(import_func);
return res;
}
Expand Down
Loading

0 comments on commit 8c5f74f

Please sign in to comment.