Skip to content

Commit ccfa9f2

Browse files
vstinnerlisroach
authored andcommitted
bpo-37194: Add a new public PyObject_CallNoArgs() function (pythonGH-13890)
Add a new public PyObject_CallNoArgs() function to the C API: call a callable Python object without any arguments. It is the most efficient way to call a callback without any argument. On x86-64, for example, PyObject_CallFunctionObjArgs(func, NULL) allocates 960 bytes on the stack per call, whereas PyObject_CallNoArgs(func) only allocates 624 bytes per call. It is excluded from stable ABI 3.8. Replace private _PyObject_CallNoArg() with public PyObject_CallNoArgs() in C extensions: _asyncio, _datetime, _elementtree, _pickle, _tkinter and readline.
1 parent 5206fa1 commit ccfa9f2

File tree

12 files changed

+51
-17
lines changed

12 files changed

+51
-17
lines changed

Doc/c-api/object.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,16 @@ Object Protocol
253253
and ``0`` otherwise. This function always succeeds.
254254
255255
256+
.. c:function:: PyObject* PyObject_CallNoArgs(PyObject *callable)
257+
258+
Call a callable Python object *callable* without any arguments.
259+
260+
Returns the result of the call on success, or raise an exception and return
261+
*NULL* on failure.
262+
263+
.. versionadded:: 3.9
264+
265+
256266
.. c:function:: PyObject* PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
257267
258268
Call a callable Python object *callable*, with arguments given by the

Doc/whatsnew/3.9.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ Optimizations
102102
Build and C API Changes
103103
=======================
104104

105+
* Add a new public :c:func:`PyObject_CallNoArgs` function to the C API:
106+
call a callable Python object without any arguments.
105107

106108

107109
Deprecated

Include/abstract.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ extern "C" {
141141
#endif
142142

143143

144+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
145+
/* Call a callable Python object without any arguments */
146+
PyAPI_FUNC(PyObject *) PyObject_CallNoArgs(PyObject *func);
147+
#endif
148+
149+
144150
/* Call a callable Python object 'callable' with arguments given by the
145151
tuple 'args' and keywords arguments given by the dictionary 'kwargs'.
146152

Include/cpython/abstract.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ _PyObject_FastCall(PyObject *func, PyObject *const *args, Py_ssize_t nargs)
144144
return _PyObject_Vectorcall(func, args, (size_t)nargs, NULL);
145145
}
146146

147-
/* Call a callable without any arguments */
147+
/* Call a callable without any arguments
148+
Private static inline function variant of public function
149+
PyObject_CallNoArgs(). */
148150
static inline PyObject *
149151
_PyObject_CallNoArg(PyObject *func) {
150152
return _PyObject_Vectorcall(func, NULL, 0, NULL);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Add a new public :c:func:`PyObject_CallNoArgs` function to the C API: call a
2+
callable Python object without any arguments. It is the most efficient way to
3+
call a callback without any argument. On x86-64, for example,
4+
``PyObject_CallFunctionObjArgs(func, NULL)`` allocates 960 bytes on the stack
5+
per call, whereas ``PyObject_CallNoArgs(func)`` only allocates 624 bytes per
6+
call.

Modules/_asynciomodule.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ get_future_loop(PyObject *fut)
216216
return NULL;
217217
}
218218
if (getloop != NULL) {
219-
PyObject *res = _PyObject_CallNoArg(getloop);
219+
PyObject *res = PyObject_CallNoArgs(getloop);
220220
Py_DECREF(getloop);
221221
return res;
222222
}
@@ -328,7 +328,7 @@ get_event_loop(void)
328328
return loop;
329329
}
330330

331-
policy = _PyObject_CallNoArg(asyncio_get_event_loop_policy);
331+
policy = PyObject_CallNoArgs(asyncio_get_event_loop_policy);
332332
if (policy == NULL) {
333333
return NULL;
334334
}
@@ -510,7 +510,7 @@ future_init(FutureObj *fut, PyObject *loop)
510510
method, which is called during the interpreter shutdown and the
511511
traceback module is already unloaded.
512512
*/
513-
fut->fut_source_tb = _PyObject_CallNoArg(traceback_extract_stack);
513+
fut->fut_source_tb = PyObject_CallNoArgs(traceback_extract_stack);
514514
if (fut->fut_source_tb == NULL) {
515515
return -1;
516516
}
@@ -553,7 +553,7 @@ future_set_exception(FutureObj *fut, PyObject *exc)
553553
}
554554

555555
if (PyExceptionClass_Check(exc)) {
556-
exc_val = _PyObject_CallNoArg(exc);
556+
exc_val = PyObject_CallNoArgs(exc);
557557
if (exc_val == NULL) {
558558
return NULL;
559559
}
@@ -2593,7 +2593,7 @@ task_step_impl(TaskObj *task, PyObject *exc)
25932593

25942594
if (!exc) {
25952595
/* exc was not a CancelledError */
2596-
exc = _PyObject_CallNoArg(asyncio_CancelledError);
2596+
exc = PyObject_CallNoArgs(asyncio_CancelledError);
25972597
if (!exc) {
25982598
goto fail;
25992599
}
@@ -3308,7 +3308,7 @@ module_init(void)
33083308
PyObject *weak_set;
33093309
WITH_MOD("weakref")
33103310
GET_MOD_ATTR(weak_set, "WeakSet");
3311-
all_tasks = _PyObject_CallNoArg(weak_set);
3311+
all_tasks = PyObject_CallNoArgs(weak_set);
33123312
Py_CLEAR(weak_set);
33133313
if (all_tasks == NULL) {
33143314
goto fail;

Modules/_datetimemodule.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,8 +1528,8 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
15281528
ntoappend = 1;
15291529
}
15301530
else if ((ch = *pin++) == '\0') {
1531-
/* Null byte follows %, copy only '%'.
1532-
*
1531+
/* Null byte follows %, copy only '%'.
1532+
*
15331533
* Back the pin up one char so that we catch the null check
15341534
* the next time through the loop.*/
15351535
pin--;
@@ -1619,7 +1619,7 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
16191619
usednew += ntoappend;
16201620
assert(usednew <= totalnew);
16211621
} /* end while() */
1622-
1622+
16231623
if (_PyBytes_Resize(&newfmt, usednew) < 0)
16241624
goto Done;
16251625
{
@@ -3607,7 +3607,7 @@ tzinfo_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
36073607

36083608
getinitargs = _PyObject_GetAttrId(self, &PyId___getinitargs__);
36093609
if (getinitargs != NULL) {
3610-
args = _PyObject_CallNoArg(getinitargs);
3610+
args = PyObject_CallNoArgs(getinitargs);
36113611
Py_DECREF(getinitargs);
36123612
if (args == NULL) {
36133613
return NULL;
@@ -3624,7 +3624,7 @@ tzinfo_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
36243624

36253625
getstate = _PyObject_GetAttrId(self, &PyId___getstate__);
36263626
if (getstate != NULL) {
3627-
state = _PyObject_CallNoArg(getstate);
3627+
state = PyObject_CallNoArgs(getstate);
36283628
Py_DECREF(getstate);
36293629
if (state == NULL) {
36303630
Py_DECREF(args);

Modules/_elementtree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3892,7 +3892,7 @@ _elementtree_XMLParser_close_impl(XMLParserObject *self)
38923892
}
38933893
else if (self->handle_close) {
38943894
Py_DECREF(res);
3895-
return _PyObject_CallNoArg(self->handle_close);
3895+
return PyObject_CallNoArgs(self->handle_close);
38963896
}
38973897
else {
38983898
return res;

Modules/_pickle.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,7 @@ _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
12741274
return -1;
12751275

12761276
if (n == READ_WHOLE_LINE) {
1277-
data = _PyObject_CallNoArg(self->readline);
1277+
data = PyObject_CallNoArgs(self->readline);
12781278
}
12791279
else {
12801280
PyObject *len;
@@ -4411,7 +4411,7 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
44114411
/* Check for a __reduce__ method. */
44124412
reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
44134413
if (reduce_func != NULL) {
4414-
reduce_value = _PyObject_CallNoArg(reduce_func);
4414+
reduce_value = PyObject_CallNoArgs(reduce_func);
44154415
}
44164416
else {
44174417
PyErr_Format(st->PicklingError,

Modules/_tkinter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2768,7 +2768,7 @@ TimerHandler(ClientData clientData)
27682768

27692769
ENTER_PYTHON
27702770

2771-
res = _PyObject_CallNoArg(func);
2771+
res = PyObject_CallNoArgs(func);
27722772
Py_DECREF(func);
27732773
Py_DECREF(v); /* See Tktt_New() */
27742774

Modules/readline.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ on_hook(PyObject *func)
867867
int result = 0;
868868
if (func != NULL) {
869869
PyObject *r;
870-
r = _PyObject_CallNoArg(func);
870+
r = PyObject_CallNoArgs(func);
871871
if (r == NULL)
872872
goto error;
873873
if (r == Py_None)

Objects/call.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ _Py_CheckFunctionResult(PyObject *callable, PyObject *result, const char *where)
7070

7171
/* --- Core PyObject call functions ------------------------------- */
7272

73+
/* Call a callable Python object without any arguments */
74+
PyObject *
75+
PyObject_CallNoArgs(PyObject *func)
76+
{
77+
return _PyObject_CallNoArg(func);
78+
}
79+
80+
7381
PyObject *
7482
_PyObject_FastCallDict(PyObject *callable, PyObject *const *args,
7583
size_t nargsf, PyObject *kwargs)

0 commit comments

Comments
 (0)