[Zodb-checkins] CVS: Zope3/src/zope/interface - surrogate.py:1.1.2.8
Jim Fulton
cvs-admin at zope.org
Mon Nov 10 15:45:51 EST 2003
Update of /cvs-repository/Zope3/src/zope/interface
In directory cvs.zope.org:/tmp/cvs-serv24129
Modified Files:
Tag: adaptergeddon-branch
surrogate.py
Log Message:
Changed the strategy for maintaining the implied mapping to
use local (to surrogates) implied and multi-implied mappings that get
merged to make the overall implied mapping. This makes the
computation go a bit faster, especially when there are updates.
=== Zope3/src/zope/interface/surrogate.py 1.1.2.7 => 1.1.2.8 ===
--- Zope3/src/zope/interface/surrogate.py:1.1.2.7 Mon Nov 10 06:24:45 2003
+++ Zope3/src/zope/interface/surrogate.py Mon Nov 10 15:45:20 2003
@@ -75,12 +75,6 @@
Default = InterfaceClass("Default", (), {})
-
-from _zope_interface_coptimizations import debug
-
-
-
-
class Surrogate(object):
"""Specification surrogate
@@ -91,6 +85,9 @@
def __init__(self, interface, registry):
self._adapters = {}
self._implied = {}
+ self._selfimplied = {}
+ self._multimplied = {}
+
self._interface = interface.weakref()
interface.subscribe(self)
@@ -99,6 +96,7 @@
self.__bases__ = [registry.get(base) for base in interface.__bases__]
for base in self.__bases__:
base.subscribe(self)
+
self._computeImplied()
def subscribe(self, dependent):
@@ -115,44 +113,54 @@
def _adaptTo(self, specification, factories, name='', with=()):
self._adapters[with, name, specification] = factories
+ self._computeSelfImplied()
self.changed()
def changed(self):
self._computeImplied()
for dependent in self.dependents.keys():
dependent.changed()
+
+ def _computeSelfImplied(self):
+ implied = self._selfimplied
+ implied.clear()
+ multi = self._multimplied
+ multi.clear()
+ registered = {}
+
+ # Add adapters and interfaces directly implied by same:
+ for (with, name, target), factories in self._adapters.iteritems():
+ if with:
+ self.__add_multi_adapter(with, name, target, target,
+ multi, factories)
+ elif name:
+ self.__add_named_adapter(target, target, name,
+ implied, registered,
+ factories)
+ else:
+ self.__add_adapter(target, target,
+ implied, registered,
+ factories)
+
def _computeImplied(self):
-
implied = self._implied
implied.clear()
ancestors = ro(self)
-
+ # Collect implied data in reverse order to have more specific data
+ # override less-specific data.
+ ancestors.reverse()
for ancestor in ancestors:
- # Work dict used to keep track of the registered interface
- # leading to an implied entry. This is so that we can can
- # override an implied entry of we get a closer "provided"
- # fit.
- registered = {}
-
- # Add adapters and interfaces directly implied by same:
- items = ancestor._adapters.iteritems()
- for (with, name, target), factories in items:
- if with:
- self.__add_multi_adapter(with, name, target, target,
- implied, registered,
- factories)
- elif name:
- self.__add_named_adapter(target, target, name,
- implied, registered,
- factories)
+ implied.update(ancestor._selfimplied)
+ for k, ancestor_adapters in ancestor._multimplied.iteritems():
+ implied_adapters = implied.get(k)
+ if implied_adapters:
+ implied_adapters.update(ancestor_adapters)
else:
- self.__add_adapter(target, target,
- implied, registered,
- factories)
-
+ implied[k] = ancestor_adapters.copy()
+
def __add_adapter(self, target, provided, implied, registered, factories):
if (target not in implied
@@ -178,7 +186,7 @@
implied, registered, factories)
def __add_multi_adapter(self, interfaces, name, target, provided,
- implied, registered, factories):
+ implied, factories):
order = len(interfaces)+1
adapters = implied.get((target, name, order))
@@ -190,7 +198,7 @@
for b in target.__bases__:
self.__add_multi_adapter(interfaces, name, b, provided,
- implied, registered, factories)
+ implied, factories)
def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, self._interface())
More information about the Zodb-checkins
mailing list