diff Python/bltinmodule.c @ 8581:221667f89064 legacy-trunk

Experimental feature: add default argument to getattr().
author Guido van Rossum <guido@python.org>
date Mon, 29 Jun 1998 13:38:57 +0000
parents 8973e7085cf4
children 97dc6aa532e7
line wrap: on
line diff
--- a/Python/bltinmodule.c	Mon Jun 29 04:00:40 1998 +0000
+++ b/Python/bltinmodule.c	Mon Jun 29 13:38:57 1998 +0000
@@ -669,18 +669,26 @@
 	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 *