Package proton :: Module _wrapper
[frames] | no frames]

Source Code for Module proton._wrapper

  1  # 
  2  # Licensed to the Apache Software Foundation (ASF) under one 
  3  # or more contributor license agreements.  See the NOTICE file 
  4  # distributed with this work for additional information 
  5  # regarding copyright ownership.  The ASF licenses this file 
  6  # to you under the Apache License, Version 2.0 (the 
  7  # "License"); you may not use this file except in compliance 
  8  # with the License.  You may obtain a copy of the License at 
  9  # 
 10  #   http://www.apache.org/licenses/LICENSE-2.0 
 11  # 
 12  # Unless required by applicable law or agreed to in writing, 
 13  # software distributed under the License is distributed on an 
 14  # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
 15  # KIND, either express or implied.  See the License for the 
 16  # specific language governing permissions and limitations 
 17  # under the License. 
 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   
26 -class EmptyAttrs:
27
28 - def __contains__(self, name):
29 return False
30
31 - def __getitem__(self, name):
32 raise KeyError(name)
33
34 - def __setitem__(self, name, value):
35 raise TypeError("does not support item assignment")
36 37 38 EMPTY_ATTRS = EmptyAttrs() 39 40
41 -class Wrapper(object):
42
43 - def __init__(self, impl_or_constructor, get_context=None):
44 init = False 45 if callable(impl_or_constructor): 46 # we are constructing a new object 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 # we are wrapping an existing object 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
78 - def __getattr__(self, name):
79 attrs = self.__dict__["_attrs"] 80 if name in attrs: 81 return attrs[name] 82 else: 83 raise AttributeError(name + " not in _attrs")
84
85 - def __setattr__(self, name, value):
86 if hasattr(self.__class__, name): 87 object.__setattr__(self, name, value) 88 else: 89 attrs = self.__dict__["_attrs"] 90 attrs[name] = value
91
92 - def __delattr__(self, name):
93 attrs = self.__dict__["_attrs"] 94 if attrs: 95 del attrs[name]
96
97 - def __hash__(self):
98 return hash(addressof(self._impl))
99
100 - def __eq__(self, other):
101 if isinstance(other, Wrapper): 102 return addressof(self._impl) == addressof(other._impl) 103 return False
104
105 - def __ne__(self, other):
106 if isinstance(other, Wrapper): 107 return addressof(self._impl) != addressof(other._impl) 108 return True
109
110 - def __del__(self):
111 pn_decref(self._impl)
112
113 - def __repr__(self):
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