Skip to content

Commit

Permalink
bpo-41333: Convert OrderedDict.pop() to Argument Clinic (pythonGH-21534)
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka authored Jul 19, 2020
1 parent c53b310 commit 6bf3237
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:meth:`collections.OrderedDict.pop` is now 2 times faster.
5 changes: 3 additions & 2 deletions Objects/clinic/dictobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 44 additions & 1 deletion Objects/clinic/odictobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3054,12 +3054,13 @@ dict.pop
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]*/

static PyObject *
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);
}
Expand Down
37 changes: 17 additions & 20 deletions Objects/odictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1045,30 +1045,28 @@ OrderedDict_setdefault_impl(PyODictObject *self, PyObject *key,

/* 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 */
static PyObject * _odict_popkey(PyObject *, PyObject *, PyObject *);

/* Skips __missing__() calls. */
static PyObject *
odict_pop(PyObject *od, PyObject *args, PyObject *kwargs)
{
static char *kwlist[] = {"key", "default", 0};
PyObject *key, *failobj = NULL;
/*[clinic input]
OrderedDict.pop
/* borrowed */
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:pop", kwlist,
&key, &failobj)) {
return NULL;
}
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]*/

return _odict_popkey(od, key, failobj);
static PyObject *
OrderedDict_pop_impl(PyODictObject *self, PyObject *key,
PyObject *default_value)
/*[clinic end generated code: output=7a6447d104e7494b input=7efe36601007dff7]*/
{
return _odict_popkey((PyObject *)self, key, default_value);
}

static PyObject *
Expand Down Expand Up @@ -1362,8 +1360,7 @@ static PyMethodDef odict_methods[] = {
{"__reduce__", (PyCFunction)odict_reduce, METH_NOARGS,
odict_reduce__doc__},
ORDEREDDICT_SETDEFAULT_METHODDEF
{"pop", (PyCFunction)(void(*)(void))odict_pop,
METH_VARARGS | METH_KEYWORDS, odict_pop__doc__},
ORDEREDDICT_POP_METHODDEF
ORDEREDDICT_POPITEM_METHODDEF
{"keys", odictkeys_new, METH_NOARGS,
odict_keys__doc__},
Expand Down

0 comments on commit 6bf3237

Please sign in to comment.