Commit 620e5d16 authored by Sidnei da Silva's avatar Sidnei da Silva

- Add guards for 'all' and 'any', new in Python 2.5. Add support for 'key'...

 - Add guards for 'all' and 'any', new in Python 2.5. Add support for 'key' keyword argument to 'max' and 'min' guards
parent 247a385f
...@@ -237,18 +237,18 @@ def guarded_reduce(f, seq, initial=_marker): ...@@ -237,18 +237,18 @@ def guarded_reduce(f, seq, initial=_marker):
return reduce(f, guarded_iter(seq), initial) return reduce(f, guarded_iter(seq), initial)
safe_builtins['reduce'] = guarded_reduce safe_builtins['reduce'] = guarded_reduce
def guarded_max(item, *items): def guarded_max(item, *items, **kw):
if items: if items:
item = [item] item = [item]
item.extend(items) item.extend(items)
return max(guarded_iter(item)) return max(guarded_iter(item), **kw)
safe_builtins['max'] = guarded_max safe_builtins['max'] = guarded_max
def guarded_min(item, *items): def guarded_min(item, *items, **kw):
if items: if items:
item = [item] item = [item]
item.extend(items) item.extend(items)
return min(guarded_iter(item)) return min(guarded_iter(item), **kw)
safe_builtins['min'] = guarded_min safe_builtins['min'] = guarded_min
def guarded_map(f, *seqs): def guarded_map(f, *seqs):
...@@ -366,6 +366,17 @@ def builtin_guarded_apply(func, args=(), kws={}): ...@@ -366,6 +366,17 @@ def builtin_guarded_apply(func, args=(), kws={}):
safe_builtins['apply'] = builtin_guarded_apply 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 # This metaclass supplies the security declarations that allow all
# attributes of a class and its instances to be read and written. # attributes of a class and its instances to be read and written.
def _metaclass(name, bases, dict): def _metaclass(name, bases, dict):
......
...@@ -163,3 +163,14 @@ def f11(): ...@@ -163,3 +163,14 @@ def f11():
x += 1 x += 1
f11() 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()
...@@ -19,6 +19,7 @@ $Id$ ...@@ -19,6 +19,7 @@ $Id$
""" """
import os, sys import os, sys
import operator
import unittest import unittest
from zope.testing import doctest from zope.testing import doctest
import ZODB import ZODB
...@@ -30,6 +31,9 @@ from AccessControl.ZopeGuards \ ...@@ -30,6 +31,9 @@ from AccessControl.ZopeGuards \
get_iter, guarded_min, guarded_max, safe_builtins, guarded_enumerate, \ get_iter, guarded_min, guarded_max, safe_builtins, guarded_enumerate, \
guarded_sum, guarded_apply guarded_sum, guarded_apply
if sys.version_info >= (2, 5):
from AccessControl.ZopeGuards import guarded_any, guarded_all
try: try:
__file__ __file__
except NameError: except NameError:
...@@ -236,11 +240,26 @@ class TestListGuards(GuardTestCase): ...@@ -236,11 +240,26 @@ class TestListGuards(GuardTestCase):
class TestBuiltinFunctionGuards(GuardTestCase): 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): def test_min_fails(self):
sm = SecurityManager(1) # rejects sm = SecurityManager(1) # rejects
old = self.setSecurityManager(sm) 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, 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) self.setSecurityManager(old)
def test_max_fails(self): def test_max_fails(self):
...@@ -248,6 +267,8 @@ class TestBuiltinFunctionGuards(GuardTestCase): ...@@ -248,6 +267,8 @@ class TestBuiltinFunctionGuards(GuardTestCase):
old = self.setSecurityManager(sm) 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, 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) self.setSecurityManager(old)
def test_enumerate_fails(self): def test_enumerate_fails(self):
...@@ -263,11 +284,26 @@ class TestBuiltinFunctionGuards(GuardTestCase): ...@@ -263,11 +284,26 @@ class TestBuiltinFunctionGuards(GuardTestCase):
self.assertRaises(Unauthorized, guarded_sum, [1,2,3]) self.assertRaises(Unauthorized, guarded_sum, [1,2,3])
self.setSecurityManager(old) 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): def test_min_succeeds(self):
sm = SecurityManager() # accepts sm = SecurityManager() # accepts
old = self.setSecurityManager(sm) old = self.setSecurityManager(sm)
self.assertEqual(guarded_min([1,2,3]), 1) self.assertEqual(guarded_min([1,2,3]), 1)
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) self.setSecurityManager(old)
def test_max_succeeds(self): def test_max_succeeds(self):
...@@ -275,6 +311,8 @@ class TestBuiltinFunctionGuards(GuardTestCase): ...@@ -275,6 +311,8 @@ class TestBuiltinFunctionGuards(GuardTestCase):
old = self.setSecurityManager(sm) old = self.setSecurityManager(sm)
self.assertEqual(guarded_max([1,2,3]), 3) self.assertEqual(guarded_max([1,2,3]), 3)
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) self.setSecurityManager(old)
def test_enumerate_succeeds(self): def test_enumerate_succeeds(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment