[Zope-Checkins] SVN: Zope/branches/gsoc-python-2.5/lib/python/AccessControl/ - Add guards for 'all' and 'any', new in Python 2.5. Add support for 'key' keyword argument to 'max' and 'min' guards

Sidnei da Silva sidnei at enfoldsystems.com
Mon Oct 13 15:28:35 EDT 2008


Log message for revision 92151:
   - Add guards for 'all' and 'any', new in Python 2.5. Add support for 'key' keyword argument to 'max' and 'min' guards

Changed:
  U   Zope/branches/gsoc-python-2.5/lib/python/AccessControl/ZopeGuards.py
  U   Zope/branches/gsoc-python-2.5/lib/python/AccessControl/tests/actual_python.py
  U   Zope/branches/gsoc-python-2.5/lib/python/AccessControl/tests/testZopeGuards.py

-=-
Modified: Zope/branches/gsoc-python-2.5/lib/python/AccessControl/ZopeGuards.py
===================================================================
--- Zope/branches/gsoc-python-2.5/lib/python/AccessControl/ZopeGuards.py	2008-10-13 19:27:54 UTC (rev 92150)
+++ Zope/branches/gsoc-python-2.5/lib/python/AccessControl/ZopeGuards.py	2008-10-13 19:28:34 UTC (rev 92151)
@@ -237,18 +237,18 @@
         return reduce(f, guarded_iter(seq), initial)
 safe_builtins['reduce'] = guarded_reduce
 
-def guarded_max(item, *items):
+def guarded_max(item, *items, **kw):
     if items:
         item = [item]
         item.extend(items)
-    return max(guarded_iter(item))
+    return max(guarded_iter(item), **kw)
 safe_builtins['max'] = guarded_max
 
-def guarded_min(item, *items):
+def guarded_min(item, *items, **kw):
     if items:
         item = [item]
         item.extend(items)
-    return min(guarded_iter(item))
+    return min(guarded_iter(item), **kw)
 safe_builtins['min'] = guarded_min
 
 def guarded_map(f, *seqs):
@@ -366,6 +366,17 @@
 
 safe_builtins['apply'] = builtin_guarded_apply
 
+# Similar to min and reduce, use guarded_iter on the sequence being
+# tested and apply the original function.
+if sys.version_info >= (2, 5):
+    def guarded_any(seq):
+        return any(guarded_iter(seq))
+    safe_builtins['any'] = guarded_any
+
+    def guarded_all(seq):
+        return all(guarded_iter(seq))
+    safe_builtins['all'] = guarded_all
+
 # This metaclass supplies the security declarations that allow all
 # attributes of a class and its instances to be read and written.
 def _metaclass(name, bases, dict):

Modified: Zope/branches/gsoc-python-2.5/lib/python/AccessControl/tests/actual_python.py
===================================================================
--- Zope/branches/gsoc-python-2.5/lib/python/AccessControl/tests/actual_python.py	2008-10-13 19:27:54 UTC (rev 92150)
+++ Zope/branches/gsoc-python-2.5/lib/python/AccessControl/tests/actual_python.py	2008-10-13 19:28:34 UTC (rev 92151)
@@ -163,3 +163,14 @@
     x += 1
 f11()
 
+def f12():
+    assert all([True, True, True]) == True
+    assert all([True, False, True]) == False
+f12()
+
+def f13():
+    assert any([True, True, True]) == True
+    assert any([True, False, True]) == True
+    assert any([False, False, False]) == False
+f13()
+

Modified: Zope/branches/gsoc-python-2.5/lib/python/AccessControl/tests/testZopeGuards.py
===================================================================
--- Zope/branches/gsoc-python-2.5/lib/python/AccessControl/tests/testZopeGuards.py	2008-10-13 19:27:54 UTC (rev 92150)
+++ Zope/branches/gsoc-python-2.5/lib/python/AccessControl/tests/testZopeGuards.py	2008-10-13 19:28:34 UTC (rev 92151)
@@ -19,6 +19,7 @@
 """
 
 import os, sys
+import operator
 import unittest
 from zope.testing import doctest
 import ZODB
@@ -30,6 +31,9 @@
     get_iter, guarded_min, guarded_max, safe_builtins, guarded_enumerate, \
     guarded_sum, guarded_apply
 
+if sys.version_info >= (2, 5):
+    from AccessControl.ZopeGuards import guarded_any, guarded_all
+
 try:
     __file__
 except NameError:
@@ -236,11 +240,26 @@
 
 class TestBuiltinFunctionGuards(GuardTestCase):
 
+    if sys.version_info >= (2, 5):
+        def test_all_fails(self):
+            sm = SecurityManager(1) # rejects
+            old = self.setSecurityManager(sm)
+            self.assertRaises(Unauthorized, guarded_all, [True,True,False])
+            self.setSecurityManager(old)
+
+        def test_any_fails(self):
+            sm = SecurityManager(1) # rejects
+            old = self.setSecurityManager(sm)
+            self.assertRaises(Unauthorized, guarded_any, [True,True,False])
+            self.setSecurityManager(old)
+
     def test_min_fails(self):
         sm = SecurityManager(1) # rejects
         old = self.setSecurityManager(sm)
         self.assertRaises(Unauthorized, guarded_min, [1,2,3])
         self.assertRaises(Unauthorized, guarded_min, 1,2,3)
+        self.assertRaises(Unauthorized, guarded_min,
+                          [{'x':1},{'x':2}], operator.itemgetter('x'))
         self.setSecurityManager(old)
 
     def test_max_fails(self):
@@ -248,6 +267,8 @@
         old = self.setSecurityManager(sm)
         self.assertRaises(Unauthorized, guarded_max, [1,2,3])
         self.assertRaises(Unauthorized, guarded_max, 1,2,3)
+        self.assertRaises(Unauthorized, guarded_max, 
+                          [{'x':1},{'x':2}], operator.itemgetter('x'))
         self.setSecurityManager(old)
 
     def test_enumerate_fails(self):
@@ -263,11 +284,26 @@
         self.assertRaises(Unauthorized, guarded_sum, [1,2,3])
         self.setSecurityManager(old)
 
+    if sys.version_info >= (2, 5):
+        def test_all_succeeds(self):
+            sm = SecurityManager() # accepts
+            old = self.setSecurityManager(sm)
+            self.assertEqual(guarded_all([True,True,False]), False)
+            self.setSecurityManager(old)
+
+        def test_any_succeeds(self):
+            sm = SecurityManager() # accepts
+            old = self.setSecurityManager(sm)
+            self.assertEquals(guarded_any([True,True,False]), True)
+            self.setSecurityManager(old)
+
     def test_min_succeeds(self):
         sm = SecurityManager() # accepts
         old = self.setSecurityManager(sm)
         self.assertEqual(guarded_min([1,2,3]), 1)
         self.assertEqual(guarded_min(1,2,3), 1)
+        self.assertEqual(guarded_min({'x':1},{'x':2}, 
+                                     key=operator.itemgetter('x')), {'x':1})
         self.setSecurityManager(old)
 
     def test_max_succeeds(self):
@@ -275,6 +311,8 @@
         old = self.setSecurityManager(sm)
         self.assertEqual(guarded_max([1,2,3]), 3)
         self.assertEqual(guarded_max(1,2,3), 3)
+        self.assertEqual(guarded_max({'x':1},{'x':2}, 
+                                     key=operator.itemgetter('x')), {'x':2})
         self.setSecurityManager(old)
 
     def test_enumerate_succeeds(self):



More information about the Zope-Checkins mailing list