[Zope-Checkins] SVN: Zope/branches/2.10/lib/python/Testing/ZopeTestCase/ Use a decorator to save and restore thread-local state.

Stefan H. Holek stefan at epy.co.at
Thu Mar 15 05:58:27 EDT 2007


Log message for revision 73184:
  Use a decorator to save and restore thread-local state.
  

Changed:
  U   Zope/branches/2.10/lib/python/Testing/ZopeTestCase/functional.py
  U   Zope/branches/2.10/lib/python/Testing/ZopeTestCase/zopedoctest/functional.py

-=-
Modified: Zope/branches/2.10/lib/python/Testing/ZopeTestCase/functional.py
===================================================================
--- Zope/branches/2.10/lib/python/Testing/ZopeTestCase/functional.py	2007-03-15 04:23:45 UTC (rev 73183)
+++ Zope/branches/2.10/lib/python/Testing/ZopeTestCase/functional.py	2007-03-15 09:58:25 UTC (rev 73184)
@@ -23,6 +23,25 @@
 import interfaces
 
 
+def savestate(func):
+    '''Decorator saving thread local state before executing func
+       and restoring it afterwards.
+    '''
+    from AccessControl.SecurityManagement import getSecurityManager
+    from AccessControl.SecurityManagement import setSecurityManager
+    from zope.app.component.hooks import getSite
+    from zope.app.component.hooks import setSite
+
+    def wrapped_func(*args, **kw):
+        sm, site = getSecurityManager(), getSite()
+        try:
+            return func(*args, **kw)
+        finally:
+            setSecurityManager(sm)
+            setSite(site)
+    return wrapped_func
+
+
 class Functional(sandbox.Sandboxed):
     '''Derive from this class and an xTestCase to get functional
        testing support::
@@ -33,25 +52,15 @@
 
     __implements__ = (interfaces.IFunctional,)
 
+    @savestate
     def publish(self, path, basic=None, env=None, extra=None,
                 request_method='GET', stdin=None, handle_errors=True):
         '''Publishes the object at 'path' returning a response object.'''
 
-        from zope.app.component.hooks import setSite, getSite
         from StringIO import StringIO
         from ZPublisher.Response import Response
         from ZPublisher.Test import publish_module
 
-        from AccessControl.SecurityManagement import getSecurityManager
-        from AccessControl.SecurityManagement import setSecurityManager
-
-        # Save current security manager
-        sm = getSecurityManager()
-
-        # And we need to store the old site
-        old_site = getSite()
-        setSite(None)
-
         # Commit the sandbox for good measure
         transaction.commit()
 
@@ -91,12 +100,6 @@
                        debug=not handle_errors,
                       )
 
-        # Restore security manager
-        setSecurityManager(sm)
-
-        # And we need to restore the site again
-        setSite(old_site)
-
         return ResponseWrapper(response, outstream, path)
 
 

Modified: Zope/branches/2.10/lib/python/Testing/ZopeTestCase/zopedoctest/functional.py
===================================================================
--- Zope/branches/2.10/lib/python/Testing/ZopeTestCase/zopedoctest/functional.py	2007-03-15 04:23:45 UTC (rev 73183)
+++ Zope/branches/2.10/lib/python/Testing/ZopeTestCase/zopedoctest/functional.py	2007-03-15 09:58:25 UTC (rev 73184)
@@ -31,6 +31,7 @@
 from Testing.ZopeTestCase import standard_permissions
 from Testing.ZopeTestCase.sandbox import AppZapper
 from Testing.ZopeTestCase.functional import ResponseWrapper
+from Testing.ZopeTestCase.functional import savestate
 
 
 class HTTPHeaderOutput:
@@ -110,6 +111,7 @@
     getRootFolder()._p_jar.sync()
 
 
+ at savestate
 def http(request_string, handle_errors=True):
     """Execute an HTTP request string via the publisher
 
@@ -117,20 +119,10 @@
     """
     import urllib
     import rfc822
-    from zope.app.component.hooks import setSite, getSite
     from cStringIO import StringIO
     from ZPublisher.Response import Response
     from ZPublisher.Test import publish_module
-    from AccessControl.SecurityManagement import getSecurityManager
-    from AccessControl.SecurityManagement import setSecurityManager
 
-    # Save current Security Manager
-    old_sm = getSecurityManager()
-
-    # And we need to store the old site
-    old_site = getSite()
-    setSite(None)
-
     # Commit work done by previous python code.
     transaction.commit()
 
@@ -194,14 +186,6 @@
     header_output.appendResponseHeaders(response._cookie_list())
     header_output.appendResponseHeaders(response.accumulated_headers.splitlines())
 
-    # Restore previous security manager, which may have been changed
-    # by calling the publish method above
-    setSecurityManager(old_sm)
-
-    # And we need to restore the site again
-    setSite(old_site)
-    # Sync connection
-
     sync()
 
     return DocResponseWrapper(response, outstream, path, header_output)



More information about the Zope-Checkins mailing list