| 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 typing import Any, Callable, Optional, Union |
| 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 |
""" Wrapper for python objects that need to be stored in event contexts and be retrived again from them |
| 47 |
Quick note on how this works: |
| 48 |
The actual *python* object has only 3 attributes which redirect into the wrapped C objects: |
| 49 |
_impl The wrapped C object itself |
| 50 |
_attrs This is a special pn_record_t holding a PYCTX which is a python dict |
| 51 |
every attribute in the python object is actually looked up here |
| 52 |
_record This is the C record itself (so actually identical to _attrs really but |
| 53 |
a different python type |
| 54 |
|
| 55 |
Because the objects actual attributes are stored away they must be initialised *after* the wrapping |
| 56 |
is set up. This is the purpose of the _init method in the wrapped object. Wrapper.__init__ will call |
| 57 |
eht subclass _init to initialise attributes. So they *must not* be initialised in the subclass __init__ |
| 58 |
before calling the superclass (Wrapper) __init__ or they will not be accessible from the wrapper at all. |
| 59 |
|
| 60 |
""" |
| 61 |
|
| 62 |
def __init__( |
| 63 |
self, |
| 64 |
impl_or_constructor: Union[Any, Callable[[], Any]], |
| 65 |
get_context: Optional[Callable[[Any], Any]] = None, |
| 66 |
) -> None: |
| 67 |
init = False |
| 68 |
if callable(impl_or_constructor): |
| 69 |
# we are constructing a new object |
| 70 |
impl = impl_or_constructor() |
| 71 |
if impl is None: |
| 72 |
self.__dict__["_impl"] = impl |
| 73 |
self.__dict__["_attrs"] = EMPTY_ATTRS |
| 74 |
self.__dict__["_record"] = None |
| 75 |
raise ProtonException( |
| 76 |
"Wrapper failed to create wrapped object. Check for file descriptor or memory exhaustion.") |
| 77 |
init = True |
| 78 |
else: |
| 79 |
# we are wrapping an existing object |
| 80 |
impl = impl_or_constructor |
| 81 |
pn_incref(impl) |
| 82 |
|
| 83 |
if get_context: |
| 84 |
record = get_context(impl) |
| 85 |
attrs = pn_void2py(pn_record_get(record, PYCTX)) |
| 86 |
if attrs is None: |
| 87 |
attrs = {} |
| 88 |
pn_record_def(record, PYCTX, PN_PYREF) |
| 89 |
pn_record_set(record, PYCTX, pn_py2void(attrs)) |
| 90 |
init = True |
| 91 |
else: |
| 92 |
attrs = EMPTY_ATTRS |
| 93 |
init = False |
| 94 |
record = None |
| 95 |
self.__dict__["_impl"] = impl |
| 96 |
self.__dict__["_attrs"] = attrs |
| 97 |
self.__dict__["_record"] = record |
| 98 |
if init: |
| 99 |
self._init() |
| 100 |
|
| 101 |
def __getattr__(self, name: str) -> Any: |
| 102 |
attrs = self.__dict__["_attrs"] |
| 103 |
if name in attrs: |
| 104 |
return attrs[name] |
| 105 |
else: |
| 106 |
raise AttributeError(name + " not in _attrs") |
| 107 |
|
| 108 |
def __setattr__(self, name: str, value: Any) -> None: |
| 109 |
if hasattr(self.__class__, name): |
| 110 |
object.__setattr__(self, name, value) |
| 111 |
else: |
| 112 |
attrs = self.__dict__["_attrs"] |
| 113 |
attrs[name] = value |
| 114 |
|
| 115 |
def __delattr__(self, name: str) -> None: |
| 116 |
attrs = self.__dict__["_attrs"] |
| 117 |
if attrs: |
| 118 |
del attrs[name] |
| 119 |
|
| 120 |
def __hash__(self) -> int: |
| 121 |
return hash(addressof(self._impl)) |
| 122 |
|
| 123 |
def __eq__(self, other: Any) -> bool: |
| 124 |
if isinstance(other, Wrapper): |
| 125 |
return addressof(self._impl) == addressof(other._impl) |
| 126 |
return False |
| 127 |
|
| 128 |
def __ne__(self, other: Any) -> bool: |
| 129 |
if isinstance(other, Wrapper): |
| 130 |
return addressof(self._impl) != addressof(other._impl) |
| 131 |
return True |
| 132 |
|
| 133 |
def __del__(self) -> None: |
| 134 |
pn_decref(self._impl) |
| 135 |
|
| 136 |
def __repr__(self) -> str: |
| 137 |
return '<%s.%s 0x%x ~ 0x%x>' % (self.__class__.__module__, |
| 138 |
self.__class__.__name__, |
| 139 |
id(self), addressof(self._impl)) |
| 140 |
|
| 141 |
|
| 142 |
PYCTX = int(pn_py2void(Wrapper)) |
| 143 |
addressof = int |