diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2017-01-19 11:37:13 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2017-01-19 11:37:13 (GMT) |
commit | 7dc6a5f50aa47d32a75941dcb091eabe7958b2a1 (patch) | |
tree | c909658d7f5e9dc17bac36ef8552132b0cdd81de /Objects/clinic | |
parent | 06d34393c2f1f28bf97873418e2b981e7b188811 (diff) | |
download | cpython-7dc6a5f50aa47d32a75941dcb091eabe7958b2a1.zip cpython-7dc6a5f50aa47d32a75941dcb091eabe7958b2a1.tar.gz cpython-7dc6a5f50aa47d32a75941dcb091eabe7958b2a1.tar.bz2 |
dict.get() and dict.setdefault() now use AC
Issue #29311: dict.get() and dict.setdefault() methods now use Argument Clinic
to parse arguments. Their calling convention changes from METH_VARARGS to
METH_FASTCALL which avoids the creation of a temporary tuple.
The signature of docstrings is also enhanced. For example,
get(...)
becomes:
get(self, key, default=None, /)
Diffstat (limited to 'Objects/clinic')
-rw-r--r-- | Objects/clinic/dictobject.c.h | 70 |
1 files changed, 69 insertions, 1 deletions
diff --git a/Objects/clinic/dictobject.c.h b/Objects/clinic/dictobject.c.h index 3f06c0b..21c2b0b 100644 --- a/Objects/clinic/dictobject.c.h +++ b/Objects/clinic/dictobject.c.h @@ -44,4 +44,72 @@ PyDoc_STRVAR(dict___contains____doc__, #define DICT___CONTAINS___METHODDEF \ {"__contains__", (PyCFunction)dict___contains__, METH_O|METH_COEXIST, dict___contains____doc__}, -/*[clinic end generated code: output=69f3d767ed44e8ec input=a9049054013a1b77]*/ + +PyDoc_STRVAR(dict_get__doc__, +"get($self, key, default=None, /)\n" +"--\n" +"\n" +"D.get(key[, default]) -> D[key] if key in D, else default."); + +#define DICT_GET_METHODDEF \ + {"get", (PyCFunction)dict_get, METH_FASTCALL, dict_get__doc__}, + +static PyObject * +dict_get_impl(PyDictObject *self, PyObject *key, PyObject *failobj); + +static PyObject * +dict_get(PyDictObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + PyObject *key; + PyObject *failobj = Py_None; + + if (!_PyArg_UnpackStack(args, nargs, "get", + 1, 2, + &key, &failobj)) { + goto exit; + } + + if (!_PyArg_NoStackKeywords("get", kwnames)) { + goto exit; + } + return_value = dict_get_impl(self, key, failobj); + +exit: + return return_value; +} + +PyDoc_STRVAR(dict_setdefault__doc__, +"setdefault($self, key, default=None, /)\n" +"--\n" +"\n" +"D.get(key,default), also set D[key]=default if key not in D."); + +#define DICT_SETDEFAULT_METHODDEF \ + {"setdefault", (PyCFunction)dict_setdefault, METH_FASTCALL, dict_setdefault__doc__}, + +static PyObject * +dict_setdefault_impl(PyDictObject *self, PyObject *key, PyObject *defaultobj); + +static PyObject * +dict_setdefault(PyDictObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + PyObject *key; + PyObject *defaultobj = Py_None; + + if (!_PyArg_UnpackStack(args, nargs, "setdefault", + 1, 2, + &key, &defaultobj)) { + goto exit; + } + + if (!_PyArg_NoStackKeywords("setdefault", kwnames)) { + goto exit; + } + return_value = dict_setdefault_impl(self, key, defaultobj); + +exit: + return return_value; +} +/*[clinic end generated code: output=1b0cea84b4b6989e input=a9049054013a1b77]*/ |