-
Notifications
You must be signed in to change notification settings - Fork 0
/
interfaces.py
84 lines (66 loc) · 2.67 KB
/
interfaces.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Interfaces relating to weak references.
"""
from __future__ import print_function, absolute_import, division
__docformat__ = "restructuredtext en"
logger = __import__('logging').getLogger(__name__)
from zope import interface
class IWeakRef(interface.Interface):
"""
Represents a weak reference to some object. The strategy for
creating and maintaining weak references may vary dramatically for
different types of objects, so the semantics associated with cleanup
should not be assumed. For example, weak references to ordinary python
objects can exist only within the lifetime of a single process
and clear immediately when the original object is gone, but weak
references to persistent objects may last for several processes and
may persist even after the original object is gone.
"""
def __call__():
"""
Weak references are callable objects. Calling them returns
the object they reference if it is still available, otherwise
it returns ``None``.
"""
def __eq__(other):
"""
Weak references should be equal to other objects that
weakly reference the same object.
"""
def __hash__():
"""
Weak references should be suitable for hashing.
If possible, they should hash the same as the underlying object.
"""
import weakref
interface.classImplements(weakref.ref, IWeakRef)
import persistent.wref
interface.classImplements(persistent.wref.WeakRef, IWeakRef)
class IWeakRefToMissing(IWeakRef):
"""
A weak reference that knows enough about the object it was
referencing to be able to produce a :const:`.TYPE_MISSING` NTIID
when the reference is clear (when calling this object returns ``None``).
"""
def make_missing_ntiid():
"""
Call this when the reference is clear to produce an NTIID
that refers to the object that was referenced.
Calling this before the reference is clear is not defined.
"""
class ICachingWeakRef(IWeakRef):
"""
A weak ref that, as an implementation detail, may cache the referant.
Whether or not to use that cached value is exposed as a keyword argument.
"""
def __call__(allow_cached=True):
"""
Resolve the reference, as with :meth:`IWeakRef.call`; however, allows
control of caching.
:keyword allow_cached: If `True` (the default) a cached value can be used. If
set to `False`, this will not return a cached value. Note, however, that,
in a cluster, this may still return an object that other nodes might consider
gone.
"""