Skip to content

Commit

Permalink
Fixed str and int subclasses not working on fast API
Browse files Browse the repository at this point in the history
  • Loading branch information
igo95862 committed Jun 4, 2022
1 parent 1a8ab05 commit b1f28a6
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 47 deletions.
6 changes: 0 additions & 6 deletions src/sdbus/sd_bus_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@
return NULL; \
}

#define SD_BUS_PY_CHECK_ARG_TYPE(arg_num, arg_expected_type) \
if (Py_TYPE(args[arg_num]) != &arg_expected_type) { \
PyErr_SetString(PyExc_TypeError, "Argument is not an " #arg_expected_type ""); \
return NULL; \
}

#define SD_BUS_PY_CHECK_ARG_CHECK_FUNC(arg_num, arg_check_function) \
if (!arg_check_function(args[arg_num])) { \
PyErr_SetString(PyExc_TypeError, "Argument failed a " #arg_check_function " check"); \
Expand Down
46 changes: 23 additions & 23 deletions src/sdbus/sd_bus_internals_bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ static int SdBus_init(SdBusObject* self, PyObject* Py_UNUSED(args), PyObject* Py
#ifndef Py_LIMITED_API
static SdBusMessageObject* SdBus_new_method_call_message(SdBusObject* self, PyObject* const* args, Py_ssize_t nargs) {
SD_BUS_PY_CHECK_ARGS_NUMBER(4);
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(2, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(3, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(2, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(3, PyUnicode_Check);

const char* destination_bus_name = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
const char* object_path = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);
Expand All @@ -66,10 +66,10 @@ static SdBusMessageObject* SdBus_new_method_call_message(SdBusObject* self, PyOb
#ifndef Py_LIMITED_API
static SdBusMessageObject* SdBus_new_property_get_message(SdBusObject* self, PyObject* const* args, Py_ssize_t nargs) {
SD_BUS_PY_CHECK_ARGS_NUMBER(4);
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(2, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(3, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(2, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(3, PyUnicode_Check);

const char* destination_service_name = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
const char* object_path = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);
Expand Down Expand Up @@ -99,10 +99,10 @@ static SdBusMessageObject* SdBus_new_property_get_message(SdBusObject* self, PyO
#ifndef Py_LIMITED_API
static SdBusMessageObject* SdBus_new_property_set_message(SdBusObject* self, PyObject* const* args, Py_ssize_t nargs) {
SD_BUS_PY_CHECK_ARGS_NUMBER(4);
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(2, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(3, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(2, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(3, PyUnicode_Check);

const char* destination_service_name = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
const char* object_path = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);
Expand Down Expand Up @@ -132,9 +132,9 @@ static SdBusMessageObject* SdBus_new_property_set_message(SdBusObject* self, PyO
#ifndef Py_LIMITED_API
static SdBusMessageObject* SdBus_new_signal_message(SdBusObject* self, PyObject* const* args, Py_ssize_t nargs) {
SD_BUS_PY_CHECK_ARGS_NUMBER(3);
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type); // Path
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type); // Interface
SD_BUS_PY_CHECK_ARG_TYPE(2, PyUnicode_Type); // Member
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check); // Path
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check); // Interface
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(2, PyUnicode_Check); // Member

const char* object_path = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
const char* interface_name = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);
Expand Down Expand Up @@ -352,8 +352,8 @@ static int _check_is_sdbus_interface(PyObject* type_to_check) {
static PyObject* SdBus_add_interface(SdBusObject* self, PyObject* const* args, Py_ssize_t nargs) {
SD_BUS_PY_CHECK_ARGS_NUMBER(3);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, _check_is_sdbus_interface);
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(2, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(2, PyUnicode_Check);

SdBusInterfaceObject* interface_object = (SdBusInterfaceObject*)args[0];
const char* path_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);
Expand Down Expand Up @@ -497,8 +497,8 @@ int SdBus_request_callback(sd_bus_message* m,
#ifndef Py_LIMITED_API
static PyObject* SdBus_request_name_async(SdBusObject* self, PyObject* const* args, Py_ssize_t nargs) {
SD_BUS_PY_CHECK_ARGS_NUMBER(2);
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(1, PyLong_Type);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyLong_Check);

const char* service_name_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
uint64_t flags = PyLong_AsUnsignedLongLong(args[1]);
Expand Down Expand Up @@ -529,8 +529,8 @@ static PyObject* SdBus_request_name_async(SdBusObject* self, PyObject* args) {
#ifndef Py_LIMITED_API
static PyObject* SdBus_request_name(SdBusObject* self, PyObject* const* args, Py_ssize_t nargs) {
SD_BUS_PY_CHECK_ARGS_NUMBER(2);
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(1, PyLong_Type);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyLong_Check);

const char* service_name_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
uint64_t flags = PyLong_AsUnsignedLongLong(args[1]);
Expand All @@ -551,7 +551,7 @@ static PyObject* SdBus_request_name(SdBusObject* self, PyObject* args) {
#ifndef Py_LIMITED_API
static SdBusSlotObject* SdBus_add_object_manager(SdBusObject* self, PyObject* const* args, Py_ssize_t nargs) {
SD_BUS_PY_CHECK_ARGS_NUMBER(1);
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);

const char* object_manager_path = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
#else
Expand All @@ -570,7 +570,7 @@ static SdBusSlotObject* SdBus_add_object_manager(SdBusObject* self, PyObject* ar
#ifndef Py_LIMITED_API
static PyObject* SdBus_emit_object_added(SdBusObject* self, PyObject* const* args, Py_ssize_t nargs) {
SD_BUS_PY_CHECK_ARGS_NUMBER(1);
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);

const char* added_object_path = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
#else
Expand Down
8 changes: 4 additions & 4 deletions src/sdbus/sd_bus_internals_funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ static SdBusObject* sd_bus_py_open_user_machine(PyObject* Py_UNUSED(self), PyObj
#ifndef Py_LIMITED_API
static PyObject* encode_object_path(PyObject* Py_UNUSED(self), PyObject* const* args, Py_ssize_t nargs) {
SD_BUS_PY_CHECK_ARGS_NUMBER(2);
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);

const char* prefix_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
const char* external_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);
Expand Down Expand Up @@ -106,8 +106,8 @@ static PyObject* encode_object_path(PyObject* Py_UNUSED(self), PyObject* args) {
#ifndef Py_LIMITED_API
static PyObject* decode_object_path(PyObject* Py_UNUSED(self), PyObject* const* args, Py_ssize_t nargs) {
SD_BUS_PY_CHECK_ARGS_NUMBER(2);
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);

const char* prefix_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
const char* full_path_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);
Expand Down
14 changes: 7 additions & 7 deletions src/sdbus/sd_bus_internals_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ static PyObject* SdBusInterface_add_property(SdBusInterfaceObject* self, PyObjec
// Arguments
// Name, Signature, Get, Set, Flags
SD_BUS_PY_CHECK_ARGS_NUMBER(5);
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(2, PyCallable_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(3, _check_callable_or_none);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(4, PyLong_Check);
Expand Down Expand Up @@ -96,10 +96,10 @@ static PyObject* SdBusInterface_add_method(SdBusInterfaceObject* self, PyObject*
// Method name, signature, names of input values, result signature,
// names of result values, flags, callback function or coroutine
SD_BUS_PY_CHECK_ARGS_NUMBER(7);
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(2, PySequence_Check);
SD_BUS_PY_CHECK_ARG_TYPE(3, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(3, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(4, PySequence_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(5, PyLong_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(6, PyCallable_Check);
Expand Down Expand Up @@ -153,8 +153,8 @@ static PyObject* SdBusInterface_add_signal(SdBusInterfaceObject* self, PyObject*
// Arguments
// Signal name, signature, names of input values, flags
SD_BUS_PY_CHECK_ARGS_NUMBER(4);
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(2, PySequence_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(3, PyLong_Check);

Expand Down
14 changes: 7 additions & 7 deletions src/sdbus/sd_bus_internals_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ static PyObject* SdBusMessage_append_data(SdBusMessageObject* self, PyObject* co
PyErr_SetString(PyExc_TypeError, "Minimum 2 args required");
return NULL;
}
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);

const char* signature_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);

Expand Down Expand Up @@ -648,8 +648,8 @@ static PyObject* SdBusMessage_append_data(SdBusMessageObject* self, PyObject* ar
#ifndef Py_LIMITED_API
static PyObject* SdBusMessage_open_container(SdBusMessageObject* self, PyObject* const* args, Py_ssize_t nargs) {
SD_BUS_PY_CHECK_ARGS_NUMBER(2);
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);

const char* container_type_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
const char* container_contents_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);
Expand All @@ -673,8 +673,8 @@ static PyObject* SdBusMessage_close_container(SdBusMessageObject* self, PyObject
#ifndef Py_LIMITED_API
static PyObject* SdBusMessage_enter_container(SdBusMessageObject* self, PyObject* const* args, Py_ssize_t nargs) {
SD_BUS_PY_CHECK_ARGS_NUMBER(2);
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);

const char* container_type_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
const char* container_contents_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);
Expand Down Expand Up @@ -982,8 +982,8 @@ static PyObject* SdBusMessage_get_contents2(SdBusMessageObject* self, PyObject*
#ifndef Py_LIMITED_API
static SdBusMessageObject* SdBusMessage_create_error_reply(SdBusMessageObject* self, PyObject* const* args, Py_ssize_t nargs) {
SD_BUS_PY_CHECK_ARGS_NUMBER(2);
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);

const char* name = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
const char* error_message = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);
Expand Down
23 changes: 23 additions & 0 deletions test/test_sd_bus_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,3 +599,26 @@ async def test_singal_queue_wildcard_match(self) -> None:
message = await wait_for(message_queue.get(), timeout=1)
self.assertEqual(message.member,
test_object.test_signal.dbus_signal.signal_name)

async def test_class_with_string_subclass_parameter(self) -> None:
from enum import Enum

class InterfaceNameEnum(str, Enum):
FOO = 'org.example.foo'
BAR = 'org.example.bar'

class ObjectPathEnum(str, Enum):
FOO = '/foo'
BAR = '/bar'

class EnumedInterfaceAsync(
DbusInterfaceCommonAsync,
interface_name=InterfaceNameEnum.BAR,
):

@dbus_property_async('s')
def hello_world(self) -> str:
return 'Hello World!'

test_object = EnumedInterfaceAsync()
test_object.export_to_dbus(ObjectPathEnum.FOO)

1 comment on commit b1f28a6

@igo95862
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wes8ty You should now be able to use string enums anywhere on fast API.

Please sign in to comment.