summaryrefslogtreecommitdiff
path: root/src/zope/interface/declarations.py
diff options
context:
space:
mode:
authorJason Madden <jamadden@gmail.com>2020-01-28 16:04:31 -0600
committerJason Madden <jamadden@gmail.com>2020-01-28 16:04:31 -0600
commit97997956d1a17a0cfb064dfe0487291e8f8ee181 (patch)
treea491a49d49388774f3b04ced86985578e6be4bac /src/zope/interface/declarations.py
parent489328a5afc1430a064db2dba7a7a408591789f9 (diff)
downloadzope-interface-issue162.tar.gz
The _empty singleton has no-op subscribe/unsubscribe methods.issue162
Turns out they can be called in some very strange error cases. See #162 and #163 for details. This should fix #162 (at least the provided test case, five.intid, passes now). It also does enough work on #163 that (a) the test can be written and run in pure-python mode, which was needed to debug it and (b) five.intid runs in pure-python mode (well, with a bunch of other small hacks to Acquisition, ExtensionClass, DocumentTemplate and AccessControl), but I won't claim that it fully fixes #163. For one thing, there are no specific tests. For another, I see more such differences.
Diffstat (limited to 'src/zope/interface/declarations.py')
-rw-r--r--src/zope/interface/declarations.py29
1 files changed, 21 insertions, 8 deletions
diff --git a/src/zope/interface/declarations.py b/src/zope/interface/declarations.py
index 97ccd55..04e2cc8 100644
--- a/src/zope/interface/declarations.py
+++ b/src/zope/interface/declarations.py
@@ -145,13 +145,15 @@ class _ImmutableDeclaration(Declaration):
if new_bases != ():
raise TypeError("Cannot set non-empty bases on shared empty Declaration.")
+ # As the immutable empty declaration, we cannot be changed.
+ # This means there's no logical reason for us to have dependents
+ # or subscriptions: we'll never notify them. So there's no need for
+ # us to keep track of any of that.
@property
def dependents(self):
return {}
- def changed(self, originally_changed):
- # Does nothing, we have no dependents or dependencies
- return
+ changed = subscribe = unsubscribe = lambda self, _ignored: None
def interfaces(self):
# An empty iterator
@@ -163,6 +165,16 @@ class _ImmutableDeclaration(Declaration):
def get(self, name, default=None):
return default
+ def weakref(self, callback=None):
+ # We're a singleton, we never go away. So there's no need to return
+ # distinct weakref objects here; their callbacks will never
+ # be called. Instead, we only need to return a callable that
+ # returns ourself. The easiest one is to return _ImmutableDeclaration
+ # itself; testing on Python 3.8 shows that's faster than a function that
+ # returns _empty. (Remember, one goal is to avoid allocating any
+ # object, and that includes a method.)
+ return _ImmutableDeclaration
+
##############################################################################
#
@@ -855,18 +867,19 @@ def ObjectSpecification(direct, cls):
@_use_c_impl
def getObjectSpecification(ob):
-
- provides = getattr(ob, '__provides__', None)
+ try:
+ provides = getattr(ob, '__provides__', None)
+ except:
+ provides = None
if provides is not None:
if isinstance(provides, SpecificationBase):
return provides
try:
cls = ob.__class__
- except AttributeError:
+ except:
# We can't get the class, so just consider provides
return _empty
-
return implementedBy(cls)
@@ -879,7 +892,7 @@ def providedBy(ob):
# Try to get __providedBy__
try:
r = ob.__providedBy__
- except AttributeError:
+ except:
# Not set yet. Fall back to lower-level thing that computes it
return getObjectSpecification(ob)