[Zodb-checkins] CVS: Zope3/src/zope/interface -
declarations.py:1.17.10.5
Jim Fulton
jim at zope.com
Mon Oct 13 11:09:00 EDT 2003
Update of /cvs-repository/Zope3/src/zope/interface
In directory cvs.zope.org:/tmp/cvs-serv22218
Modified Files:
Tag: adaptergeddon-branch
declarations.py
Log Message:
Added a test for the provides declaration cache.
=== Zope3/src/zope/interface/declarations.py 1.17.10.4 => 1.17.10.5 ===
--- Zope3/src/zope/interface/declarations.py:1.17.10.4 Sun Oct 12 18:34:03 2003
+++ Zope3/src/zope/interface/declarations.py Mon Oct 13 11:08:59 2003
@@ -585,10 +585,6 @@
#
# Instance declarations
-# Registry of instance declarations
-# This is a memory optimization to allow objects to share specifications.
-InstanceDeclarations = weakref.WeakValueDictionary()
-
class Provides(Declaration):
"""Implement __provides__, the instance-specific specification
@@ -633,7 +629,54 @@
ProvidesClass = Provides
+# Registry of instance declarations
+# This is a memory optimization to allow objects to share specifications.
+InstanceDeclarations = weakref.WeakValueDictionary()
+
def Provides(*interfaces):
+ """Cache instance declarations
+
+ Instance declarations are shared among instances that have the
+ same declaration. The declarations are cached in an weak value
+ dictionary.
+
+ >>> import gc
+ >>> x = gc.collect()
+ >>> before = len(InstanceDeclarations)
+
+ >>> class C:
+ ... pass
+
+ >>> from zope.interface import Interface
+ >>> class I(Interface):
+ ... pass
+
+ >>> c1 = C()
+ >>> c2 = C()
+
+ >>> len(InstanceDeclarations) == before
+ 1
+
+ >>> directlyProvides(c1, I)
+ >>> len(InstanceDeclarations) == before + 1
+ 1
+
+ >>> directlyProvides(c2, I)
+ >>> len(InstanceDeclarations) == before + 1
+ 1
+
+ >>> del c1
+ >>> x = gc.collect()
+ >>> len(InstanceDeclarations) == before + 1
+ 1
+
+ >>> del c2
+ >>> x = gc.collect()
+ >>> len(InstanceDeclarations) == before
+ 1
+
+ """
+
spec = InstanceDeclarations.get(interfaces)
if spec is None:
spec = ProvidesClass(*interfaces)
More information about the Zodb-checkins
mailing list