diff options
author | Guido van Rossum <guido@python.org> | 1998-06-29 13:38:57 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1998-06-29 13:38:57 (GMT) |
commit | 950ff2923af04c5579775ac453f714cb4eb08ea1 (patch) | |
tree | eb7cade64429e65e6f827c9a9e0d1562185cbf25 /Python | |
parent | 39ef2274a39cbae96b1335969f84da9a9adadc27 (diff) | |
download | cpython-950ff2923af04c5579775ac453f714cb4eb08ea1.zip cpython-950ff2923af04c5579775ac453f714cb4eb08ea1.tar.gz cpython-950ff2923af04c5579775ac453f714cb4eb08ea1.tar.bz2 |
Experimental feature: add default argument to getattr().
Diffstat (limited to 'Python')
-rw-r--r-- | Python/bltinmodule.c | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 85d8b37..727f8d1 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -669,18 +669,26 @@ builtin_getattr(self, args) PyObject *self; PyObject *args; { - PyObject *v; + PyObject *v, *result, *dflt = NULL; PyObject *name; - if (!PyArg_ParseTuple(args, "OS:getattr", &v, &name)) + if (!PyArg_ParseTuple(args, "OS|O:getattr", &v, &name, &dflt)) return NULL; - return PyObject_GetAttr(v, name); + result = PyObject_GetAttr(v, name); + if (result == NULL && dflt != NULL) { + PyErr_Clear(); + Py_INCREF(dflt); + result = dflt; + } + return result; } static char getattr_doc[] = -"getattr(object, name) -> value\n\ +"getattr(object, name[, default]) -> value\n\ \n\ -Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y."; +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."; static PyObject * |