[Zope-Checkins] CVS: Zope2 - ZopeGuards.py:1.4
shane@digicool.com
shane@digicool.com
Thu, 21 Jun 2001 13:20:28 -0400 (EDT)
Update of /cvs-repository/Zope2/lib/python/AccessControl
In directory korak.digicool.com:/tmp/cvs-serv23938
Modified Files:
ZopeGuards.py
Log Message:
- Added checking of simple container types to guarded_getattr().
This is much faster (according to profiler results) than letting
validate() check for simple types.
- Corrected handling of slice objects passed to guarded_getitem().
- Used guarded_getitem() instead of a read guard in guarded_map().
--- Updated File ZopeGuards.py in package Zope2 --
--- ZopeGuards.py 2001/06/18 16:38:07 1.3
+++ ZopeGuards.py 2001/06/21 17:20:28 1.4
@@ -90,6 +90,7 @@
from RestrictedPython.Utilities import utility_builtins
from SecurityManagement import getSecurityManager
from SecurityInfo import secureModule
+from SimpleObjectPolicies import Containers
Unauthorized = 'Unauthorized'
@@ -110,6 +111,9 @@
if default is not _marker:
return default
raise
+ if Containers(type(inst)):
+ # Simple type. Short circuit.
+ return v
validate = getSecurityManager().validate
# Filter out the objects we can't access.
if hasattr(inst, 'aq_acquire'):
@@ -128,12 +132,26 @@
return 1
safe_builtins['hasattr'] = guarded_hasattr
-slicetype = type(slice(0))
+SliceType = type(slice(0))
def guarded_getitem(object, index):
- if type(object) is slicetype:
- # We don't guard slices
- return object[index.start:index.stop]
+ if type(index) is SliceType:
+ if index.step is not None:
+ v = object[index]
+ else:
+ start = index.start
+ stop = index.stop
+ if start is None:
+ start = 0
+ if stop is None:
+ v = object[start:]
+ else:
+ v = object[start:stop]
+ # We don't guard slices.
+ return v
v = object[index]
+ if Containers(type(object)):
+ # Simple type. Short circuit.
+ return v
if getSecurityManager().validate(object, object, index, v):
return v
raise Unauthorized, 'unauthorized access to element %s' % `i`
@@ -159,11 +177,11 @@
safe_builtins['filter'] = guarded_filter
def guarded_map(f, *seqs):
- for seq in seqs:
- if type(seq) is type(''):
- raise TypeError, 'cannot map a string'
- list(seq) # Ensure that it's a sequence
- return apply(map, tuple([f] + map(full_read_guard, seqs)))
+ safe_seqs = []
+ for seqno in len(seqs):
+ seq = guarded_getitem(f, seqno)
+ safe_seqs.append(seq)
+ return map(f, *safe_seqs)
safe_builtins['map'] = guarded_map
import sys