Skip to content

Commit b1f28a6

Browse files
committed
Fixed str and int subclasses not working on fast API
1 parent 1a8ab05 commit b1f28a6

File tree

6 files changed

+64
-47
lines changed

6 files changed

+64
-47
lines changed

src/sdbus/sd_bus_internals.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@
3131
return NULL; \
3232
}
3333

34-
#define SD_BUS_PY_CHECK_ARG_TYPE(arg_num, arg_expected_type) \
35-
if (Py_TYPE(args[arg_num]) != &arg_expected_type) { \
36-
PyErr_SetString(PyExc_TypeError, "Argument is not an " #arg_expected_type ""); \
37-
return NULL; \
38-
}
39-
4034
#define SD_BUS_PY_CHECK_ARG_CHECK_FUNC(arg_num, arg_check_function) \
4135
if (!arg_check_function(args[arg_num])) { \
4236
PyErr_SetString(PyExc_TypeError, "Argument failed a " #arg_check_function " check"); \

src/sdbus/sd_bus_internals_bus.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ static int SdBus_init(SdBusObject* self, PyObject* Py_UNUSED(args), PyObject* Py
3636
#ifndef Py_LIMITED_API
3737
static SdBusMessageObject* SdBus_new_method_call_message(SdBusObject* self, PyObject* const* args, Py_ssize_t nargs) {
3838
SD_BUS_PY_CHECK_ARGS_NUMBER(4);
39-
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
40-
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
41-
SD_BUS_PY_CHECK_ARG_TYPE(2, PyUnicode_Type);
42-
SD_BUS_PY_CHECK_ARG_TYPE(3, PyUnicode_Type);
39+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
40+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
41+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(2, PyUnicode_Check);
42+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(3, PyUnicode_Check);
4343

4444
const char* destination_bus_name = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
4545
const char* object_path = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);
@@ -66,10 +66,10 @@ static SdBusMessageObject* SdBus_new_method_call_message(SdBusObject* self, PyOb
6666
#ifndef Py_LIMITED_API
6767
static SdBusMessageObject* SdBus_new_property_get_message(SdBusObject* self, PyObject* const* args, Py_ssize_t nargs) {
6868
SD_BUS_PY_CHECK_ARGS_NUMBER(4);
69-
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
70-
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
71-
SD_BUS_PY_CHECK_ARG_TYPE(2, PyUnicode_Type);
72-
SD_BUS_PY_CHECK_ARG_TYPE(3, PyUnicode_Type);
69+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
70+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
71+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(2, PyUnicode_Check);
72+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(3, PyUnicode_Check);
7373

7474
const char* destination_service_name = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
7575
const char* object_path = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);
@@ -99,10 +99,10 @@ static SdBusMessageObject* SdBus_new_property_get_message(SdBusObject* self, PyO
9999
#ifndef Py_LIMITED_API
100100
static SdBusMessageObject* SdBus_new_property_set_message(SdBusObject* self, PyObject* const* args, Py_ssize_t nargs) {
101101
SD_BUS_PY_CHECK_ARGS_NUMBER(4);
102-
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
103-
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
104-
SD_BUS_PY_CHECK_ARG_TYPE(2, PyUnicode_Type);
105-
SD_BUS_PY_CHECK_ARG_TYPE(3, PyUnicode_Type);
102+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
103+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
104+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(2, PyUnicode_Check);
105+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(3, PyUnicode_Check);
106106

107107
const char* destination_service_name = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
108108
const char* object_path = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);
@@ -132,9 +132,9 @@ static SdBusMessageObject* SdBus_new_property_set_message(SdBusObject* self, PyO
132132
#ifndef Py_LIMITED_API
133133
static SdBusMessageObject* SdBus_new_signal_message(SdBusObject* self, PyObject* const* args, Py_ssize_t nargs) {
134134
SD_BUS_PY_CHECK_ARGS_NUMBER(3);
135-
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type); // Path
136-
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type); // Interface
137-
SD_BUS_PY_CHECK_ARG_TYPE(2, PyUnicode_Type); // Member
135+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check); // Path
136+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check); // Interface
137+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(2, PyUnicode_Check); // Member
138138

139139
const char* object_path = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
140140
const char* interface_name = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);
@@ -352,8 +352,8 @@ static int _check_is_sdbus_interface(PyObject* type_to_check) {
352352
static PyObject* SdBus_add_interface(SdBusObject* self, PyObject* const* args, Py_ssize_t nargs) {
353353
SD_BUS_PY_CHECK_ARGS_NUMBER(3);
354354
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, _check_is_sdbus_interface);
355-
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
356-
SD_BUS_PY_CHECK_ARG_TYPE(2, PyUnicode_Type);
355+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
356+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(2, PyUnicode_Check);
357357

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

503503
const char* service_name_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
504504
uint64_t flags = PyLong_AsUnsignedLongLong(args[1]);
@@ -529,8 +529,8 @@ static PyObject* SdBus_request_name_async(SdBusObject* self, PyObject* args) {
529529
#ifndef Py_LIMITED_API
530530
static PyObject* SdBus_request_name(SdBusObject* self, PyObject* const* args, Py_ssize_t nargs) {
531531
SD_BUS_PY_CHECK_ARGS_NUMBER(2);
532-
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
533-
SD_BUS_PY_CHECK_ARG_TYPE(1, PyLong_Type);
532+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
533+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyLong_Check);
534534

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

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

575575
const char* added_object_path = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
576576
#else

src/sdbus/sd_bus_internals_funcs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ static SdBusObject* sd_bus_py_open_user_machine(PyObject* Py_UNUSED(self), PyObj
7373
#ifndef Py_LIMITED_API
7474
static PyObject* encode_object_path(PyObject* Py_UNUSED(self), PyObject* const* args, Py_ssize_t nargs) {
7575
SD_BUS_PY_CHECK_ARGS_NUMBER(2);
76-
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
77-
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
76+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
77+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
7878

7979
const char* prefix_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
8080
const char* external_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);
@@ -106,8 +106,8 @@ static PyObject* encode_object_path(PyObject* Py_UNUSED(self), PyObject* args) {
106106
#ifndef Py_LIMITED_API
107107
static PyObject* decode_object_path(PyObject* Py_UNUSED(self), PyObject* const* args, Py_ssize_t nargs) {
108108
SD_BUS_PY_CHECK_ARGS_NUMBER(2);
109-
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
110-
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
109+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
110+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
111111

112112
const char* prefix_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
113113
const char* full_path_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);

src/sdbus/sd_bus_internals_interface.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ static PyObject* SdBusInterface_add_property(SdBusInterfaceObject* self, PyObjec
5858
// Arguments
5959
// Name, Signature, Get, Set, Flags
6060
SD_BUS_PY_CHECK_ARGS_NUMBER(5);
61-
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
62-
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
61+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
62+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
6363
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(2, PyCallable_Check);
6464
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(3, _check_callable_or_none);
6565
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(4, PyLong_Check);
@@ -96,10 +96,10 @@ static PyObject* SdBusInterface_add_method(SdBusInterfaceObject* self, PyObject*
9696
// Method name, signature, names of input values, result signature,
9797
// names of result values, flags, callback function or coroutine
9898
SD_BUS_PY_CHECK_ARGS_NUMBER(7);
99-
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
100-
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
99+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
100+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
101101
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(2, PySequence_Check);
102-
SD_BUS_PY_CHECK_ARG_TYPE(3, PyUnicode_Type);
102+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(3, PyUnicode_Check);
103103
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(4, PySequence_Check);
104104
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(5, PyLong_Check);
105105
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(6, PyCallable_Check);
@@ -153,8 +153,8 @@ static PyObject* SdBusInterface_add_signal(SdBusInterfaceObject* self, PyObject*
153153
// Arguments
154154
// Signal name, signature, names of input values, flags
155155
SD_BUS_PY_CHECK_ARGS_NUMBER(4);
156-
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
157-
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
156+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
157+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
158158
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(2, PySequence_Check);
159159
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(3, PyLong_Check);
160160

src/sdbus/sd_bus_internals_message.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ static PyObject* SdBusMessage_append_data(SdBusMessageObject* self, PyObject* co
606606
PyErr_SetString(PyExc_TypeError, "Minimum 2 args required");
607607
return NULL;
608608
}
609-
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
609+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
610610

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

@@ -648,8 +648,8 @@ static PyObject* SdBusMessage_append_data(SdBusMessageObject* self, PyObject* ar
648648
#ifndef Py_LIMITED_API
649649
static PyObject* SdBusMessage_open_container(SdBusMessageObject* self, PyObject* const* args, Py_ssize_t nargs) {
650650
SD_BUS_PY_CHECK_ARGS_NUMBER(2);
651-
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
652-
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
651+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
652+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
653653

654654
const char* container_type_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
655655
const char* container_contents_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);
@@ -673,8 +673,8 @@ static PyObject* SdBusMessage_close_container(SdBusMessageObject* self, PyObject
673673
#ifndef Py_LIMITED_API
674674
static PyObject* SdBusMessage_enter_container(SdBusMessageObject* self, PyObject* const* args, Py_ssize_t nargs) {
675675
SD_BUS_PY_CHECK_ARGS_NUMBER(2);
676-
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
677-
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
676+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
677+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
678678

679679
const char* container_type_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
680680
const char* container_contents_char_ptr = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);
@@ -982,8 +982,8 @@ static PyObject* SdBusMessage_get_contents2(SdBusMessageObject* self, PyObject*
982982
#ifndef Py_LIMITED_API
983983
static SdBusMessageObject* SdBusMessage_create_error_reply(SdBusMessageObject* self, PyObject* const* args, Py_ssize_t nargs) {
984984
SD_BUS_PY_CHECK_ARGS_NUMBER(2);
985-
SD_BUS_PY_CHECK_ARG_TYPE(0, PyUnicode_Type);
986-
SD_BUS_PY_CHECK_ARG_TYPE(1, PyUnicode_Type);
985+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(0, PyUnicode_Check);
986+
SD_BUS_PY_CHECK_ARG_CHECK_FUNC(1, PyUnicode_Check);
987987

988988
const char* name = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[0]);
989989
const char* error_message = SD_BUS_PY_UNICODE_AS_CHAR_PTR(args[1]);

test/test_sd_bus_async.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,3 +599,26 @@ async def test_singal_queue_wildcard_match(self) -> None:
599599
message = await wait_for(message_queue.get(), timeout=1)
600600
self.assertEqual(message.member,
601601
test_object.test_signal.dbus_signal.signal_name)
602+
603+
async def test_class_with_string_subclass_parameter(self) -> None:
604+
from enum import Enum
605+
606+
class InterfaceNameEnum(str, Enum):
607+
FOO = 'org.example.foo'
608+
BAR = 'org.example.bar'
609+
610+
class ObjectPathEnum(str, Enum):
611+
FOO = '/foo'
612+
BAR = '/bar'
613+
614+
class EnumedInterfaceAsync(
615+
DbusInterfaceCommonAsync,
616+
interface_name=InterfaceNameEnum.BAR,
617+
):
618+
619+
@dbus_property_async('s')
620+
def hello_world(self) -> str:
621+
return 'Hello World!'
622+
623+
test_object = EnumedInterfaceAsync()
624+
test_object.export_to_dbus(ObjectPathEnum.FOO)

0 commit comments

Comments
 (0)