1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 from cproton import pn_incref, pn_decref, \
21 pn_py2void, pn_void2py, \
22 pn_record_get, pn_record_def, pn_record_set, \
23 PN_PYREF
24
25
27
30
33
35 raise TypeError("does not support item assignment")
36
37
38 EMPTY_ATTRS = EmptyAttrs()
39
40
42
43 - def __init__(self, impl_or_constructor, get_context=None):
44 init = False
45 if callable(impl_or_constructor):
46
47 impl = impl_or_constructor()
48 if impl is None:
49 self.__dict__["_impl"] = impl
50 self.__dict__["_attrs"] = EMPTY_ATTRS
51 self.__dict__["_record"] = None
52 from proton import ProtonException
53 raise ProtonException(
54 "Wrapper failed to create wrapped object. Check for file descriptor or memory exhaustion.")
55 init = True
56 else:
57
58 impl = impl_or_constructor
59 pn_incref(impl)
60
61 if get_context:
62 record = get_context(impl)
63 attrs = pn_void2py(pn_record_get(record, PYCTX))
64 if attrs is None:
65 attrs = {}
66 pn_record_def(record, PYCTX, PN_PYREF)
67 pn_record_set(record, PYCTX, pn_py2void(attrs))
68 init = True
69 else:
70 attrs = EMPTY_ATTRS
71 init = False
72 record = None
73 self.__dict__["_impl"] = impl
74 self.__dict__["_attrs"] = attrs
75 self.__dict__["_record"] = record
76 if init: self._init()
77
79 attrs = self.__dict__["_attrs"]
80 if name in attrs:
81 return attrs[name]
82 else:
83 raise AttributeError(name + " not in _attrs")
84
86 if hasattr(self.__class__, name):
87 object.__setattr__(self, name, value)
88 else:
89 attrs = self.__dict__["_attrs"]
90 attrs[name] = value
91
93 attrs = self.__dict__["_attrs"]
94 if attrs:
95 del attrs[name]
96
98 return hash(addressof(self._impl))
99
101 if isinstance(other, Wrapper):
102 return addressof(self._impl) == addressof(other._impl)
103 return False
104
106 if isinstance(other, Wrapper):
107 return addressof(self._impl) != addressof(other._impl)
108 return True
109
111 pn_decref(self._impl)
112
114 return '<%s.%s 0x%x ~ 0x%x>' % (self.__class__.__module__,
115 self.__class__.__name__,
116 id(self), addressof(self._impl))
117
118
119 PYCTX = int(pn_py2void(Wrapper))
120 addressof = int
121