[Zope3-checkins] SVN: Zope3/branches/srichter-blow-services/src/zope/component/ Make GlobalSiteManager and its two registries pickable.

Stephan Richter srichter at cosmos.phy.tufts.edu
Thu Jan 6 19:08:38 EST 2005


Log message for revision 28755:
  Make GlobalSiteManager and its two registries pickable.
  

Changed:
  U   Zope3/branches/srichter-blow-services/src/zope/component/site.py
  U   Zope3/branches/srichter-blow-services/src/zope/component/tests.py

-=-
Modified: Zope3/branches/srichter-blow-services/src/zope/component/site.py
===================================================================
--- Zope3/branches/srichter-blow-services/src/zope/component/site.py	2005-01-07 00:07:39 UTC (rev 28754)
+++ Zope3/branches/srichter-blow-services/src/zope/component/site.py	2005-01-07 00:08:38 UTC (rev 28755)
@@ -65,10 +65,6 @@
 class SiteManager(object):
     """Site Manager implementation"""
 
-    def __init__(self):
-        self.adapters = AdapterRegistry()
-        self.utilities = AdapterRegistry()
-
     def queryAdapter(self, object, interface, name, default=None):
         """See ISiteManager interface"""
         return self.adapters.queryAdapter(object, interface, name, default)
@@ -113,10 +109,12 @@
     implements(IGlobalSiteManager)
 
     def __init__(self, name=None):
-        super(GlobalSiteManager, self).__init__()
-        self.__name__ = None
+        self.__name__ = name
         self._registrations = {}
+        self.adapters = GlobalAdapterRegistry(self, 'adapters')
+        self.utilities = GlobalAdapterRegistry(self, 'utilities')
 
+
     def provideAdapter(self, required, provided, name, factory, info=''):
         """Register an adapter
 
@@ -243,17 +241,35 @@
                 yield registration
 
     def __reduce__(self):
-        # Global service managers are pickled as global objects
+        # Global site managers are pickled as global objects
         return self.__name__
 
 
+def GAR(siteManager, registryName):
+    return getattr(siteManager, registryName)
+
+class GlobalAdapterRegistry(AdapterRegistry):
+    """A global adapter registry
+
+    This adapter registry's main purpose is to be picklable in combination
+    with a site manager.
+    """
+    def __init__(self, parent=None, name=None):
+       self.__parent__ = parent
+       self.__name__ = name
+       super(GlobalAdapterRegistry, self).__init__()
+    
+    def __reduce__(self):
+        return GAR, (self.__parent__, self.__name__)
+
+
 # Global Site Manager Instance
 globalSiteManager = GlobalSiteManager('globalSiteManager')
 
 # Register our cleanup with zope.testing.cleanup to make writing unit tests
 # simpler.
 from zope.testing.cleanup import addCleanUp
-addCleanUp(globalSiteManager.__init__)
+addCleanUp(lambda : globalSiteManager.__init__(globalSiteManager.__name__))
 del addCleanUp
 
 

Modified: Zope3/branches/srichter-blow-services/src/zope/component/tests.py
===================================================================
--- Zope3/branches/srichter-blow-services/src/zope/component/tests.py	2005-01-07 00:07:39 UTC (rev 28754)
+++ Zope3/branches/srichter-blow-services/src/zope/component/tests.py	2005-01-07 00:08:38 UTC (rev 28755)
@@ -563,6 +563,26 @@
       AttributeError: __component_adapts__
     """
 
+def test_ability_to_pickle_globalsitemanager():
+    """
+    We need to make sure that it is possible to pickle the global site manager
+    and its two global adapter registries.
+
+      >>> from zope.component import site
+      >>> import cPickle
+      >>> pickle = cPickle.dumps(site.globalSiteManager)
+      >>> sm = cPickle.loads(pickle)
+      >>> sm is site.globalSiteManager
+      True
+
+    Now let's ensure that the registries themselves can be pickled as well:
+
+      >>> pickle = cPickle.dumps(site.globalSiteManager.adapters)
+      >>> adapters = cPickle.loads(pickle)
+      >>> adapters is site.globalSiteManager.adapters
+      True
+    """
+
 def test_suite():
     return unittest.TestSuite((
         doctest.DocTestSuite(setUp=setUp, tearDown=tearDown),



More information about the Zope3-Checkins mailing list