[Zope3-checkins] SVN: Zope3/trunk/src/zope/security/ We had a policy that hash, non-zero and comparison operators should

Jim Fulton jim at zope.com
Sun Apr 30 09:56:46 EDT 2006


Log message for revision 67762:
  We had a policy that hash, non-zero and comparison operators should
  always be allowed.  I moved this policy into the proxy, providing
  about a 1% speedup.  (Doesn't sound like much, but 1% here, 1% there, 
  pretty soon you've got 2%. ;)
  

Changed:
  U   Zope3/trunk/src/zope/security/_proxy.c
  U   Zope3/trunk/src/zope/security/tests/test_proxy.py

-=-
Modified: Zope3/trunk/src/zope/security/_proxy.c
===================================================================
--- Zope3/trunk/src/zope/security/_proxy.c	2006-04-30 13:56:44 UTC (rev 67761)
+++ Zope3/trunk/src/zope/security/_proxy.c	2006-04-30 13:56:45 UTC (rev 67762)
@@ -309,20 +309,15 @@
   return 0;
 }
 
-
-/* Map rich comparison operators to their __xx__ namesakes */
-static PyObject *name_op[6];
-
 static PyObject *
 proxy_richcompare(SecurityProxy* self, PyObject* other, int op)
 {
   PyObject *result = NULL;
 
-  if (check(self, str_check, name_op[op]) >= 0) 
-    {
-      result = PyObject_RichCompare(self->proxy.proxy_object, other, op);
-      PROXY_RESULT(self, result);
-    } 
+  result = PyObject_RichCompare(self->proxy.proxy_object, other, op);
+  if (result == Py_True || result == Py_False)
+    return result;
+  PROXY_RESULT(self, result);
   return result;
 }
 
@@ -447,17 +442,13 @@
 static int
 proxy_compare(SecurityProxy *self, PyObject *other)
 {
-  if (check(self, str_check, str___cmp__) >= 0)
-    return PyObject_Compare(self->proxy.proxy_object, other);
-  return -1;
+  return PyObject_Compare(self->proxy.proxy_object, other);
 }
 
 static long
 proxy_hash(SecurityProxy *self)
 {
-  if (check(self, str_check, str___hash__) >= 0)
-    return PyObject_Hash(self->proxy.proxy_object);
-  return -1;
+  return PyObject_Hash(self->proxy.proxy_object);
 }
 
 static PyObject *
@@ -609,9 +600,7 @@
 static int
 proxy_nonzero(PyObject *self)
 {
-  if (check(((SecurityProxy*)self), str_check, str___nonzero__) >= 0)
-    return PyObject_IsTrue(((SecurityProxy*)self)->proxy.proxy_object);
-  return -1;
+  return PyObject_IsTrue(((SecurityProxy*)self)->proxy.proxy_object);
 }
 
 UNOP(invert, PyNumber_Invert)
@@ -918,13 +907,6 @@
   if (Proxy_Import() < 0)
     return;
 
-  name_op[0] = PyString_FromString("__lt__");
-  name_op[1] = PyString_FromString("__le__");
-  name_op[2] = PyString_FromString("__eq__");
-  name_op[3] = PyString_FromString("__ne__");
-  name_op[4] = PyString_FromString("__gt__");
-  name_op[5] = PyString_FromString("__ge__");
-
 #define INIT_STRING(S) \
 if((str_##S = PyString_InternFromString(#S)) == NULL) return
 #define INIT_STRING_OP(S) \

Modified: Zope3/trunk/src/zope/security/tests/test_proxy.py
===================================================================
--- Zope3/trunk/src/zope/security/tests/test_proxy.py	2006-04-30 13:56:44 UTC (rev 67761)
+++ Zope3/trunk/src/zope/security/tests/test_proxy.py	2006-04-30 13:56:45 UTC (rev 67762)
@@ -165,8 +165,8 @@
     def testRichCompareOK(self):
         self.failUnless(self.p == self.x)
 
-    def testRichCompareFail(self):
-        self.shouldFail(lambda: self.p == self.x)
+##     def testRichCompareFail(self):
+##         self.shouldFail(lambda: self.p == self.x)
 
     def testIterOK(self):
         self.assertEqual(removeSecurityProxy(iter(self.p)), self.x)
@@ -183,20 +183,20 @@
     def testCompareOK(self):
         self.assertEqual(cmp(self.p, self.x), 0)
 
-    def testCompareFail(self):
-        self.shouldFail(cmp, self.p, self.x)
+##     def testCompareFail(self):
+##         self.shouldFail(cmp, self.p, self.x)
 
     def testHashOK(self):
         self.assertEqual(hash(self.p), hash(self.x))
 
-    def testHashFail(self):
-        self.shouldFail(hash, self.p)
+##     def testHashFail(self):
+##         self.shouldFail(hash, self.p)
 
     def testNonzeroOK(self):
         self.assertEqual(not self.p, 0)
 
-    def testNonzeroFail(self):
-        self.shouldFail(lambda: not self.p)
+##     def testNonzeroFail(self):
+##         self.shouldFail(lambda: not self.p)
 
     def testLenOK(self):
         self.assertEqual(len(self.p), 42)
@@ -267,7 +267,10 @@
     def test_odd_unops(self):
         # unops that don't return a proxy
         P = self.c.proxy
-        for func in hex, oct, lambda x: not x:
+        for func in (
+            hex, oct,
+            # lambda x: not x,
+            ):
             self.assertEqual(func(P(100)), func(100))
             self.shouldFail(func, P(100))
 



More information about the Zope3-Checkins mailing list