diff Objects/classobject.c @ 2105:ea7b2bf0b521 legacy-trunk

add restrictions in restricted mode
author Guido van Rossum <guido@python.org>
date Tue, 10 Jan 1995 10:39:49 +0000
parents abbf2a4b488b
children 2669ef14c205
line wrap: on
line diff
--- a/Objects/classobject.c	Tue Jan 10 10:39:16 1995 +0000
+++ b/Objects/classobject.c	Tue Jan 10 10:39:49 1995 +0000
@@ -116,21 +116,28 @@
 {
 	register object *v;
 	classobject *class;
-	if (strcmp(name, "__dict__") == 0) {
-		INCREF(op->cl_dict);
-		return op->cl_dict;
-	}
-	if (strcmp(name, "__bases__") == 0) {
-		INCREF(op->cl_bases);
-		return op->cl_bases;
-	}
-	if (strcmp(name, "__name__") == 0) {
-		if (op->cl_name == NULL)
-			v = None;
-		else
-			v = op->cl_name;
-		INCREF(v);
-		return v;
+	if (name[0] == '_' && name[1] == '_') {
+		if (strcmp(name, "__dict__") == 0) {
+			if (getrestricted()) {
+				err_setstr(RuntimeError,
+					   "class.__dict__ not accessible in restricted mode");
+				return NULL;
+			}
+			INCREF(op->cl_dict);
+			return op->cl_dict;
+		}
+		if (strcmp(name, "__bases__") == 0) {
+			INCREF(op->cl_bases);
+			return op->cl_bases;
+		}
+		if (strcmp(name, "__name__") == 0) {
+			if (op->cl_name == NULL)
+				v = None;
+			else
+				v = op->cl_name;
+			INCREF(v);
+			return v;
+		}
 	}
 	v = class_lookup(op, name, &class);
 	if (v == NULL) {
@@ -365,6 +372,11 @@
 	classobject *class;
 	if (name[0] == '_' && name[1] == '_') {
 		if (strcmp(name, "__dict__") == 0) {
+			if (getrestricted()) {
+				err_setstr(RuntimeError,
+					   "instance.__dict__ not accessible in restricted mode");
+				return NULL;
+			}
 			INCREF(inst->in_dict);
 			return inst->in_dict;
 		}
@@ -420,15 +432,6 @@
 	res = instance_getattr1(inst, name);
 	if (res == NULL && (func = inst->in_class->cl_getattr) != NULL) {
 		object *args;
-#if 0
-		if (name[0] == '_' && name[1] == '_') {
-			int n = strlen(name);
-			if (name[n-1] == '_' && name[n-2] == '_') {
-				/* Don't mess with system attributes */
-				return NULL;
-			}
-		}
-#endif
 		err_clear();
 		args = mkvalue("(Os)", inst, name);
 		if (args == NULL)
@@ -1132,6 +1135,11 @@
 	register instancemethodobject *im;
 	char *name;
 {
+	if (name[0] != '_' && getrestricted()) {
+		err_setstr(RuntimeError,
+			   "instance-method attributes not accessible in restricted mode");
+		return NULL;
+	}
 	return getmember((char *)im, instancemethod_memberlist, name);
 }