Skip to content

Commit

Permalink
fix: avoid cythonizing SendReply (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Aug 24, 2023
1 parent ed5c87f commit d12266d
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 65 deletions.
5 changes: 0 additions & 5 deletions src/dbus_fast/message_bus.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ cdef object assert_bus_name_valid
cdef _expects_reply(Message msg)


cdef class SendReply:

cdef object _bus
cdef object _msg

cdef class BaseMessageBus:

cdef public object unique_name
Expand Down
51 changes: 1 addition & 50 deletions src/dbus_fast/message_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import socket
import traceback
import xml.etree.ElementTree as ET
from types import TracebackType
from typing import Any, Callable, Dict, List, Optional, Type, Union

from . import introspection as intr
Expand All @@ -21,6 +20,7 @@
from .errors import DBusError, InvalidAddressError
from .message import Message
from .proxy_object import BaseProxyObject
from .send_reply import SendReply
from .service import ServiceInterface, _Method
from .signature import Variant
from .validators import assert_bus_name_valid, assert_object_path_valid
Expand Down Expand Up @@ -57,55 +57,6 @@ def _block_unexpected_reply(reply: _Message) -> None:
BLOCK_UNEXPECTED_REPLY = _block_unexpected_reply


class SendReply:
"""A context manager to send a reply to a message."""

__slots__ = ("_bus", "_msg")

def __init__(self, bus: "BaseMessageBus", msg: Message) -> None:
"""Create a new reply context manager."""
self._bus = bus
self._msg = msg

def __enter__(self):
return self

def __call__(self, reply: Message) -> None:
self._bus.send(reply)

def _exit(
self,
exc_type: Optional[Type[Exception]],
exc_value: Optional[Exception],
tb: Optional[TracebackType],
) -> bool:
if exc_value:
if isinstance(exc_value, DBusError):
self(exc_value._as_message(self._msg))
else:
self(
Message.new_error(
self._msg,
ErrorType.SERVICE_ERROR,
f"The service interface raised an error: {exc_value}.\n{traceback.format_tb(tb)}",
)
)
return True

return False

def __exit__(
self,
exc_type: Optional[Type[Exception]],
exc_value: Optional[Exception],
tb: Optional[TracebackType],
) -> bool:
return self._exit(exc_type, exc_value, tb)

def send_error(self, exc: Exception) -> None:
self._exit(exc.__class__, exc, exc.__traceback__)


class BaseMessageBus:
"""An abstract class to manage a connection to a DBus message bus.
Expand Down
59 changes: 59 additions & 0 deletions src/dbus_fast/send_reply.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import traceback
from types import TracebackType
from typing import TYPE_CHECKING, Optional, Type

from .constants import ErrorType
from .errors import DBusError
from .message import Message

if TYPE_CHECKING:
from .message_bus import BaseMessageBus


class SendReply:
"""A context manager to send a reply to a message."""

__slots__ = ("_bus", "_msg")

def __init__(self, bus: "BaseMessageBus", msg: Message) -> None:
"""Create a new reply context manager."""
self._bus = bus
self._msg = msg

def __enter__(self):
return self

def __call__(self, reply: Message) -> None:
self._bus.send(reply)

def _exit(
self,
exc_type: Optional[Type[Exception]],
exc_value: Optional[Exception],
tb: Optional[TracebackType],
) -> bool:
if exc_value:
if isinstance(exc_value, DBusError):
self(exc_value._as_message(self._msg))
else:
self(
Message.new_error(
self._msg,
ErrorType.SERVICE_ERROR,
f"The service interface raised an error: {exc_value}.\n{traceback.format_tb(tb)}",
)
)
return True

return False

def __exit__(
self,
exc_type: Optional[Type[Exception]],
exc_value: Optional[Exception],
tb: Optional[TracebackType],
) -> bool:
return self._exit(exc_type, exc_value, tb)

def send_error(self, exc: Exception) -> None:
self._exit(exc.__class__, exc, exc.__traceback__)
1 change: 0 additions & 1 deletion tests/service/test_standard_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ async def test_standard_interface_properties():
"org.freedesktop.DBus.Peer",
"org.freedesktop.DBus.ObjectManager",
]:

result = await bus2.call(
Message(
destination=bus1.unique_name,
Expand Down
1 change: 0 additions & 1 deletion tests/test_address_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


def test_valid_addresses():

valid_addresses = {
"unix:path=/run/user/1000/bus": [("unix", {"path": "/run/user/1000/bus"})],
"unix:abstract=/tmp/dbus-ft9sODWpZk,guid=a7b1d5912379c2d471165e9b5cb74a03": [
Expand Down
2 changes: 0 additions & 2 deletions tests/test_marshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ def test_unmarshalling_with_table(unmarshall_table):
from dbus_fast._private import unmarshaller

for item in unmarshall_table:

stream = io.BytesIO(bytes.fromhex(item["data"]))
unmarshaller = Unmarshaller(stream)
try:
Expand Down Expand Up @@ -486,7 +485,6 @@ def tests_fallback_no_cython():


def test_unmarshall_large_message():

stream = io.BytesIO(bytes.fromhex(get_managed_objects_msg))
unmarshaller = Unmarshaller(stream)
unmarshaller.unmarshall()
Expand Down
9 changes: 3 additions & 6 deletions tests/test_send_reply.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import os
import traceback
from types import TracebackType
from typing import Any, Callable, Dict, List, Optional, Type, Union
from unittest.mock import Mock

import pytest

from dbus_fast.constants import ErrorType, MessageType
from dbus_fast.errors import DBusError
from dbus_fast.message import Message
from dbus_fast.message_bus import BaseMessageBus, SendReply
from dbus_fast.message_bus import BaseMessageBus
from dbus_fast.send_reply import SendReply


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -44,7 +41,7 @@ def _setup_socket(self) -> None:
)
send_reply = SendReply(mock_message_bus, mock_message)

with send_reply as reply:
with send_reply:
raise DBusError(ErrorType.DISCONNECTED, "Disconnected", None)

assert len(messages) == 1
Expand Down

0 comments on commit d12266d

Please sign in to comment.