[Checkins] SVN: RestrictedPython/branches/davisagli-python27/ add tests for protected iteration for dict and set comprehensions (already works since they use the same ListCompFor AST node as list comps)

David Glick davidglick at onenw.org
Thu Jul 8 00:40:00 EDT 2010


Log message for revision 114297:
  add tests for protected iteration for dict and set comprehensions (already works since they use the same ListCompFor AST node as list comps)

Changed:
  U   RestrictedPython/branches/davisagli-python27/CHANGES.txt
  A   RestrictedPython/branches/davisagli-python27/src/RestrictedPython/tests/before_and_after27.py
  A   RestrictedPython/branches/davisagli-python27/src/RestrictedPython/tests/security_in_syntax27.py
  U   RestrictedPython/branches/davisagli-python27/src/RestrictedPython/tests/testRestrictions.py

-=-
Modified: RestrictedPython/branches/davisagli-python27/CHANGES.txt
===================================================================
--- RestrictedPython/branches/davisagli-python27/CHANGES.txt	2010-07-08 04:37:01 UTC (rev 114296)
+++ RestrictedPython/branches/davisagli-python27/CHANGES.txt	2010-07-08 04:40:00 UTC (rev 114297)
@@ -4,6 +4,8 @@
 3.6.0 (unreleased)
 ------------------
 
+- Added tests for protection of the iterators for dict and set comprehensions
+  in Python 2.7.
 
 3.6.0a1 (2010-06-05)
 --------------------

Added: RestrictedPython/branches/davisagli-python27/src/RestrictedPython/tests/before_and_after27.py
===================================================================
--- RestrictedPython/branches/davisagli-python27/src/RestrictedPython/tests/before_and_after27.py	                        (rev 0)
+++ RestrictedPython/branches/davisagli-python27/src/RestrictedPython/tests/before_and_after27.py	2010-07-08 04:40:00 UTC (rev 114297)
@@ -0,0 +1,45 @@
+##############################################################################
+#
+# Copyright (c) 2010 Zope Foundation 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.
+#
+##############################################################################
+"""Restricted Python transformation examples
+
+This module contains pairs of functions. Each pair has a before and an
+after function.  The after function shows the source code equivalent
+of the before function after it has been modified by the restricted
+compiler.
+
+These examples are actually used in the testRestrictions.py
+checkBeforeAndAfter() unit tests, which verifies that the restricted compiler
+actually produces the same output as would be output by the normal compiler
+for the after function.
+"""
+
+# dictionary and set comprehensions
+
+def simple_dict_comprehension_before():
+    x = {y: y for y in whatever if y}
+
+def simple_dict_comprehension_after():
+    x = {y: y for y in _getiter_(whatever) if y}
+
+def dict_comprehension_attrs_before():
+    x = {y: y.q for y in whatever.z if y.q}
+
+def dict_comprehension_attrs_after():
+    x = {y: _getattr_(y, 'q') for y in _getiter_(_getattr_(whatever, 'z')) if _getattr_(y, 'q')}
+
+def simple_set_comprehension_before():
+    x = {y for y in whatever if y}
+
+def simple_set_comprehension_after():
+    x = {y for y in _getiter_(whatever) if y}

Added: RestrictedPython/branches/davisagli-python27/src/RestrictedPython/tests/security_in_syntax27.py
===================================================================
--- RestrictedPython/branches/davisagli-python27/src/RestrictedPython/tests/security_in_syntax27.py	                        (rev 0)
+++ RestrictedPython/branches/davisagli-python27/src/RestrictedPython/tests/security_in_syntax27.py	2010-07-08 04:40:00 UTC (rev 114297)
@@ -0,0 +1,9 @@
+# These are all supposed to raise a SyntaxError when using
+# compile_restricted() but not when using compile().
+# Each function in this module is compiled using compile_restricted().
+
+def dict_comp_bad_name():
+    {y: y for _restricted_name in x}
+
+def set_comp_bad_name():
+    {y for _restricted_name in x}

Modified: RestrictedPython/branches/davisagli-python27/src/RestrictedPython/tests/testRestrictions.py
===================================================================
--- RestrictedPython/branches/davisagli-python27/src/RestrictedPython/tests/testRestrictions.py	2010-07-08 04:37:01 UTC (rev 114296)
+++ RestrictedPython/branches/davisagli-python27/src/RestrictedPython/tests/testRestrictions.py	2010-07-08 04:40:00 UTC (rev 114297)
@@ -277,6 +277,8 @@
         self._checkSyntaxSecurity('security_in_syntax.py')
         if sys.version_info >= (2, 6):
             self._checkSyntaxSecurity('security_in_syntax26.py')
+        if sys.version_info >= (2, 7):
+            self._checkSyntaxSecurity('security_in_syntax27.py')
 
     def _checkSyntaxSecurity(self, mod_name):
         # Ensures that each of the functions in security_in_syntax.py
@@ -403,6 +405,11 @@
             from RestrictedPython.tests import before_and_after26
             self._checkBeforeAndAfter(before_and_after26)
 
+    if sys.version_info[:2] >= (2, 7):
+        def checkBeforeAndAfter27(self):
+            from RestrictedPython.tests import before_and_after27
+            self._checkBeforeAndAfter(before_and_after27)
+
     def _compile_file(self, name):
         path = os.path.join(_HERE, name)
         f = open(path, "r")



More information about the checkins mailing list