[Zope-Checkins] CVS: Zope2 - TreeTag.py:1.48

shane@digicool.com shane@digicool.com
Thu, 21 Jun 2001 13:45:16 -0400 (EDT)


Update of /cvs-repository/Zope2/lib/python/TreeDisplay
In directory korak.digicool.com:/tmp/cvs-serv24737/lib/python/TreeDisplay

Modified Files:
	TreeTag.py 
Log Message:
Based on some semi-formal performance tests, read guards turned out to be
slower than the old code.  With this change, we're using simple function
calls again to perform security checks.  But the calling sequence is
intended to be easier to comprehend than the old code.  Now instead of
DT_String.String subclasses having a validate() method attached to them, they
subclass AccessControl.DTML.RestrictedDTML, which provides a guarded_getattr()
method and a guarded_getitem() method.

Note that the functionality of guarded_getattr() used to be implemented
both in C and Python (in cDocumentTemplate and DT_Util), but now it's in
one place, ZopeGuards.py.  Thus it's not only reusable but easy to
optimize.

I ran all the tests and ran the new code through the profiler again.  The
change sped up restricted code a little more than expected, which is
definitely a good thing, but that may indicate that nested scopes
have a hidden speed penalty.

Also, RestrictedPython is now restrictive about printing to targets and
two forms of augmented assignment had to be forbidden.



--- Updated File TreeTag.py in package Zope2 --
--- TreeTag.py	2001/05/16 19:07:06	1.47
+++ TreeTag.py	2001/06/21 17:45:14	1.48
@@ -215,11 +215,10 @@
             have_arg=args.has_key
             if have_arg('branches'):
                 def get_items(node, branches=args['branches'], md=md):
-                    read_guard = md.read_guard
-                    if read_guard is None:
-                        items = getattr(node, branches)
-                    else:
-                        items = getattr(read_guard(node), branches)
+                    get = md.guarded_getattr
+                    if get is None:
+                        get = getattr
+                    items = get(node, branches)
                     return items()
             elif have_arg('branches_expr'):
                 def get_items(node, branches_expr=args['branches_expr'], md=md):
@@ -314,15 +313,14 @@
                 break
         if not exp: items=1
 
-    read_guard = md.read_guard
+    get=md.guarded_getattr
+    if get is None:
+        get = getattr
 
     if items is None:
         if have_arg('branches') and hasattr(self, args['branches']):
-            if read_guard is None:
-                items = getattr(self, args['branches'])
-            else:
-                items = getattr(read_guard(self), args['branches'])
-            items=items()
+            items = get(self, args['branches'])
+            items = items()
         elif have_arg('branches_expr'):
             items=args['branches_expr'](md)
 
@@ -330,12 +328,12 @@
 
     if items and items != 1:
 
-        if read_guard is not None:
+        getitem = getattr(md, 'guarded_getitem', None)
+        if getitem is not None:
             unauth=[]
-            guarded_items = read_guard(items)
             for index in range(len(items)):
                 try:
-                    guarded_items[index]
+                    getitem(items, index)
                 except ValidationError:
                     unauth.append(index)
             if unauth: