[Zope-Checkins] CVS: Zope3/lib/python/Zope/Security - _Proxy.c:1.1.2.9

Guido van Rossum guido@python.org
Thu, 18 Apr 2002 15:45:17 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/Security
In directory cvs.zope.org:/tmp/cvs-serv26218

Modified Files:
      Tag: SecurityProxy-branch
	_Proxy.c 
Log Message:
The 'checked' argument to the checker.proxy() method is no more.

=== Zope3/lib/python/Zope/Security/_Proxy.c 1.1.2.8 => 1.1.2.9 ===
 typedef PyObject *(*function1)(PyObject *);
 
+static int
+check(PyObject *checker, char *check_method, PyObject *object)
+{
+	PyObject *checked;
+
+	checked = PyObject_CallMethod(checker, check_method, "(O)", object);
+	if (checked == NULL)
+		return 0;
+	Py_DECREF(checked);
+	return 1;
+}
+
+static int
+checkName(PyObject *checker, char *check_method,
+	  PyObject *object, PyObject *name)
+{
+	PyObject *checked;
+
+	checked = PyObject_CallMethod(checker, check_method, "(OO)",
+				      object, name);
+	if (checked == NULL)
+		return 0;
+	Py_DECREF(checked);
+	return 1;
+}
+
 static PyObject *
 check1(ProxyObject *self, char *check_method, function1 operation)
 {
 	PyObject *result = NULL;
 	PyObject *object = self->proxy_object;
 	PyObject *checker = self->proxy_checker;
-	PyObject *checked, *value;
+	PyObject *value;
 
 	/*
-	 * checked = checker.check_method(object)
+	 * checker.check_method(object)
 	 * value = operation(object)
-	 * return checker.proxy(value, checked)
+	 * return checker.proxy(value)
 	 */
-	checked = PyObject_CallMethod(checker, check_method, "(O)", object);
-	if (checked != NULL) {
+	if (check(checker, check_method, object)) {
 		value = operation(object);
 		if (value != NULL) {
-			result = PyObject_CallMethod(
-				checker, "proxy", "(OO)", value, checked);
+			result = PyObject_CallMethod(checker, "proxy",
+						     "(O)", value);
 			Py_DECREF(value);
 		}
-		Py_DECREF(checked);
 	}
 	return result;
 }
@@ -103,12 +127,25 @@
 static PyObject *
 proxy_richcompare(PyObject* self, PyObject* other, int op)
 {
-	/* XXX check_richcompare */
-	if (Proxy_Check(self))
-		self = Proxy_GetObject(self);
-	if (Proxy_Check(other))
-		other = Proxy_GetObject(other);
-	return PyObject_RichCompare(self, other, op);
+	PyObject *result = NULL;
+	PyObject *object = Proxy_GetObject(self);
+	PyObject *checker = Proxy_GetChecker(self);
+	PyObject *value;
+
+	/*
+	 * checker.check_richcompare(object)
+	 * value = object <op> other
+	 * return checker.proxy(value)
+	 */
+	if (check(checker, "check_richcompare", object)) {
+		value = PyObject_RichCompare(object, other, op);
+		if (value != NULL) {
+			result = PyObject_CallMethod(
+				checker, "proxy", "(N)", value);
+			/*Py_DECREF(value);*/
+		}
+	}
+	return result;
 }
 
 static PyObject *
@@ -124,23 +161,20 @@
 	PyObject *result = NULL;
 	PyObject *object = Proxy_GetObject(self);
 	PyObject *checker = Proxy_GetChecker(self);
-	PyObject *checked, *value;
+	PyObject *value;
 
 	/*
-	 * checked = checker.check_getattr(object, name)
+	 * checker.check_getattr(object, name)
 	 * value = getattr(object, name)
-	 * return checker.proxy(value, checked)
+	 * return checker.proxy(value)
 	 */
-	checked = PyObject_CallMethod(
-		checker, "check_getattr", "(OO)", object, name);
-	if (checked != NULL) {
+	if (checkName(checker, "check_getattr", object, name)) {
 		value = PyObject_GetAttr(object, name);
 		if (value != NULL) {
 			result = PyObject_CallMethod(
-				checker, "proxy", "(OO)", value, checked);
+				checker, "proxy", "(O)", value);
 			Py_DECREF(value);
 		}
-		Py_DECREF(checked);
 	}
 	return result;
 }
@@ -150,18 +184,14 @@
 {
 	PyObject *object = Proxy_GetObject(self);
 	PyObject *checker = Proxy_GetChecker(self);
-	PyObject *checked;
 
 	/*
-	 * checked = checker.check_setattr(object, name)
+	 * checker.check_setattr(object, name)
 	 * setattr(object, name, value)
 	 */
-	checked = PyObject_CallMethod(
-		checker, "check_setattr", "(OO)", object, name);
-	if (checked == NULL)
-		return -1;
-	Py_DECREF(checked);
-	return PyObject_SetAttr(object, name, value);
+	if (checkName(checker, "check_setattr", object, name))
+		return PyObject_SetAttr(object, name, value);
+	return -1;
 }
 
 static PyObject *
@@ -196,22 +226,20 @@
 	PyObject *result = NULL;
 	PyObject *object = Proxy_GetObject(self);
 	PyObject *checker = Proxy_GetChecker(self);
-	PyObject *checked, *value;
+	PyObject *value;
 
 	/*
-	 * checked = checker.check_call(object)
+	 * checker.check_call(object)
 	 * value = object(*args, **kwds)
-	 * return checker.proxy(value, checked)
+	 * return checker.proxy(value)
 	 */
-	checked = PyObject_CallMethod(checker, "check_call", "(O)", object);
-	if (checked != NULL) {
+	if (check(checker, "check_call", object)) {
 		value = PyObject_Call(object, args, kwds);
 		if (value != NULL) {
 			result = PyObject_CallMethod(
-				checker, "proxy", "(OO)", value, checked);
+				checker, "proxy", "(O)", value);
 			Py_DECREF(value);
 		}
-		Py_DECREF(checked);
 	}
 	return result;
 }
@@ -272,23 +300,20 @@
 	PyObject *result = NULL;
 	PyObject *object = Proxy_GetObject(self);
 	PyObject *checker = Proxy_GetChecker(self);
-	PyObject *checked, *value;
+	PyObject *value;
 
 	/*
-	 * checked = checker.check_getitem(object, key)
+	 * checker.check_getitem(object, key)
 	 * value = object[key]
-	 * return checker.proxy(value, checked)
+	 * return checker.proxy(value)
 	 */
-	checked = PyObject_CallMethod(
-		checker, "check_getitem", "(O)", object);
-	if (checked != NULL) {
+	if (check(checker, "check_getitem", object)) {
 		value = PyObject_GetItem(object, key);
 		if (value != NULL) {
 			result = PyObject_CallMethod(
-				checker, "proxy", "(OO)", value, checked);
+				checker, "proxy", "(O)", value);
 			Py_DECREF(value);
 		}
-		Py_DECREF(checked);
 	}
 	return result;
 }
@@ -298,18 +323,14 @@
 {
 	PyObject *object = Proxy_GetObject(self);
 	PyObject *checker = Proxy_GetChecker(self);
-	PyObject *checked;
 
 	/*
-	 * checked = checker.check_setitem(object, key)
+	 * checker.check_setitem(object, key)
 	 * object[key] = value
 	 */
-	checked = PyObject_CallMethod(
-		checker, "check_setitem", "(O)", object);
-	if (checked == NULL)
-		return -1;
-	Py_DECREF(checked);
-	return PyObject_SetItem(object, key, value);
+	if (check(checker, "check_setitem", object))
+		return PyObject_SetItem(object, key, value);
+	return -1;
 }
 
 /*