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