Skip to content

Commit b6a0230

Browse files
committed
gh-106023: Update code using _PyObject_FastCall()
Replace _PyObject_FastCall() calls with PyObject_Vectorcall().
1 parent cea9d4e commit b6a0230

File tree

8 files changed

+34
-53
lines changed

8 files changed

+34
-53
lines changed

Modules/_elementtree.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,6 @@ LOCAL(PyObject *)
882882
deepcopy(elementtreestate *st, PyObject *object, PyObject *memo)
883883
{
884884
/* do a deep copy of the given object */
885-
PyObject *stack[2];
886885

887886
/* Fast paths */
888887
if (object == Py_None || PyUnicode_CheckExact(object)) {
@@ -917,9 +916,8 @@ deepcopy(elementtreestate *st, PyObject *object, PyObject *memo)
917916
return NULL;
918917
}
919918

920-
stack[0] = object;
921-
stack[1] = memo;
922-
return _PyObject_FastCall(st->deepcopy_obj, stack, 2);
919+
PyObject *args[2] = {object, memo};
920+
return PyObject_Vectorcall(st->deepcopy_obj, args, 2, NULL);
923921
}
924922

925923

@@ -2852,14 +2850,14 @@ treebuilder_handle_pi(TreeBuilderObject* self, PyObject* target, PyObject* text)
28522850
{
28532851
PyObject* pi;
28542852
PyObject* this;
2855-
PyObject* stack[2] = {target, text};
28562853

28572854
if (treebuilder_flush_data(self) < 0) {
28582855
return NULL;
28592856
}
28602857

28612858
if (self->pi_factory) {
2862-
pi = _PyObject_FastCall(self->pi_factory, stack, 2);
2859+
PyObject* args[2] = {target, text};
2860+
pi = PyObject_Vectorcall(self->pi_factory, args, 2, NULL);
28632861
if (!pi) {
28642862
return NULL;
28652863
}
@@ -3372,7 +3370,6 @@ expat_start_ns_handler(XMLParserObject* self, const XML_Char* prefix_in,
33723370
PyObject* res = NULL;
33733371
PyObject* uri;
33743372
PyObject* prefix;
3375-
PyObject* stack[2];
33763373

33773374
if (PyErr_Occurred())
33783375
return;
@@ -3411,9 +3408,8 @@ expat_start_ns_handler(XMLParserObject* self, const XML_Char* prefix_in,
34113408
return;
34123409
}
34133410

3414-
stack[0] = prefix;
3415-
stack[1] = uri;
3416-
res = _PyObject_FastCall(self->handle_start_ns, stack, 2);
3411+
PyObject* args[2] = {prefix, uri};
3412+
res = PyObject_Vectorcall(self->handle_start_ns, args, 2, NULL);
34173413
Py_DECREF(uri);
34183414
Py_DECREF(prefix);
34193415
}
@@ -3551,7 +3547,6 @@ expat_pi_handler(XMLParserObject* self, const XML_Char* target_in,
35513547
PyObject* pi_target;
35523548
PyObject* data;
35533549
PyObject* res;
3554-
PyObject* stack[2];
35553550

35563551
if (PyErr_Occurred())
35573552
return;
@@ -3581,9 +3576,8 @@ expat_pi_handler(XMLParserObject* self, const XML_Char* target_in,
35813576
if (!data)
35823577
goto error;
35833578

3584-
stack[0] = pi_target;
3585-
stack[1] = data;
3586-
res = _PyObject_FastCall(self->handle_pi, stack, 2);
3579+
PyObject* args[2] = {pi_target, data};
3580+
res = PyObject_Vectorcall(self->handle_pi, args, 2, NULL);
35873581
Py_XDECREF(res);
35883582
Py_DECREF(data);
35893583
Py_DECREF(pi_target);

Modules/_functoolsmodule.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -594,21 +594,15 @@ keyobject_call(keyobject *ko, PyObject *args, PyObject *kwds)
594594
static PyObject *
595595
keyobject_richcompare(PyObject *ko, PyObject *other, int op)
596596
{
597-
PyObject *res;
598-
PyObject *x;
599-
PyObject *y;
600-
PyObject *compare;
601-
PyObject *answer;
602-
PyObject* stack[2];
603-
604597
if (!Py_IS_TYPE(other, Py_TYPE(ko))) {
605598
PyErr_Format(PyExc_TypeError, "other argument must be K instance");
606599
return NULL;
607600
}
608-
compare = ((keyobject *) ko)->cmp;
601+
602+
PyObject *compare = ((keyobject *) ko)->cmp;
609603
assert(compare != NULL);
610-
x = ((keyobject *) ko)->object;
611-
y = ((keyobject *) other)->object;
604+
PyObject *x = ((keyobject *) ko)->object;
605+
PyObject *y = ((keyobject *) other)->object;
612606
if (!x || !y){
613607
PyErr_Format(PyExc_AttributeError, "object");
614608
return NULL;
@@ -617,14 +611,13 @@ keyobject_richcompare(PyObject *ko, PyObject *other, int op)
617611
/* Call the user's comparison function and translate the 3-way
618612
* result into true or false (or error).
619613
*/
620-
stack[0] = x;
621-
stack[1] = y;
622-
res = _PyObject_FastCall(compare, stack, 2);
614+
PyObject* args[2] = {x, y};
615+
PyObject *res = PyObject_Vectorcall(compare, args, 2, NULL);
623616
if (res == NULL) {
624617
return NULL;
625618
}
626619

627-
answer = PyObject_RichCompare(res, _PyLong_GetZero(), op);
620+
PyObject *answer = PyObject_RichCompare(res, _PyLong_GetZero(), op);
628621
Py_DECREF(res);
629622
return answer;
630623
}

Objects/typeobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10013,7 +10013,8 @@ static int
1001310013
type_new_init_subclass(PyTypeObject *type, PyObject *kwds)
1001410014
{
1001510015
PyObject *args[2] = {(PyObject *)type, (PyObject *)type};
10016-
PyObject *super = _PyObject_FastCall((PyObject *)&PySuper_Type, args, 2);
10016+
PyObject *super = PyObject_Vectorcall((PyObject *)&PySuper_Type,
10017+
args, 2, NULL);
1001710018
if (super == NULL) {
1001810019
return -1;
1001910020
}

Parser/pegen.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,6 @@ _PyPegen_new_identifier(Parser *p, const char *n)
465465
identifier; if so, normalize to NFKC. */
466466
if (!PyUnicode_IS_ASCII(id))
467467
{
468-
PyObject *id2;
469468
if (!init_normalization(p))
470469
{
471470
Py_DECREF(id);
@@ -478,12 +477,13 @@ _PyPegen_new_identifier(Parser *p, const char *n)
478477
goto error;
479478
}
480479
PyObject *args[2] = {form, id};
481-
id2 = _PyObject_FastCall(p->normalize, args, 2);
480+
PyObject *id2 = PyObject_Vectorcall(p->normalize, args, 2, NULL);
482481
Py_DECREF(id);
483482
Py_DECREF(form);
484483
if (!id2) {
485484
goto error;
486485
}
486+
487487
if (!PyUnicode_Check(id2))
488488
{
489489
PyErr_Format(PyExc_TypeError,

Python/bytecodes.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include "Python.h"
1010
#include "pycore_abstract.h" // _PyIndex_Check()
11-
#include "pycore_call.h" // _PyObject_FastCallDictTstate()
1211
#include "pycore_ceval.h" // _PyEval_SignalAsyncExc()
1312
#include "pycore_code.h"
1413
#include "pycore_function.h"

Python/ceval.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include "Python.h"
66
#include "pycore_abstract.h" // _PyIndex_Check()
7-
#include "pycore_call.h" // _PyObject_FastCallDictTstate()
7+
#include "pycore_call.h" // _PyObject_CallNoArgs()
88
#include "pycore_ceval.h" // _PyEval_SignalAsyncExc()
99
#include "pycore_code.h"
1010
#include "pycore_function.h"
@@ -2431,40 +2431,37 @@ static PyObject *
24312431
import_name(PyThreadState *tstate, _PyInterpreterFrame *frame,
24322432
PyObject *name, PyObject *fromlist, PyObject *level)
24332433
{
2434-
PyObject *import_func, *res;
2435-
PyObject* stack[5];
2436-
2437-
import_func = _PyDict_GetItemWithError(frame->f_builtins, &_Py_ID(__import__));
2434+
PyObject *import_func = _PyDict_GetItemWithError(frame->f_builtins,
2435+
&_Py_ID(__import__));
24382436
if (import_func == NULL) {
24392437
if (!_PyErr_Occurred(tstate)) {
24402438
_PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
24412439
}
24422440
return NULL;
24432441
}
2442+
24442443
PyObject *locals = frame->f_locals;
2444+
if (locals == NULL) {
2445+
locals = Py_None;
2446+
}
2447+
24452448
/* Fast path for not overloaded __import__. */
24462449
if (_PyImport_IsDefaultImportFunc(tstate->interp, import_func)) {
24472450
int ilevel = _PyLong_AsInt(level);
24482451
if (ilevel == -1 && _PyErr_Occurred(tstate)) {
24492452
return NULL;
24502453
}
2451-
res = PyImport_ImportModuleLevelObject(
2454+
return PyImport_ImportModuleLevelObject(
24522455
name,
24532456
frame->f_globals,
2454-
locals == NULL ? Py_None :locals,
2457+
locals,
24552458
fromlist,
24562459
ilevel);
2457-
return res;
24582460
}
24592461

2462+
PyObject* args[5] = {name, frame->f_globals, locals, fromlist, level};
24602463
Py_INCREF(import_func);
2461-
2462-
stack[0] = name;
2463-
stack[1] = frame->f_globals;
2464-
stack[2] = locals == NULL ? Py_None : locals;
2465-
stack[3] = fromlist;
2466-
stack[4] = level;
2467-
res = _PyObject_FastCall(import_func, stack, 5);
2464+
PyObject *res = PyObject_Vectorcall(import_func, args, 5, NULL);
24682465
Py_DECREF(import_func);
24692466
return res;
24702467
}

Python/pythonrun.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -834,11 +834,8 @@ _PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
834834
_PyErr_WriteUnraisableMsg("in audit hook", NULL);
835835
}
836836
if (hook) {
837-
PyObject* stack[3];
838-
stack[0] = typ;
839-
stack[1] = exc;
840-
stack[2] = tb;
841-
PyObject *result = _PyObject_FastCall(hook, stack, 3);
837+
PyObject* args[3] = {typ, exc, tb};
838+
PyObject *result = PyObject_Vectorcall(hook, args, 3, NULL);
842839
if (result == NULL) {
843840
handle_system_exit();
844841

Python/sysmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ sys_audit_tstate(PyThreadState *ts, const char *event,
268268
PyThreadState_LeaveTracing(ts);
269269
}
270270
PyObject* args[2] = {eventName, eventArgs};
271-
o = _PyObject_FastCallTstate(ts, hook, args, 2);
271+
o = _PyObject_VectorcallTstate(ts, hook, args, 2, NULL);
272272
if (canTrace) {
273273
PyThreadState_EnterTracing(ts);
274274
}

0 commit comments

Comments
 (0)