[Zope3-checkins] SVN: Zope3/branches/3.3/ Fixed issue 564, http://www.zope.org/Collectors/Zope3-dev/564

Jim Fulton jim at zope.com
Sun Jul 16 13:10:19 EDT 2006


Log message for revision 69148:
  Fixed issue 564, http://www.zope.org/Collectors/Zope3-dev/564
  
  Binary set operations (&, |, ^, -) weren't avaliable for proxied sets.
  
  Also added tests for set checkers.
  

Changed:
  U   Zope3/branches/3.3/doc/CHANGES.txt
  U   Zope3/branches/3.3/src/zope/security/checker.py
  A   Zope3/branches/3.3/src/zope/security/tests/test_set_checkers.py

-=-
Modified: Zope3/branches/3.3/doc/CHANGES.txt
===================================================================
--- Zope3/branches/3.3/doc/CHANGES.txt	2006-07-16 15:18:09 UTC (rev 69147)
+++ Zope3/branches/3.3/doc/CHANGES.txt	2006-07-16 17:10:19 UTC (rev 69148)
@@ -10,6 +10,8 @@
 
     Bugfixes
 
+      - Fixed issue 564: binary set operations didn't work on proxied sets.
+
       - Fixed issue 572: Dict fields with key_type or value_type set
         didn't handle binding correctly.
       

Modified: Zope3/branches/3.3/src/zope/security/checker.py
===================================================================
--- Zope3/branches/3.3/src/zope/security/checker.py	2006-07-16 15:18:09 UTC (rev 69147)
+++ Zope3/branches/3.3/src/zope/security/checker.py	2006-07-16 17:10:19 UTC (rev 69148)
@@ -578,6 +578,7 @@
                             'copy', 'difference', 'intersection', 'issubset',
                             'issuperset', 'symmetric_difference', 'union',
                             '__and__', '__or__', '__sub__', '__xor__',
+                            '__rand__', '__ror__', '__rsub__', '__rxor__',
                             '__eq__', '__ne__', '__lt__', '__gt__',
                             '__le__', '__ge__'])
 
@@ -664,6 +665,8 @@
                         '__add__', '__radd__', ]),
     sets.Set: _setChecker,
     sets.ImmutableSet: _setChecker,
+    set: _setChecker,
+    frozenset: _setChecker,
 
     # YAGNI: () a rock
     tuple: NamesChecker(['__getitem__', '__getslice__', '__add__', '__radd__',
@@ -704,14 +707,6 @@
     zope.interface.declarations.Declaration: _Declaration_checker,
 }
 
-# If we are running with Python 2.4+, setup security on the builtin
-# set and frozenset types.
-try:
-    _default_checkers[set] = _setChecker
-    _default_checkers[frozenset] = _setChecker
-except NameError:
-    pass
-
 def _clear():
     _checkers.clear()
     _checkers.update(_default_checkers)

Copied: Zope3/branches/3.3/src/zope/security/tests/test_set_checkers.py (from rev 69141, Zope3/branches/3.3/src/zope/security/tests/test_standard_checkers.py)
===================================================================
--- Zope3/branches/3.3/src/zope/security/tests/test_standard_checkers.py	2006-07-15 19:48:34 UTC (rev 69141)
+++ Zope3/branches/3.3/src/zope/security/tests/test_set_checkers.py	2006-07-16 17:10:19 UTC (rev 69148)
@@ -0,0 +1,219 @@
+##############################################################################
+#
+# Copyright (c) 2003 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Test checkers for standard types
+
+This is a test of the assertions made in
+zope.security.checkers._default_checkers.
+
+$Id$
+"""
+import unittest
+from zope.testing.doctestunit import DocTestSuite
+from zope.security.checker import ProxyFactory
+from zope.security.interfaces import ForbiddenAttribute
+import sets
+
+def check_forbidden_get(object, attr):
+    try:
+        return getattr(object, attr)
+    except ForbiddenAttribute, e:
+        return 'ForbiddenAttribute: %s' % e[0]
+
+def test_set():
+    """Test that we can do everything we expect to be able to do
+
+    with proxied sets.
+
+    >>> us = set((1, 2))
+    >>> s = ProxyFactory(us)
+
+    >>> check_forbidden_get(s, 'add') # Verify that we are protected
+    'ForbiddenAttribute: add'
+    >>> check_forbidden_get(s, 'remove') # Verify that we are protected
+    'ForbiddenAttribute: remove'
+    >>> check_forbidden_get(s, 'discard') # Verify that we are protected
+    'ForbiddenAttribute: discard'
+    >>> check_forbidden_get(s, 'pop') # Verify that we are protected
+    'ForbiddenAttribute: pop'
+    >>> check_forbidden_get(s, 'clear') # Verify that we are protected
+    'ForbiddenAttribute: clear'
+
+    >>> len(s)
+    2
+
+    >>> 1 in s
+    True
+
+    >>> 1 not in s
+    False
+
+    >>> s.issubset(set((1,2,3)))
+    True
+
+    >>> s.issuperset(set((1,2,3)))
+    False
+
+    >>> c = s.union(set((2, 3)))
+    >>> sorted(c)
+    [1, 2, 3]
+    >>> check_forbidden_get(c, 'add')
+    'ForbiddenAttribute: add'
+
+    >>> c = s | set((2, 3))
+    >>> sorted(c)
+    [1, 2, 3]
+    >>> check_forbidden_get(c, 'add')
+    'ForbiddenAttribute: add'
+
+    >>> c = s | ProxyFactory(set((2, 3)))
+    >>> sorted(c)
+    [1, 2, 3]
+    >>> check_forbidden_get(c, 'add')
+    'ForbiddenAttribute: add'
+
+    >>> c = set((2, 3)) | s
+    >>> sorted(c)
+    [1, 2, 3]
+    >>> check_forbidden_get(c, 'add')
+    'ForbiddenAttribute: add'
+
+    >>> c = s.intersection(set((2, 3)))
+    >>> sorted(c)
+    [2]
+    >>> check_forbidden_get(c, 'add')
+    'ForbiddenAttribute: add'
+
+    >>> c = s & set((2, 3))
+    >>> sorted(c)
+    [2]
+    >>> check_forbidden_get(c, 'add')
+    'ForbiddenAttribute: add'
+
+    >>> c = s & ProxyFactory(set((2, 3)))
+    >>> sorted(c)
+    [2]
+    >>> check_forbidden_get(c, 'add')
+    'ForbiddenAttribute: add'
+
+    >>> c = set((2, 3)) & s
+    >>> sorted(c)
+    [2]
+    >>> check_forbidden_get(c, 'add')
+    'ForbiddenAttribute: add'
+
+    >>> c = s.difference(set((2, 3)))
+    >>> sorted(c)
+    [1]
+    >>> check_forbidden_get(c, 'add')
+    'ForbiddenAttribute: add'
+
+    >>> c = s - ProxyFactory(set((2, 3)))
+    >>> sorted(c)
+    [1]
+    >>> check_forbidden_get(c, 'add')
+    'ForbiddenAttribute: add'
+
+    >>> c = s - set((2, 3))
+    >>> sorted(c)
+    [1]
+    >>> check_forbidden_get(c, 'add')
+    'ForbiddenAttribute: add'
+
+    >>> c = set((2, 3)) - s
+    >>> sorted(c)
+    [3]
+    >>> check_forbidden_get(c, 'add')
+    'ForbiddenAttribute: add'
+
+    >>> c = s.symmetric_difference(set((2, 3)))
+    >>> sorted(c)
+    [1, 3]
+    >>> check_forbidden_get(c, 'add')
+    'ForbiddenAttribute: add'
+
+    >>> c = s ^ set((2, 3))
+    >>> sorted(c)
+    [1, 3]
+    >>> check_forbidden_get(c, 'add')
+    'ForbiddenAttribute: add'
+
+    >>> c = s ^ ProxyFactory(set((2, 3)))
+    >>> sorted(c)
+    [1, 3]
+    >>> check_forbidden_get(c, 'add')
+    'ForbiddenAttribute: add'
+
+    >>> c = set((2, 3)) ^ s
+    >>> sorted(c)
+    [1, 3]
+    >>> check_forbidden_get(c, 'add')
+    'ForbiddenAttribute: add'
+
+    >>> c = s.copy()
+    >>> sorted(c)
+    [1, 2]
+    >>> check_forbidden_get(c, 'add')
+    'ForbiddenAttribute: add'
+
+    >>> str(s) == str(us)
+    True
+    
+    >>> repr(s) == repr(us)
+    True
+
+    Always available:
+
+    >>> s < us
+    False
+    >>> s > us
+    False
+    >>> s <= us
+    True
+    >>> s >= us
+    True
+    >>> s == us
+    True
+    >>> s != us
+    False
+
+    Note that you can't compare proxied sets with other proxied sets
+    due a limitaion in the set comparison functions which won't work
+    with any kind of proxy.
+    
+    >>> bool(s)
+    True
+    >>> s.__class__ == set
+    True
+    """
+
+def setUpFrozenSet(test):
+    test.globs['set'] = frozenset
+
+def setUpSet(test):
+    test.globs['set'] = sets.Set
+
+def setUpImmutableSet(test):
+    test.globs['set'] = sets.ImmutableSet
+
+def test_suite():
+    return unittest.TestSuite((
+        DocTestSuite(),
+        DocTestSuite(setUp=setUpFrozenSet),
+        DocTestSuite(setUp=setUpSet),
+        DocTestSuite(setUp=setUpImmutableSet),
+        ))
+
+if __name__ == '__main__':
+    import unittest
+    unittest.main()



More information about the Zope3-Checkins mailing list