Skip to content

bpo-37483: add _PyObject_CallOneArg() function #14558

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Doc/c-api/object.rst
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,17 @@ Object Protocol
.. versionadded:: 3.9


.. c:function:: PyObject* _PyObject_CallOneArg(PyObject *callable, PyObject *arg)

Call a callable Python object *callable* with exactly 1 positional argument
*arg* and no keyword arguments.

Return the result of the call on success, or raise an exception and return
*NULL* on failure.

.. versionadded:: 3.9


.. c:function:: PyObject* PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)

Call a callable Python object *callable*, with arguments given by the
Expand Down
12 changes: 12 additions & 0 deletions Include/cpython/abstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ PyVectorcall_NARGS(size_t n)
static inline vectorcallfunc
_PyVectorcall_Function(PyObject *callable)
{
assert(callable != NULL);
PyTypeObject *tp = Py_TYPE(callable);
if (!PyType_HasFeature(tp, _Py_TPFLAGS_HAVE_VECTORCALL)) {
return NULL;
Expand Down Expand Up @@ -134,6 +135,17 @@ _PyObject_CallNoArg(PyObject *func) {
return _PyObject_Vectorcall(func, NULL, 0, NULL);
}

static inline PyObject *
_PyObject_CallOneArg(PyObject *func, PyObject *arg)
{
assert(arg != NULL);
PyObject *_args[2];
PyObject **args = _args + 1; // For PY_VECTORCALL_ARGUMENTS_OFFSET
args[0] = arg;
return _PyObject_Vectorcall(func, args,
1 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
}

PyAPI_FUNC(PyObject *) _PyObject_Call_Prepend(
PyObject *callable,
PyObject *obj,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add new function ``_PyObject_CallOneArg`` for calling an object with one
positional argument.
21 changes: 9 additions & 12 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ _is_coroutine(PyObject *coro)
Do this check after 'future_init()'; in case we need to raise
an error, __del__ needs a properly initialized object.
*/
PyObject *res = PyObject_CallFunctionObjArgs(
asyncio_iscoroutine_func, coro, NULL);
PyObject *res = _PyObject_CallOneArg(asyncio_iscoroutine_func, coro);
if (res == NULL) {
return -1;
}
Expand Down Expand Up @@ -1286,8 +1285,7 @@ static PyObject *
_asyncio_Future__repr_info_impl(FutureObj *self)
/*[clinic end generated code: output=fa69e901bd176cfb input=f21504d8e2ae1ca2]*/
{
return PyObject_CallFunctionObjArgs(
asyncio_future_repr_info_func, self, NULL);
return _PyObject_CallOneArg(asyncio_future_repr_info_func, (PyObject *)self);
}

static PyObject *
Expand Down Expand Up @@ -1364,7 +1362,7 @@ FutureObj_finalize(FutureObj *fut)

func = _PyObject_GetAttrId(fut->fut_loop, &PyId_call_exception_handler);
if (func != NULL) {
PyObject *res = PyObject_CallFunctionObjArgs(func, context, NULL);
PyObject *res = _PyObject_CallOneArg(func, context);
if (res == NULL) {
PyErr_WriteUnraisable(func);
}
Expand Down Expand Up @@ -2104,13 +2102,13 @@ _asyncio_Task_current_task_impl(PyTypeObject *type, PyObject *loop)
Py_DECREF(current_task_func);
return NULL;
}
ret = PyObject_CallFunctionObjArgs(current_task_func, loop, NULL);
ret = _PyObject_CallOneArg(current_task_func, loop);
Py_DECREF(current_task_func);
Py_DECREF(loop);
return ret;
}
else {
ret = PyObject_CallFunctionObjArgs(current_task_func, loop, NULL);
ret = _PyObject_CallOneArg(current_task_func, loop);
Py_DECREF(current_task_func);
return ret;
}
Expand Down Expand Up @@ -2146,7 +2144,7 @@ _asyncio_Task_all_tasks_impl(PyTypeObject *type, PyObject *loop)
return NULL;
}

res = PyObject_CallFunctionObjArgs(all_tasks_func, loop, NULL);
res = _PyObject_CallOneArg(all_tasks_func, loop);
Py_DECREF(all_tasks_func);
return res;
}
Expand All @@ -2159,8 +2157,7 @@ static PyObject *
_asyncio_Task__repr_info_impl(TaskObj *self)
/*[clinic end generated code: output=6a490eb66d5ba34b input=3c6d051ed3ddec8b]*/
{
return PyObject_CallFunctionObjArgs(
asyncio_task_repr_info_func, self, NULL);
return _PyObject_CallOneArg(asyncio_task_repr_info_func, (PyObject *)self);
}

/*[clinic input]
Expand Down Expand Up @@ -2411,7 +2408,7 @@ TaskObj_finalize(TaskObj *task)

func = _PyObject_GetAttrId(task->task_loop, &PyId_call_exception_handler);
if (func != NULL) {
PyObject *res = PyObject_CallFunctionObjArgs(func, context, NULL);
PyObject *res = _PyObject_CallOneArg(func, context);
if (res == NULL) {
PyErr_WriteUnraisable(func);
}
Expand Down Expand Up @@ -2543,7 +2540,7 @@ task_set_error_soon(TaskObj *task, PyObject *et, const char *format, ...)
return NULL;
}

PyObject *e = PyObject_CallFunctionObjArgs(et, msg, NULL);
PyObject *e = _PyObject_CallOneArg(et, msg);
Py_DECREF(msg);
if (e == NULL) {
return NULL;
Expand Down
3 changes: 1 addition & 2 deletions Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,7 @@ deque_copy(PyObject *deque, PyObject *Py_UNUSED(ignored))
return NULL;
}
if (old_deque->maxlen < 0)
result = PyObject_CallFunctionObjArgs((PyObject *)(Py_TYPE(deque)),
deque, NULL);
result = _PyObject_CallOneArg((PyObject *)(Py_TYPE(deque)), deque);
else
result = PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
deque, old_deque->maxlen, NULL);
Expand Down
2 changes: 1 addition & 1 deletion Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ csv_writerow(WriterObj *self, PyObject *seq)
if (line == NULL) {
return NULL;
}
result = PyObject_CallFunctionObjArgs(self->write, line, NULL);
result = _PyObject_CallOneArg(self->write, line);
Py_DECREF(line);
return result;
}
Expand Down
8 changes: 4 additions & 4 deletions Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ static PyObject *GetResult(PyObject *restype, void *result, PyObject *checker)
if (!checker || !retval)
return retval;

v = PyObject_CallFunctionObjArgs(checker, retval, NULL);
v = _PyObject_CallOneArg(checker, retval);
if (v == NULL)
_PyTraceback_Add("GetResult", "_ctypes/callproc.c", __LINE__-2);
Py_DECREF(retval);
Expand Down Expand Up @@ -1118,7 +1118,7 @@ PyObject *_ctypes_callproc(PPROC pProc,
if (argtypes && argtype_count > i) {
PyObject *v;
converter = PyTuple_GET_ITEM(argtypes, i);
v = PyObject_CallFunctionObjArgs(converter, arg, NULL);
v = _PyObject_CallOneArg(converter, arg);
if (v == NULL) {
_ctypes_extend_error(PyExc_ArgError, "argument %zd: ", i+1);
goto cleanup;
Expand Down Expand Up @@ -1795,15 +1795,15 @@ pointer(PyObject *self, PyObject *arg)

typ = PyDict_GetItemWithError(_ctypes_ptrtype_cache, (PyObject *)Py_TYPE(arg));
if (typ) {
return PyObject_CallFunctionObjArgs(typ, arg, NULL);
return _PyObject_CallOneArg(typ, arg);
}
else if (PyErr_Occurred()) {
return NULL;
}
typ = POINTER(NULL, (PyObject *)Py_TYPE(arg));
if (typ == NULL)
return NULL;
result = PyObject_CallFunctionObjArgs(typ, arg, NULL);
result = _PyObject_CallOneArg(typ, arg);
Py_DECREF(typ);
return result;
}
Expand Down
16 changes: 8 additions & 8 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -2696,7 +2696,7 @@ treebuilder_append_event(TreeBuilderObject *self, PyObject *action,
PyObject *event = PyTuple_Pack(2, action, node);
if (event == NULL)
return -1;
res = _PyObject_FastCall(self->events_append, &event, 1);
res = _PyObject_CallOneArg(self->events_append, event);
Py_DECREF(event);
if (res == NULL)
return -1;
Expand Down Expand Up @@ -2859,7 +2859,7 @@ treebuilder_handle_comment(TreeBuilderObject* self, PyObject* text)
}

if (self->comment_factory) {
comment = _PyObject_FastCall(self->comment_factory, &text, 1);
comment = _PyObject_CallOneArg(self->comment_factory, text);
if (!comment)
return NULL;

Expand Down Expand Up @@ -3197,7 +3197,7 @@ expat_set_error(enum XML_Error error_code, Py_ssize_t line, Py_ssize_t column,
if (errmsg == NULL)
return;

error = _PyObject_FastCall(st->parseerror_obj, &errmsg, 1);
error = _PyObject_CallOneArg(st->parseerror_obj, errmsg);
Py_DECREF(errmsg);
if (!error)
return;
Expand Down Expand Up @@ -3260,7 +3260,7 @@ expat_default_handler(XMLParserObject* self, const XML_Char* data_in,
(TreeBuilderObject*) self->target, value
);
else if (self->handle_data)
res = _PyObject_FastCall(self->handle_data, &value, 1);
res = _PyObject_CallOneArg(self->handle_data, value);
else
res = NULL;
Py_XDECREF(res);
Expand Down Expand Up @@ -3371,7 +3371,7 @@ expat_data_handler(XMLParserObject* self, const XML_Char* data_in,
/* shortcut */
res = treebuilder_handle_data((TreeBuilderObject*) self->target, data);
else if (self->handle_data)
res = _PyObject_FastCall(self->handle_data, &data, 1);
res = _PyObject_CallOneArg(self->handle_data, data);
else
res = NULL;

Expand All @@ -3398,7 +3398,7 @@ expat_end_handler(XMLParserObject* self, const XML_Char* tag_in)
else if (self->handle_end) {
tag = makeuniversal(self, tag_in);
if (tag) {
res = _PyObject_FastCall(self->handle_end, &tag, 1);
res = _PyObject_CallOneArg(self->handle_end, tag);
Py_DECREF(tag);
}
}
Expand Down Expand Up @@ -3485,7 +3485,7 @@ expat_end_ns_handler(XMLParserObject* self, const XML_Char* prefix_in)
if (!prefix)
return;

res = _PyObject_FastCall(self->handle_end_ns, &prefix, 1);
res = _PyObject_CallOneArg(self->handle_end_ns, prefix);
Py_DECREF(prefix);
}

Expand Down Expand Up @@ -3515,7 +3515,7 @@ expat_comment_handler(XMLParserObject* self, const XML_Char* comment_in)
if (!comment)
return;

res = _PyObject_FastCall(self->handle_comment, &comment, 1);
res = _PyObject_CallOneArg(self->handle_comment, comment);
}

Py_XDECREF(res);
Expand Down
2 changes: 1 addition & 1 deletion Modules/_io/iobase.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ _io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit)
PyObject *b;

if (peek != NULL) {
PyObject *readahead = PyObject_CallFunctionObjArgs(peek, _PyLong_One, NULL);
PyObject *readahead = _PyObject_CallOneArg(peek, _PyLong_One);
if (readahead == NULL) {
/* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
when EINTR occurs so we needn't do it ourselves. */
Expand Down
12 changes: 6 additions & 6 deletions Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -818,14 +818,14 @@ _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss
*next_idx_ptr = idx + 1;

if (has_pairs_hook) {
val = PyObject_CallFunctionObjArgs(s->object_pairs_hook, rval, NULL);
val = _PyObject_CallOneArg(s->object_pairs_hook, rval);
Py_DECREF(rval);
return val;
}

/* if object_hook is not None: rval = object_hook(rval) */
if (s->object_hook != Py_None) {
val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL);
val = _PyObject_CallOneArg(s->object_hook, rval);
Py_DECREF(rval);
return val;
}
Expand Down Expand Up @@ -931,7 +931,7 @@ _parse_constant(PyScannerObject *s, const char *constant, Py_ssize_t idx, Py_ssi
return NULL;

/* rval = parse_constant(constant) */
rval = PyObject_CallFunctionObjArgs(s->parse_constant, cstr, NULL);
rval = _PyObject_CallOneArg(s->parse_constant, cstr);
idx += PyUnicode_GET_LENGTH(cstr);
Py_DECREF(cstr);
*next_idx_ptr = idx;
Expand Down Expand Up @@ -1030,7 +1030,7 @@ _match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_
idx - start);
if (numstr == NULL)
return NULL;
rval = PyObject_CallFunctionObjArgs(custom_func, numstr, NULL);
rval = _PyObject_CallOneArg(custom_func, numstr);
}
else {
Py_ssize_t i, n;
Expand Down Expand Up @@ -1440,7 +1440,7 @@ encoder_encode_string(PyEncoderObject *s, PyObject *obj)
if (s->fast_encode) {
return s->fast_encode(NULL, obj);
}
encoded = PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
encoded = _PyObject_CallOneArg(s->encoder, obj);
if (encoded != NULL && !PyUnicode_Check(encoded)) {
PyErr_Format(PyExc_TypeError,
"encoder() must return a string, not %.80s",
Expand Down Expand Up @@ -1526,7 +1526,7 @@ encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
return -1;
}
}
newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL);
newobj = _PyObject_CallOneArg(s->defaultfn, obj);
if (newobj == NULL) {
Py_XDECREF(ident);
return -1;
Expand Down
12 changes: 5 additions & 7 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ _Pickle_FastCall(PyObject *func, PyObject *obj)
{
PyObject *result;

result = PyObject_CallFunctionObjArgs(func, obj, NULL);
result = _PyObject_CallOneArg(func, obj);
Py_DECREF(obj);
return result;
}
Expand Down Expand Up @@ -420,7 +420,7 @@ call_method(PyObject *func, PyObject *self, PyObject *obj)
return PyObject_CallFunctionObjArgs(func, self, obj, NULL);
}
else {
return PyObject_CallFunctionObjArgs(func, obj, NULL);
return _PyObject_CallOneArg(func, obj);
}
}

Expand Down Expand Up @@ -2296,7 +2296,7 @@ _Pickler_write_bytes(PicklerObject *self,
return -1;
}
}
result = PyObject_CallFunctionObjArgs(self->write, payload, NULL);
result = _PyObject_CallOneArg(self->write, payload);
Py_XDECREF(mem);
if (result == NULL) {
return -1;
Expand Down Expand Up @@ -2504,8 +2504,7 @@ save_picklebuffer(PicklerObject *self, PyObject *obj)
}
int in_band = 1;
if (self->buffer_callback != NULL) {
PyObject *ret = PyObject_CallFunctionObjArgs(self->buffer_callback,
obj, NULL);
PyObject *ret = _PyObject_CallOneArg(self->buffer_callback, obj);
if (ret == NULL) {
return -1;
}
Expand Down Expand Up @@ -4322,8 +4321,7 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
* regular reduction mechanism.
*/
if (self->reducer_override != NULL) {
reduce_value = PyObject_CallFunctionObjArgs(self->reducer_override,
obj, NULL);
reduce_value = _PyObject_CallOneArg(self->reducer_override, obj);
if (reduce_value == NULL) {
goto error;
}
Expand Down
6 changes: 4 additions & 2 deletions Modules/_sqlite/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,8 @@ void pysqlite_cache_dealloc(pysqlite_Cache* self)
Py_TYPE(self)->tp_free((PyObject*)self);
}

PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* args)
PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* key)
{
PyObject* key = args;
pysqlite_Node* node;
pysqlite_Node* ptr;
PyObject* data;
Expand Down Expand Up @@ -184,6 +183,9 @@ PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* args)
}
}

/* We cannot replace this by _PyObject_CallOneArg() since
* PyObject_CallFunction() has a special case when using a
* single tuple as argument. */
data = PyObject_CallFunction(self->factory, "O", key);

if (!data) {
Expand Down
Loading