bpo-41333: Convert OrderedDict.pop() to Argument Clinic (GH-21534)

This commit is contained in:
Serhiy Storchaka 2020-07-19 09:18:55 +03:00 committed by GitHub
parent c53b310e59
commit 6bf3237379
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 68 additions and 25 deletions

View File

@ -0,0 +1 @@
:meth:`collections.OrderedDict.pop` is now 2 times faster.

View File

@ -122,7 +122,8 @@ PyDoc_STRVAR(dict_pop__doc__,
"\n" "\n"
"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n" "D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n"
"\n" "\n"
"If key is not found, default is returned if given, otherwise KeyError is raised"); "If the key is not found, return the default if given; otherwise,\n"
"raise a KeyError.");
#define DICT_POP_METHODDEF \ #define DICT_POP_METHODDEF \
{"pop", (PyCFunction)(void(*)(void))dict_pop, METH_FASTCALL, dict_pop__doc__}, {"pop", (PyCFunction)(void(*)(void))dict_pop, METH_FASTCALL, dict_pop__doc__},
@ -190,4 +191,4 @@ dict___reversed__(PyDictObject *self, PyObject *Py_UNUSED(ignored))
{ {
return dict___reversed___impl(self); return dict___reversed___impl(self);
} }
/*[clinic end generated code: output=4d98145508da8fa3 input=a9049054013a1b77]*/ /*[clinic end generated code: output=7b77c16e43d6735a input=a9049054013a1b77]*/

View File

@ -83,6 +83,49 @@ exit:
return return_value; return return_value;
} }
PyDoc_STRVAR(OrderedDict_pop__doc__,
"pop($self, /, key, default=<unrepresentable>)\n"
"--\n"
"\n"
"od.pop(key[,default]) -> v, remove specified key and return the corresponding value.\n"
"\n"
"If the key is not found, return the default if given; otherwise,\n"
"raise a KeyError.");
#define ORDEREDDICT_POP_METHODDEF \
{"pop", (PyCFunction)(void(*)(void))OrderedDict_pop, METH_FASTCALL|METH_KEYWORDS, OrderedDict_pop__doc__},
static PyObject *
OrderedDict_pop_impl(PyODictObject *self, PyObject *key,
PyObject *default_value);
static PyObject *
OrderedDict_pop(PyODictObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
static const char * const _keywords[] = {"key", "default", NULL};
static _PyArg_Parser _parser = {NULL, _keywords, "pop", 0};
PyObject *argsbuf[2];
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
PyObject *key;
PyObject *default_value = NULL;
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
if (!args) {
goto exit;
}
key = args[0];
if (!noptargs) {
goto skip_optional_pos;
}
default_value = args[1];
skip_optional_pos:
return_value = OrderedDict_pop_impl(self, key, default_value);
exit:
return return_value;
}
PyDoc_STRVAR(OrderedDict_popitem__doc__, PyDoc_STRVAR(OrderedDict_popitem__doc__,
"popitem($self, /, last=True)\n" "popitem($self, /, last=True)\n"
"--\n" "--\n"
@ -168,4 +211,4 @@ skip_optional_pos:
exit: exit:
return return_value; return return_value;
} }
/*[clinic end generated code: output=8eb1296df9142908 input=a9049054013a1b77]*/ /*[clinic end generated code: output=e0afaad5b4bb47fe input=a9049054013a1b77]*/

View File

@ -3054,12 +3054,13 @@ dict.pop
D.pop(k[,d]) -> v, remove specified key and return the corresponding value. D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, default is returned if given, otherwise KeyError is raised If the key is not found, return the default if given; otherwise,
raise a KeyError.
[clinic start generated code]*/ [clinic start generated code]*/
static PyObject * static PyObject *
dict_pop_impl(PyDictObject *self, PyObject *key, PyObject *default_value) dict_pop_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
/*[clinic end generated code: output=3abb47b89f24c21c input=eeebec7812190348]*/ /*[clinic end generated code: output=3abb47b89f24c21c input=e221baa01044c44c]*/
{ {
return _PyDict_Pop((PyObject*)self, key, default_value); return _PyDict_Pop((PyObject*)self, key, default_value);
} }

View File

@ -1045,30 +1045,28 @@ OrderedDict_setdefault_impl(PyODictObject *self, PyObject *key,
/* pop() */ /* pop() */
PyDoc_STRVAR(odict_pop__doc__,
"od.pop(k[,d]) -> v, remove specified key and return the corresponding\n\
value. If key is not found, d is returned if given, otherwise KeyError\n\
is raised.\n\
\n\
");
/* forward */ /* forward */
static PyObject * _odict_popkey(PyObject *, PyObject *, PyObject *); static PyObject * _odict_popkey(PyObject *, PyObject *, PyObject *);
/* Skips __missing__() calls. */ /* Skips __missing__() calls. */
/*[clinic input]
OrderedDict.pop
key: object
default: object = NULL
od.pop(key[,default]) -> v, remove specified key and return the corresponding value.
If the key is not found, return the default if given; otherwise,
raise a KeyError.
[clinic start generated code]*/
static PyObject * static PyObject *
odict_pop(PyObject *od, PyObject *args, PyObject *kwargs) OrderedDict_pop_impl(PyODictObject *self, PyObject *key,
PyObject *default_value)
/*[clinic end generated code: output=7a6447d104e7494b input=7efe36601007dff7]*/
{ {
static char *kwlist[] = {"key", "default", 0}; return _odict_popkey((PyObject *)self, key, default_value);
PyObject *key, *failobj = NULL;
/* borrowed */
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:pop", kwlist,
&key, &failobj)) {
return NULL;
}
return _odict_popkey(od, key, failobj);
} }
static PyObject * static PyObject *
@ -1362,8 +1360,7 @@ static PyMethodDef odict_methods[] = {
{"__reduce__", (PyCFunction)odict_reduce, METH_NOARGS, {"__reduce__", (PyCFunction)odict_reduce, METH_NOARGS,
odict_reduce__doc__}, odict_reduce__doc__},
ORDEREDDICT_SETDEFAULT_METHODDEF ORDEREDDICT_SETDEFAULT_METHODDEF
{"pop", (PyCFunction)(void(*)(void))odict_pop, ORDEREDDICT_POP_METHODDEF
METH_VARARGS | METH_KEYWORDS, odict_pop__doc__},
ORDEREDDICT_POPITEM_METHODDEF ORDEREDDICT_POPITEM_METHODDEF
{"keys", odictkeys_new, METH_NOARGS, {"keys", odictkeys_new, METH_NOARGS,
odict_keys__doc__}, odict_keys__doc__},