bpo-29548: Fix some inefficient call API usage (GH-97) · python/cpython@72dccde (original) (raw)

7 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -3070,7 +3070,7 @@ slot_tp_del(PyObject *self)
3070 3070 /* Execute __del__ method, if any. */
3071 3071 del = _PyObject_LookupSpecial(self, &PyId___tp_del__);
3072 3072 if (del != NULL) {
3073 -res = PyEval_CallObject(del, NULL);
3073 +res = _PyObject_CallNoArg(del);
3074 3074 if (res == NULL)
3075 3075 PyErr_WriteUnraisable(del);
3076 3076 else
Original file line number Diff line number Diff line change
@@ -994,8 +994,7 @@ t_bootstrap(void *boot_raw)
994 994 _PyThreadState_Init(tstate);
995 995 PyEval_AcquireThread(tstate);
996 996 nb_threads++;
997 -res = PyEval_CallObjectWithKeywords(
998 -boot->func, boot->args, boot->keyw);
997 +res = PyObject_Call(boot->func, boot->args, boot->keyw);
999 998 if (res == NULL) {
1000 999 if (PyErr_ExceptionMatches(PyExc_SystemExit))
1001 1000 PyErr_Clear();
Original file line number Diff line number Diff line change
@@ -2417,7 +2417,7 @@ PythonCmd(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[
2417 2417 }
2418 2418 PyTuple_SET_ITEM(arg, i, s);
2419 2419 }
2420 -res = PyEval_CallObject(func, arg);
2420 +res = PyObject_Call(func, arg, NULL);
2421 2421 Py_DECREF(arg);
2422 2422
2423 2423 if (res == NULL)
@@ -2661,16 +2661,13 @@ static void
2661 2661 FileHandler(ClientData clientData, int mask)
2662 2662 {
2663 2663 FileHandler_ClientData *data = (FileHandler_ClientData *)clientData;
2664 -PyObject *func, *file, *arg, *res;
2664 +PyObject *func, *file, *res;
2665 2665
2666 2666 ENTER_PYTHON
2667 2667 func = data->func;
2668 2668 file = data->file;
2669 2669
2670 -arg = Py_BuildValue("(Oi)", file, (long) mask);
2671 -res = PyEval_CallObject(func, arg);
2672 -Py_DECREF(arg);
2673 -
2670 +res = PyObject_CallFunction(func, "Oi", file, mask);
2674 2671 if (res == NULL) {
2675 2672 errorInCmd = 1;
2676 2673 PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd);
@@ -2840,7 +2837,7 @@ TimerHandler(ClientData clientData)
2840 2837
2841 2838 ENTER_PYTHON
2842 2839
2843 -res = PyEval_CallObject(func, NULL);
2840 +res = _PyObject_CallNoArg(func);
2844 2841 Py_DECREF(func);
2845 2842 Py_DECREF(v); /* See Tktt_New() */
2846 2843
Original file line number Diff line number Diff line change
@@ -1329,7 +1329,7 @@ PyNumber_Long(PyObject *o)
1329 1329 }
1330 1330 trunc_func = _PyObject_LookupSpecial(o, &PyId___trunc__);
1331 1331 if (trunc_func) {
1332 -result = PyEval_CallObject(trunc_func, NULL);
1332 +result = _PyObject_CallNoArg(trunc_func);
1333 1333 Py_DECREF(trunc_func);
1334 1334 if (result == NULL |
1335 1335 return result;
Original file line number Diff line number Diff line change
@@ -49,39 +49,26 @@ PyFile_FromFd(int fd, const char *name, const char *mode, int buffering, const c
49 49 PyObject *
50 50 PyFile_GetLine(PyObject *f, int n)
51 51 {
52 +_Py_IDENTIFIER(readline);
52 53 PyObject *result;
53 54
54 55 if (f == NULL) {
55 56 PyErr_BadInternalCall();
56 57 return NULL;
57 58 }
58 59
59 - {
60 -PyObject *reader;
61 -PyObject *args;
62 -_Py_IDENTIFIER(readline);
63 -
64 -reader = _PyObject_GetAttrId(f, &PyId_readline);
65 -if (reader == NULL)
66 -return NULL;
67 -if (n <= 0)
68 -args = PyTuple_New(0);
69 -else
70 -args = Py_BuildValue("(i)", n);
71 -if (args == NULL) {
72 -Py_DECREF(reader);
73 -return NULL;
74 - }
75 -result = PyEval_CallObject(reader, args);
76 -Py_DECREF(reader);
77 -Py_DECREF(args);
78 -if (result != NULL && !PyBytes_Check(result) &&
79 - !PyUnicode_Check(result)) {
80 -Py_DECREF(result);
81 -result = NULL;
82 -PyErr_SetString(PyExc_TypeError,
83 -"object.readline() returned non-string");
84 - }
60 +if (n <= 0) {
61 +result = _PyObject_CallMethodIdObjArgs(f, &PyId_readline, NULL);
62 + }
63 +else {
64 +result = _PyObject_CallMethodId(f, &PyId_readline, "i", n);
65 + }
66 +if (result != NULL && !PyBytes_Check(result) &&
67 + !PyUnicode_Check(result)) {
68 +Py_DECREF(result);
69 +result = NULL;
70 +PyErr_SetString(PyExc_TypeError,
71 +"object.readline() returned non-string");
85 72 }
86 73
87 74 if (n < 0 && result != NULL && PyBytes_Check(result)) {
@@ -197,7 +184,7 @@ PyObject_AsFileDescriptor(PyObject *o)
197 184 }
198 185 else if ((meth = _PyObject_GetAttrId(o, &PyId_fileno)) != NULL)
199 186 {
200 -PyObject *fno = PyEval_CallObject(meth, NULL);
187 +PyObject *fno = _PyObject_CallNoArg(meth);
201 188 Py_DECREF(meth);
202 189 if (fno == NULL)
203 190 return -1;
Original file line number Diff line number Diff line change
@@ -4348,7 +4348,7 @@ _common_reduce(PyObject *self, int proto)
4348 4348 if (!copyreg)
4349 4349 return NULL;
4350 4350
4351 -res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto);
4351 +res = PyObject_CallMethod(copyreg, "_reduce_ex", "Oi", self, proto);
4352 4352 Py_DECREF(copyreg);
4353 4353
4354 4354 return res;
Original file line number Diff line number Diff line change
@@ -461,7 +461,7 @@ proxy_checkref(PyWeakReference *proxy)
461 461
462 462 WRAP_BINARY(proxy_getattr, PyObject_GetAttr)
463 463 WRAP_UNARY(proxy_str, PyObject_Str)
464 -WRAP_TERNARY(proxy_call, PyEval_CallObjectWithKeywords)
464 +WRAP_TERNARY(proxy_call, PyObject_Call)
465 465
466 466 static PyObject *
467 467 proxy_repr(PyWeakReference *proxy)