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 __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   
30 -class EmptyAttrs:
31
32 - def __contains__(self, name):
33 return False
34
35 - def __getitem__(self, name):
36 raise KeyError(name)
37
38 - def __setitem__(self, name, value):
39 raise TypeError("does not support item assignment")
40 41 42 EMPTY_ATTRS = EmptyAttrs() 43 44
45 -class Wrapper(object):
46
47 - def __init__(self, impl_or_constructor, get_context=None):
48 init = False 49 if callable(impl_or_constructor): 50 # we are constructing a new object 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 # we are wrapping an existing object 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
81 - def __getattr__(self, name):
82 attrs = self.__dict__["_attrs"] 83 if name in attrs: 84 return attrs[name] 85 else: 86 raise AttributeError(name + " not in _attrs")
87
88 - def __setattr__(self, name, value):
89 if hasattr(self.__class__, name): 90 object.__setattr__(self, name, value) 91 else: 92 attrs = self.__dict__["_attrs"] 93 attrs[name] = value
94
95 - def __delattr__(self, name):
96 attrs = self.__dict__["_attrs"] 97 if attrs: 98 del attrs[name]
99
100 - def __hash__(self):
101 return hash(addressof(self._impl))
102
103 - def __eq__(self, other):
104 if isinstance(other, Wrapper): 105 return addressof(self._impl) == addressof(other._impl) 106 return False
107
108 - def __ne__(self, other):
109 if isinstance(other, Wrapper): 110 return addressof(self._impl) != addressof(other._impl) 111 return True
112
113 - def __del__(self):
114 pn_decref(self._impl)
115
116 - def __repr__(self):
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