[Zope3-checkins] SVN: Zope3/trunk/src/zope/publisher/ Added IHeld interface. Now, when a request is closed, if any of it's

Jim Fulton jim at zope.com
Mon Apr 25 16:18:41 EDT 2005


Log message for revision 30173:
  Added IHeld interface. Now, when a request is closed, if any of it's
  held objects provide IHeld, then their release method will be called
  to notify them that they can release resources.
  

Changed:
  U   Zope3/trunk/src/zope/publisher/base.py
  U   Zope3/trunk/src/zope/publisher/interfaces/__init__.py
  U   Zope3/trunk/src/zope/publisher/tests/basetestipublicationrequest.py

-=-
Modified: Zope3/trunk/src/zope/publisher/base.py
===================================================================
--- Zope3/trunk/src/zope/publisher/base.py	2005-04-25 19:16:25 UTC (rev 30172)
+++ Zope3/trunk/src/zope/publisher/base.py	2005-04-25 20:18:36 UTC (rev 30173)
@@ -25,7 +25,7 @@
 from zope.interface.common.mapping import IReadMapping, IEnumerableMapping
 from zope.publisher.interfaces import NotFound
 
-from zope.publisher.interfaces import IPublication
+from zope.publisher.interfaces import IPublication, IHeld
 from zope.publisher.interfaces import NotFound, DebugError, Unauthorized
 from zope.publisher.interfaces import IRequest, IResponse, IDebugFlags
 from zope.publisher.publish import mapply
@@ -278,6 +278,11 @@
 
     def close(self):
         'See IPublicationRequest'
+
+        for held in self._held:
+            if IHeld.providedBy(held):
+                held.release()
+        
         self._held = None
         self._response = None
         self._body_instream = None

Modified: Zope3/trunk/src/zope/publisher/interfaces/__init__.py
===================================================================
--- Zope3/trunk/src/zope/publisher/interfaces/__init__.py	2005-04-25 19:16:25 UTC (rev 30172)
+++ Zope3/trunk/src/zope/publisher/interfaces/__init__.py	2005-04-25 20:18:36 UTC (rev 30173)
@@ -305,8 +305,12 @@
         """Release resources held by the request.
         """
 
-    def hold(object):
-        """Hold a reference to an object until the request is closed
+    def hold(held):
+        """Hold a reference to an object until the request is closed.
+
+        The object should be an IHeld.  If it is an IHeld, it's
+        release method will be called when it is released.
+        
         """
 
     def getTraversalStack():
@@ -332,7 +336,18 @@
         It should be IPrincipal wrapped in it's AuthenticationService's context.
         """
 
+class IHeld(Interface):
+    """Object to be held and explicitly released by a request
+    """
 
+    def release():
+        """Release the held object
+
+        This is called by a request that holds the IHeld when the
+        request is closed
+
+        """
+
 class IPublisherRequest(IPublicationRequest):
     """Request interface use by the publisher
 

Modified: Zope3/trunk/src/zope/publisher/tests/basetestipublicationrequest.py
===================================================================
--- Zope3/trunk/src/zope/publisher/tests/basetestipublicationrequest.py	2005-04-25 19:16:25 UTC (rev 30172)
+++ Zope3/trunk/src/zope/publisher/tests/basetestipublicationrequest.py	2005-04-25 20:18:36 UTC (rev 30173)
@@ -17,12 +17,20 @@
 """
 import sys
 
-from zope.interface import Interface, directlyProvides
+from zope.interface import Interface, directlyProvides, implements
 from zope.interface.verify import verifyObject
-from zope.publisher.interfaces import IPublicationRequest
+from zope.publisher.interfaces import IPublicationRequest, IHeld
 from zope.publisher.interfaces.browser import ISkin
 
+class Held:
+    implements(IHeld)
+    
+    released = False
 
+    def release(self):
+        self.released = True
+
+
 class BaseTestIPublicationRequest(object):
     def testVerifyIPublicationRequest(self):
         verifyObject(IPublicationRequest, self._Test__new())
@@ -48,11 +56,20 @@
 
         request.hold(resource)
 
+        resource2 = Held()
+        rcresource2 = sys.getrefcount(resource2)
+        request.hold(resource2)
+
         self.failUnless(sys.getrefcount(resource) > rcresource)
+        self.failUnless(sys.getrefcount(resource2) > rcresource2)
+        self.failIf(resource2.released)
 
         request.close()
+
+        self.failUnless(resource2.released)
         self.failUnless(sys.getrefcount(response) < rcresponse)
         self.assertEqual(sys.getrefcount(resource), rcresource)
+        self.assertEqual(sys.getrefcount(resource2), rcresource2)
 
     def testSkinManagement(self):
         request = self._Test__new()



More information about the Zope3-Checkins mailing list