[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/Security - _protections.py:1.5

Steve Alexander steve@cat-box.net
Sat, 30 Nov 2002 09:49:43 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/Security
In directory cvs.zope.org:/tmp/cvs-serv14971/lib/python/Zope/App/Security

Modified Files:
	_protections.py 
Log Message:
Moved the new expanded BTrees security assertions from Checker.py to
_protections.py, as suggested by Jim.


=== Zope3/lib/python/Zope/App/Security/_protections.py 1.4 => 1.5 ===
--- Zope3/lib/python/Zope/App/Security/_protections.py:1.4	Sat Nov 30 08:11:49 2002
+++ Zope3/lib/python/Zope/App/Security/_protections.py	Sat Nov 30 09:49:42 2002
@@ -19,38 +19,70 @@
 
 def protect():
     from Zope.Security.Checker import \
-         defineChecker, getCheckerForInstancesOf, NamesChecker
-    import Persistence.BTrees
+         defineChecker, getCheckerForInstancesOf, NamesChecker, NoProxy
 
 
-    def _protect(which):
-        __import__('Persistence.BTrees.%sBTree' % which)
-        module = getattr(Persistence.BTrees, "%sBTree" % which)
-        
-        defineChecker(getattr(module, '%sBTree' % which),
-                      getCheckerForInstancesOf(dict))
-        defineChecker(getattr(module, '%sBucket' % which),
-                      getCheckerForInstancesOf(dict))
-        defineChecker(getattr(module, '%sSet' % which),
-                      NamesChecker(['__getitem__', '__len__', 'has_key',
-                                    '__repr__', '__str__', "__contains__",
-                                    'keys', 'maxKey', 'minKey']
-                                   )
-                      )
-        defineChecker(getattr(module, '%sTreeSet' % which),
-                      NamesChecker(['__len__', 'has_key', "__contains__",
-                                   '__repr__', '__str__',
-                                   'keys', 'maxKey', 'minKey']
-                                   )
-                      )
-        items = getattr(module, '%sBTree' % which)().keys()
-        defineChecker(type(items),
-                      getCheckerForInstancesOf(tuple))
-        
+    # excluding _check, _bucket_type, _firstbucket, and write operations
+    _btreeChecker = NamesChecker(['__str__', '__repr__', '__contains__',
+                                  '__getitem__', '__iter__', '__len__',  
+                                  'byValue', 'get', 'has_key', 'items', 
+                                  'iteritems', 'iterkeys', 'itervalues',
+                                  'keys', 'maxKey', 'minKey', 'values'])
 
-# These now conflict with assertions elsewhere.
-##     for which in 'OO', 'II', 'OI', 'IO':
-##         _protect(which)
+    # excluding _next
+    _btreeBucketChecker = NamesChecker([
+            '__contains__', '__getitem__', '__iter__', '__len__', '__repr__',
+            '__str__', 'byValue', 'get', 'has_key', 'items', 'iteritems',
+            'iterkeys', 'itervalues', 'keys', 'maxKey','minKey', 'values'])
+    
+    _btreeSetChecker = NamesChecker([
+            '__contains__', '__getitem__', '__iter__', '__len__', '__repr__',
+            '__str__', 'has_key', 'insert', 'keys', 'maxKey', 'minKey'])
+    
+    # excluding _bucket_type, _check
+    _btreeTreeSetChecker = NamesChecker([
+            '__contains__', '__iter__', '__len__', '__repr__',
+            '__str__', 'has_key', 'insert', 'keys', 'maxKey', 'minKey'])
+    
+    _btreeItemsChecker = NamesChecker([
+            '__iter__', '__repr__', '__str__', '__getitem__', '__len__',
+            '__contains__'])
+    
+    _iteratorChecker = NamesChecker(['next'])
+
+    from Persistence.BTrees.IIBTree import IIBTree, IIBucket, IISet, IITreeSet
+    from Persistence.BTrees.IOBTree import IOBTree, IOBucket, IOSet, IOTreeSet
+    from Persistence.BTrees.OIBTree import OIBTree, OIBucket, OISet, OITreeSet
+    from Persistence.BTrees.OOBTree import OOBTree, OOBucket, OOSet, OOTreeSet
+
+    _btree_checkers = { 
+        IIBTree: _btreeChecker,
+        IOBTree: _btreeChecker,
+        OIBTree: _btreeChecker,
+        OOBTree: _btreeChecker,
+        IIBucket: _btreeBucketChecker,
+        IOBucket: _btreeBucketChecker,
+        OIBucket: _btreeBucketChecker,
+        OOBucket: _btreeBucketChecker,
+        IISet: _btreeSetChecker,
+        IOSet: _btreeSetChecker,
+        OISet: _btreeSetChecker,
+        OOSet: _btreeSetChecker,
+        IITreeSet: _btreeTreeSetChecker,
+        IOTreeSet: _btreeTreeSetChecker,
+        OITreeSet: _btreeTreeSetChecker,
+        OOTreeSet: _btreeTreeSetChecker,
+        type(iter(IIBTree())): NoProxy, # II-iterator is a rock
+        type(iter(IOBTree())): _iteratorChecker, # IO-iterator
+        type(iter(OIBTree())): _iteratorChecker, # OI-iterator
+        type(iter(OOBTree())): _iteratorChecker, # OO-iterator
+        type(IIBTree().keys()): NoProxy, # IIBTreeItems is a rock
+        type(IOBTree().keys()): _btreeItemsChecker, # IOBTreeItems
+        type(OIBTree().keys()): _btreeItemsChecker, # OIBTreeItems
+        type(OOBTree().keys()): _btreeItemsChecker, # OOBTreeItems
+    }
+    for which_type, checker in _btree_checkers.iteritems():
+        defineChecker(which_type, checker)
 
     from Persistence.PersistentList import PersistentList