[Python-checkins] r55658 - python/branches/bcannon-objcap/tests/fail/dangerous_things_inaccessible.py python/branches/bcannon-objcap/tests/fail/sys_inaccessible.py

brett.cannon python-checkins at python.org
Tue May 29 22:22:36 CEST 2007


Author: brett.cannon
Date: Tue May 29 22:22:34 2007
New Revision: 55658

Added:
   python/branches/bcannon-objcap/tests/fail/dangerous_things_inaccessible.py
      - copied, changed from r55655, python/branches/bcannon-objcap/tests/fail/sys_inaccessible.py
Removed:
   python/branches/bcannon-objcap/tests/fail/sys_inaccessible.py
Log:
Change test for 'sys' on required modules for the interpreter to also check for
'open' and 'execfile'.  Rename the test accordingly.


Copied: python/branches/bcannon-objcap/tests/fail/dangerous_things_inaccessible.py (from r55655, python/branches/bcannon-objcap/tests/fail/sys_inaccessible.py)
==============================================================================
--- python/branches/bcannon-objcap/tests/fail/sys_inaccessible.py	(original)
+++ python/branches/bcannon-objcap/tests/fail/dangerous_things_inaccessible.py	Tue May 29 22:22:34 2007
@@ -1,8 +1,14 @@
 """None of the modules required for the interpreter to work should expose the
-'sys' module (nor the modules that they import)."""
+'sys' module, 'open', or 'execfile'.  This holds for the modules that they
+import as well."""
+# Stuff needed to look for sys.
 import encodings
 module_type = type(encodings)
 examined = set()
+# Needed to look for 'open' and 'execfile'.
+builtin_fxn_type = type(any)
+dangerous_builtins = ('open', 'execfile')
+
 def check_imported_modules(module):
     """Recursively check that the module (and the modules it imports) do not
     expose the 'sys' module."""
@@ -10,61 +16,33 @@
     if module.__name__ == 'sys':
         raise Exception
     for attr in module.__dict__.values():
+        # If an object doesn't define __name__ then we don't care about it.
+        try:
+            attr_name = attr.__name__
+        except AttributeError:
+            continue
         if isinstance(attr, module_type) and attr.__name__ not in examined:
-            examined.add(attr.__name__)
+            examined.add(attr_name)
             check_imported_modules(attr)
+        elif isinstance(attr, builtin_fxn_type):
+            if attr_name in dangerous_builtins:
+                raise Exception
+
+
+import __builtin__
+check_imported_modules(__builtin__)
+
+import __main__
+check_imported_modules(__main__)
+
+import exceptions
+check_imported_modules(exceptions)
+
+import encodings
+check_imported_modules(encodings)
 
+import codecs
+check_imported_modules(codecs)
 
-try:
-    import __builtin__
-    __builtin__.sys
-    check_imported_modules(__builtin__)
-except AttributeError:
-    pass
-else:
-    raise Exception
-
-try:
-    import __main__
-    __main__.sys
-    check_imported_modules(__main__)
-except AttributeError:
-    pass
-else:
-    raise Exception
-
-try:
-    import exceptions
-    exceptions.sys
-    check_imported_modules(exceptions)
-except AttributeError:
-    pass
-else:
-    raise Exception
-
-try:
-    import encodings
-    encodings.sys
-    check_imported_modules(encodings)
-except AttributeError:
-    pass
-else:
-    raise Exception
-
-try:
-    import codecs
-    codecs.sys
-    check_imported_modules(codecs)
-except AttributeError:
-    pass
-else:
-    raise Exception
-
-try:
-    import _codecs
-    _codecs.sys
-    check_imported_modules(_codecs)
-except AttributeError:
-    pass
-else:
-    raise Exception
+import _codecs
+check_imported_modules(_codecs)

Deleted: /python/branches/bcannon-objcap/tests/fail/sys_inaccessible.py
==============================================================================
--- /python/branches/bcannon-objcap/tests/fail/sys_inaccessible.py	Tue May 29 22:22:34 2007
+++ (empty file)
@@ -1,70 +0,0 @@
-"""None of the modules required for the interpreter to work should expose the
-'sys' module (nor the modules that they import)."""
-import encodings
-module_type = type(encodings)
-examined = set()
-def check_imported_modules(module):
-    """Recursively check that the module (and the modules it imports) do not
-    expose the 'sys' module."""
-    assert isinstance(module, module_type)
-    if module.__name__ == 'sys':
-        raise Exception
-    for attr in module.__dict__.values():
-        if isinstance(attr, module_type) and attr.__name__ not in examined:
-            examined.add(attr.__name__)
-            check_imported_modules(attr)
-
-
-try:
-    import __builtin__
-    __builtin__.sys
-    check_imported_modules(__builtin__)
-except AttributeError:
-    pass
-else:
-    raise Exception
-
-try:
-    import __main__
-    __main__.sys
-    check_imported_modules(__main__)
-except AttributeError:
-    pass
-else:
-    raise Exception
-
-try:
-    import exceptions
-    exceptions.sys
-    check_imported_modules(exceptions)
-except AttributeError:
-    pass
-else:
-    raise Exception
-
-try:
-    import encodings
-    encodings.sys
-    check_imported_modules(encodings)
-except AttributeError:
-    pass
-else:
-    raise Exception
-
-try:
-    import codecs
-    codecs.sys
-    check_imported_modules(codecs)
-except AttributeError:
-    pass
-else:
-    raise Exception
-
-try:
-    import _codecs
-    _codecs.sys
-    check_imported_modules(_codecs)
-except AttributeError:
-    pass
-else:
-    raise Exception


More information about the Python-checkins mailing list