summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorJeroen Demeyer <J.Demeyer@UGent.be>2019-06-28 09:49:00 (GMT)
committerInada Naoki <songofacandy@gmail.com>2019-06-28 09:49:00 (GMT)
commitb1263d5a60d3f7ab02dd28409fff59b3815a3f67 (patch)
treed2b48c8ffcf6b8d4c042495e0b8888b7deda086e /Objects
parentb4bee03087a3c70cb040e2ce569c828860ed8e87 (diff)
downloadcpython-b1263d5a60d3f7ab02dd28409fff59b3815a3f67.zip
cpython-b1263d5a60d3f7ab02dd28409fff59b3815a3f67.tar.gz
cpython-b1263d5a60d3f7ab02dd28409fff59b3815a3f67.tar.bz2
bpo-37337: Add _PyObject_VectorcallMethod() (GH-14228)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/call.c32
-rw-r--r--Objects/descrobject.c21
2 files changed, 46 insertions, 7 deletions
diff --git a/Objects/call.c b/Objects/call.c
index bde5513..2b52bdf 100644
--- a/Objects/call.c
+++ b/Objects/call.c
@@ -1078,6 +1078,38 @@ object_vacall(PyObject *base, PyObject *callable, va_list vargs)
PyObject *
+_PyObject_VectorcallMethod(PyObject *name, PyObject *const *args,
+ size_t nargsf, PyObject *kwnames)
+{
+ assert(name != NULL);
+ assert(args != NULL);
+ assert(PyVectorcall_NARGS(nargsf) >= 1);
+
+ PyObject *callable = NULL;
+ /* Use args[0] as "self" argument */
+ int unbound = _PyObject_GetMethod(args[0], name, &callable);
+ if (callable == NULL) {
+ return NULL;
+ }
+
+ if (unbound) {
+ /* We must remove PY_VECTORCALL_ARGUMENTS_OFFSET since
+ * that would be interpreted as allowing to change args[-1] */
+ nargsf &= ~PY_VECTORCALL_ARGUMENTS_OFFSET;
+ }
+ else {
+ /* Skip "self". We can keep PY_VECTORCALL_ARGUMENTS_OFFSET since
+ * args[-1] in the onward call is args[0] here. */
+ args++;
+ nargsf--;
+ }
+ PyObject *result = _PyObject_Vectorcall(callable, args, nargsf, kwnames);
+ Py_DECREF(callable);
+ return result;
+}
+
+
+PyObject *
PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...)
{
if (obj == NULL || name == NULL) {
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
index 806c0af..a0eb505 100644
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -851,15 +851,22 @@ static PySequenceMethods mappingproxy_as_sequence = {
};
static PyObject *
-mappingproxy_get(mappingproxyobject *pp, PyObject *args)
+mappingproxy_get(mappingproxyobject *pp, PyObject *const *args, Py_ssize_t nargs)
{
- PyObject *key, *def = Py_None;
- _Py_IDENTIFIER(get);
+ /* newargs: mapping, key, default=None */
+ PyObject *newargs[3];
+ newargs[0] = pp->mapping;
+ newargs[2] = Py_None;
- if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &def))
+ if (!_PyArg_UnpackStack(args, nargs, "get", 1, 2,
+ &newargs[1], &newargs[2]))
+ {
return NULL;
- return _PyObject_CallMethodIdObjArgs(pp->mapping, &PyId_get,
- key, def, NULL);
+ }
+ _Py_IDENTIFIER(get);
+ return _PyObject_VectorcallMethodId(&PyId_get, newargs,
+ 3 | PY_VECTORCALL_ARGUMENTS_OFFSET,
+ NULL);
}
static PyObject *
@@ -894,7 +901,7 @@ mappingproxy_copy(mappingproxyobject *pp, PyObject *Py_UNUSED(ignored))
to the underlying mapping */
static PyMethodDef mappingproxy_methods[] = {
- {"get", (PyCFunction)mappingproxy_get, METH_VARARGS,
+ {"get", (PyCFunction)mappingproxy_get, METH_FASTCALL,
PyDoc_STR("D.get(k[,d]) -> D[k] if k in D, else d."
" d defaults to None.")},
{"keys", (PyCFunction)mappingproxy_keys, METH_NOARGS,