diff Python/bltinmodule.c @ 385:039aee6cbd78 legacy-trunk

Protect dir() against non-directory __dict__ attributes.
author Guido van Rossum <guido@python.org>
date Sun, 20 Oct 1991 20:11:03 +0000
parents be3052a77b7f
children 9cd38d1ac391
line wrap: on
line diff
--- a/Python/bltinmodule.c	Sun Oct 20 20:10:46 1991 +0000
+++ b/Python/bltinmodule.c	Sun Oct 20 20:11:03 1991 +0000
@@ -78,20 +78,27 @@
 	object *d;
 	if (v == NULL) {
 		d = getlocals();
+		INCREF(d);
 	}
 	else {
-		if (!is_moduleobject(v)) {
+		d = getattr(v, "__dict__");
+		if (d == NULL) {
 			err_setstr(TypeError,
-				"dir() argument must be module or absent");
+				"dir() argument has no variable attributes");
 			return NULL;
 		}
-		d = getmoduledict(v);
+	}
+	if (!is_dictobject(d)) { /* Assume it is None */
+		v = newlistobject(0);
 	}
-	v = getdictkeys(d);
-	if (sortlist(v) != 0) {
-		DECREF(v);
-		v = NULL;
+	else {
+		v = getdictkeys(d);
+		if (sortlist(v) != 0) {
+			DECREF(v);
+			v = NULL;
+		}
 	}
+	DECREF(d);
 	return v;
 }