Skip to content

Commit

Permalink
WIP: Simplify python wrapper code
Browse files Browse the repository at this point in the history
Needed to add a custom __new__ to Transport because its constructor
parameters aren't compatible with the Wrapper class.
  • Loading branch information
astitcher committed Jan 13, 2025
1 parent a9e44eb commit 3d379b7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
1 change: 0 additions & 1 deletion python/proton/_delivery.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ def wrap(impl):
get_context = pn_delivery_attachments

def __init__(self, impl):
Wrapper.__init__(self, impl)
if self.Uninitialized():
self.local = Disposition(pn_delivery_local(self._impl), True)
self.remote = Disposition(pn_delivery_remote(self._impl), False)
Expand Down
3 changes: 0 additions & 3 deletions python/proton/_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ def wrap(impl):
get_context = pn_connection_attachments

def __init__(self, impl: Any = None) -> None:
Wrapper.__init__(self, impl)
if self.Uninitialized():
Endpoint.__init__(self)
self.offered_capabilities_list = None
Expand Down Expand Up @@ -559,7 +558,6 @@ def wrap(impl):
get_context = pn_session_attachments

def __init__(self, impl):
Wrapper.__init__(self, impl)
if self.Uninitialized():
Endpoint.__init__(self)

Expand Down Expand Up @@ -729,7 +727,6 @@ def wrap(impl):
get_context = pn_link_attachments

def __init__(self, impl):
Wrapper.__init__(self, impl)
if self.Uninitialized():
Endpoint.__init__(self)
self.properties = None
Expand Down
8 changes: 7 additions & 1 deletion python/proton/_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,18 @@ def wrap(impl: Optional[Callable]) -> Optional['Transport']:
constructor = pn_transport
get_context = pn_transport_attachments

def __new__(
cls,
mode: Optional[int] = None,
impl=None,
) -> 'Transport':
return super().__new__(cls, impl)

def __init__(
self,
mode: Optional[int] = None,
impl=None,
) -> None:
Wrapper.__init__(self, impl)
if mode == Transport.SERVER:
pn_transport_set_server(self._impl)
elif mode is None or mode == Transport.CLIENT:
Expand Down
8 changes: 5 additions & 3 deletions python/proton/_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,26 @@ class Wrapper(object):

__slots__ = ["_impl", "_attrs"]

def __init__(self, impl: Any = None) -> None:
def __new__(cls, impl: Any = None, *kw) -> Any:
attrs = None
try:
if impl is None:
# we are constructing a new object
impl = self.constructor()
impl = cls.constructor()
if impl is None:
raise ProtonException(
"Wrapper failed to create wrapped object. Check for file descriptor or memory exhaustion.")
else:
# we are wrapping an existing object
pn_incref(impl)

record = self.get_context(impl)
record = cls.get_context(impl)
attrs = pn_record_get_py(record)
finally:
self = object.__new__(cls)
self._impl = impl
self._attrs = attrs
return self

def Uninitialized(self) -> bool:
return self._attrs == {}
Expand Down

0 comments on commit 3d379b7

Please sign in to comment.