Skip to content

Commit b853432

Browse files
committed
Iterate interfaces in the MRO reversed order
Iterating the MRO in the reversed order will allow to add interfaces to the given object in a well-defined way - always starting from the base class. Such ordering will be compatible with adding interfaces manually one by one.
1 parent b7b9c5c commit b853432

File tree

6 files changed

+37
-8
lines changed

6 files changed

+37
-8
lines changed

src/sdbus/dbus_proxy_async_interface_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def _dbus_iter_interfaces_meta(
295295
cls,
296296
) -> Iterator[tuple[str, DbusClassMeta]]:
297297

298-
for base in cls.__mro__:
298+
for base in reversed(cls.__mro__):
299299
meta = DBUS_CLASS_TO_META.get(base)
300300
if meta is None:
301301
continue

src/sdbus/dbus_proxy_async_interfaces.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ async def properties_get_all_dict(
105105

106106

107107
class DbusInterfaceCommonAsync(
108-
DbusPeerInterfaceAsync, DbusPropertiesInterfaceAsync,
109-
DbusIntrospectableAsync):
108+
DbusPropertiesInterfaceAsync,
109+
DbusIntrospectableAsync,
110+
DbusPeerInterfaceAsync):
110111
...

src/sdbus/dbus_proxy_sync_interface_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def _dbus_iter_interfaces_meta(
171171
cls,
172172
) -> Iterator[tuple[str, DbusClassMeta]]:
173173

174-
for base in cls.__mro__:
174+
for base in reversed(cls.__mro__):
175175
meta = DBUS_CLASS_TO_META.get(base)
176176
if meta is None:
177177
continue

src/sdbus/dbus_proxy_sync_interfaces.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ def properties_get_all_dict(
9494

9595

9696
class DbusInterfaceCommon(
97-
DbusPeerInterface,
97+
DbusPropertiesInterface,
9898
DbusIntrospectable,
99-
DbusPropertiesInterface):
99+
DbusPeerInterface):
100100
...
101101

102102

test/test_sdbus_async.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,9 +948,23 @@ class TwoInterface(
948948
async def two(self) -> int:
949949
return 2
950950

951-
class CombinedInterface(OneInterface, TwoInterface):
951+
class CombinedInterface(TwoInterface, OneInterface):
952952
...
953953

954+
test_combined = CombinedInterface()
955+
test_combined_interfaces = [
956+
iface for iface, _ in test_combined._dbus_iter_interfaces_meta()
957+
]
958+
959+
# Verify the order of reported interfaces on the combined class.
960+
self.assertEqual(test_combined_interfaces, [
961+
"org.freedesktop.DBus.Peer",
962+
"org.freedesktop.DBus.Introspectable",
963+
"org.freedesktop.DBus.Properties",
964+
"org.example.one",
965+
"org.example.two",
966+
])
967+
954968
async def test_extremely_large_string(self) -> None:
955969
test_object, test_object_connection = initialize_object()
956970

test/test_sdbus_block.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,23 @@ class TwoInterface(
9292
def two(self) -> int:
9393
raise NotImplementedError
9494

95-
class CombinedInterface(OneInterface, TwoInterface):
95+
class CombinedInterface(TwoInterface, OneInterface):
9696
...
9797

98+
test_combined = CombinedInterface("org.test", "/")
99+
test_combined_interfaces = [
100+
iface for iface, _ in test_combined._dbus_iter_interfaces_meta()
101+
]
102+
103+
# Verify the order of reported interfaces on the combined class.
104+
self.assertEqual(test_combined_interfaces, [
105+
"org.freedesktop.DBus.Peer",
106+
"org.freedesktop.DBus.Introspectable",
107+
"org.freedesktop.DBus.Properties",
108+
"org.example.one",
109+
"org.example.two",
110+
])
111+
98112

99113
if __name__ == '__main__':
100114
main()

0 commit comments

Comments
 (0)