[Checkins] SVN: five.pt/trunk/ Add all ``RestrictedPython`` builtins to scope.

Malthe Borch mborch at gmail.com
Fri Jul 15 02:25:39 EDT 2011


Log message for revision 122252:
  Add all ``RestrictedPython`` builtins to scope.

Changed:
  U   five.pt/trunk/CHANGES.txt
  U   five.pt/trunk/src/five/pt/patches.py

-=-
Modified: five.pt/trunk/CHANGES.txt
===================================================================
--- five.pt/trunk/CHANGES.txt	2011-07-15 06:12:58 UTC (rev 122251)
+++ five.pt/trunk/CHANGES.txt	2011-07-15 06:25:39 UTC (rev 122252)
@@ -3,7 +3,12 @@
 
 In next release ...
 
-- Added the ``same_type`` function to the provided context.
+- Added builtins from ``RestrictedPython`` to context for Zope 2
+  templates.
+
+  This fixes name error issues on templates that use one or more
+  utility function builtins normally available to Python expressions
+  in restricted mode.
   [malthe]
 
 2.1 (2011-07-14)

Modified: five.pt/trunk/src/five/pt/patches.py
===================================================================
--- five.pt/trunk/src/five/pt/patches.py	2011-07-15 06:12:58 UTC (rev 122251)
+++ five.pt/trunk/src/five/pt/patches.py	2011-07-15 06:25:39 UTC (rev 122252)
@@ -15,6 +15,7 @@
 from AccessControl.SecurityInfo import ClassSecurityInfo
 from App.class_init import InitializeClass
 from Products.PageTemplates.Expressions import getEngine
+from RestrictedPython.Utilities import utility_builtins
 
 from chameleon.tales import StringExpr
 from chameleon.tales import NotExpr
@@ -67,8 +68,10 @@
 
     if engine is getEngine():
         expression_types = _secure_expression_types
+        builtins = utility_builtins
     else:
         expression_types = _expression_types
+        builtins = {}
 
     if filename is None:
         program = ChameleonPageTemplate(
@@ -81,6 +84,8 @@
             expression_types=expression_types,
             encoding='utf-8')
 
+    program._v_builtins = builtins
+
     self._v_program = program
     self._v_macros = program.macros
     self._v_cooked = 1
@@ -97,23 +102,6 @@
         self._v_errors = ()
 
 
-def same_type(arg1, *args):
-    """Compares the class or type of two or more objects. Copied from
-    RestrictedPython.
-    """
-    t = getattr(arg1, '__class__', type(arg1))
-    for arg in args:
-        if getattr(arg, '__class__', type(arg)) is not t:
-            return False
-    return True
-
-
-def test(condition, a, b):
-    if condition:
-        return a
-    return b
-
-
 @staticmethod
 def create_interpreter(cls, *args, **kwargs):
     return ChameleonTALInterpreter(*args, **kwargs)
@@ -140,11 +128,8 @@
             context['wrapped_repeat'] = context['repeat']
             context['repeat'] = RepeatDict(self.repeat)
 
-            # XXX: This could be implemented as a transform which uses
-            # the turnary operator in place of calls to the test
-            # function.
-            context.setdefault('test', test)
-            context.setdefault('same_type', same_type)
+            # Update context with applicable builtins
+            context.update(self.template._v_builtins)
 
             result = self.template.render(**context)
 



More information about the checkins mailing list