changeset: 106204:5dd5d50c4d58 tag: tip user: Victor Stinner date: Tue Jan 17 15:28:36 2017 +0100 files: Python/bltinmodule.c Python/clinic/bltinmodule.c.h description: getattr() now uses Argument Clinic diff -r 1a97b10cb420 -r 5dd5d50c4d58 Python/bltinmodule.c --- a/Python/bltinmodule.c Tue Jan 17 04:20:26 2017 +0100 +++ b/Python/bltinmodule.c Tue Jan 17 15:28:36 2017 +0100 @@ -991,26 +991,33 @@ builtin_exec_impl(PyObject *module, PyOb } -/* AC: cannot convert yet, as needs PEP 457 group support in inspect */ +/*[clinic input] +getattr as builtin_getattr + + object as v: object + name: object + default as dflt: object = NULL + / + +Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y. + +When a default argument is given, it is returned when the attribute doesn't +exist; without it, an exception is raised in that case. +[clinic start generated code]*/ + static PyObject * -builtin_getattr(PyObject *self, PyObject **args, Py_ssize_t nargs, - PyObject *kwnames) +builtin_getattr_impl(PyObject *module, PyObject *v, PyObject *name, + PyObject *dflt) +/*[clinic end generated code: output=dd719f3bee023bc8 input=dc85a89814d5c7da]*/ { - PyObject *v, *result, *dflt = NULL; - PyObject *name; - - if (!_PyArg_UnpackStack(args, nargs, "getattr", 2, 3, &v, &name, &dflt)) - return NULL; - - if (!_PyArg_NoStackKeywords("getattr", kwnames)) { - return NULL; - } + PyObject *result; if (!PyUnicode_Check(name)) { PyErr_SetString(PyExc_TypeError, "getattr(): attribute name must be string"); return NULL; } + result = PyObject_GetAttr(v, name); if (result == NULL && dflt != NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) @@ -1022,12 +1029,6 @@ builtin_getattr(PyObject *self, PyObject return result; } -PyDoc_STRVAR(getattr_doc, -"getattr(object, name[, default]) -> value\n\ -\n\ -Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\ -When a default argument is given, it is returned when the attribute doesn't\n\ -exist; without it, an exception is raised in that case."); /*[clinic input] @@ -2631,7 +2632,7 @@ static PyMethodDef builtin_methods[] = { BUILTIN_EVAL_METHODDEF BUILTIN_EXEC_METHODDEF BUILTIN_FORMAT_METHODDEF - {"getattr", (PyCFunction)builtin_getattr, METH_FASTCALL, getattr_doc}, + BUILTIN_GETATTR_METHODDEF BUILTIN_GLOBALS_METHODDEF BUILTIN_HASATTR_METHODDEF BUILTIN_HASH_METHODDEF