[Zope-Checkins] CVS: Zope/lib/python/AccessControl - cAccessControl.c:1.18

Chris McDonough chrism@zope.com
Mon, 16 Dec 2002 14:13:01 -0500


Update of /cvs-repository/Zope/lib/python/AccessControl
In directory cvs.zope.org:/tmp/cvs-serv24600

Modified Files:
	cAccessControl.c 
Log Message:
Support getattr on PermissionRole instances to allow access to _p, _d, __name__m and __roles__ (patch submitted by Dieter Maurer in collector #161).


=== Zope/lib/python/AccessControl/cAccessControl.c 1.17 => 1.18 ===
--- Zope/lib/python/AccessControl/cAccessControl.c:1.17	Tue Jul 23 10:08:55 2002
+++ Zope/lib/python/AccessControl/cAccessControl.c	Mon Dec 16 14:13:00 2002
@@ -343,6 +343,8 @@
 static PyObject *PermissionRole_of(PermissionRole *self, PyObject *args);
 static void PermissionRole_dealloc(PermissionRole *self);
 
+static PyObject *PermissionRole_getattro(PermissionRole *self, PyObject *name);
+
 static PyObject *imPermissionRole_of(imPermissionRole *self, PyObject *args);
 static int imPermissionRole_length(imPermissionRole *self);
 static PyObject *imPermissionRole_get(imPermissionRole *self,
@@ -370,6 +372,10 @@
                                           PyObject *name);
 static int SecurityManager_setattro(SecurityManager *self, 
                                     PyObject *name, PyObject *value);
+
+static getattrofunc ExtensionClassGetattro;
+
+
 /*
 ** Constants
 */
@@ -547,7 +553,7 @@
 	NULL,					/* tp_hash	*/
 	NULL,					/* tp_call	*/
 	NULL,					/* tp_str	*/
-	NULL,					/* tp_getattro	*/
+	(getattrofunc) PermissionRole_getattro,	/* tp_getattro	*/
 	NULL,					/* tp_setattro	*/
 	/* Reserved fields	*/
 	0,					/* tp_xxx3	*/
@@ -1530,6 +1536,42 @@
 	PyMem_DEL(self);  
 }
 
+
+/* for DocFinder */
+/*
+** PermissionRole_getattro
+**
+*/
+
+static PyObject *PermissionRole_getattro(PermissionRole *self, PyObject *name) {
+  	PyObject  *result= NULL;
+  	char      *name_s= PyString_AsString(name);
+
+	/* see whether we know the attribute */
+	/* we support both the old "_d" (from the Python implementation)
+	   and the new "__roles__"
+	*/
+	if (name_s[0] == '_') {
+		if (name_s[1] == '_') {
+			if (strcmp(name_s,"__name__") == 0) 
+                                result= self->__name__;
+			else if (strcmp(name_s,"__roles__") == 0)
+                                result= self->__roles__;
+		}
+		else if (name_s[1] == 'p' && name_s[2] == 0)
+                                result= self->_p;
+		else if (name_s[1] == 'd' && name_s[2] == 0)
+                                result= self->__roles__;
+	}
+	if (result) {
+		Py_INCREF(result);
+		return result;
+	} else {
+		return ExtensionClassGetattro((PyObject *)self,name);
+	}
+}
+
+
 /*
 ** imPermissionRole_of
 **
@@ -2089,8 +2131,7 @@
 	ZopeSecurityPolicyType.tp_getattro =
 		(getattrofunc) PyExtensionClassCAPI->getattro;
 
-	PermissionRoleType.tp_getattro =
-		(getattrofunc) PyExtensionClassCAPI->getattro;
+	ExtensionClassGetattro= PyExtensionClassCAPI->getattro;
 
 	imPermissionRoleType.tp_getattro =
 		(getattrofunc) PyExtensionClassCAPI->getattro;