From e6d5bb0209c9cba4d42f12a448bd708a2cabaa9f Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 3 May 2007 12:11:31 +0100 Subject: [PATCH] dbus/gobject_service.py: Make ExportedGObject work correctly. Also add a simple unit test for it. --- dbus/Makefile.am | 1 + dbus/gobject_service.py | 13 +++++++++++-- test/test-client.py | 10 ++++++++++ test/test-service.py | 11 +++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/dbus/Makefile.am b/dbus/Makefile.am index 271c899..fc30f53 100644 --- a/dbus/Makefile.am +++ b/dbus/Makefile.am @@ -9,6 +9,7 @@ nobase_pythondbus_PYTHON = bus.py \ exceptions.py \ _expat_introspect_parser.py \ glib.py \ + gobject_service.py \ __init__.py \ lowlevel.py \ mainloop/__init__.py \ diff --git a/dbus/gobject_service.py b/dbus/gobject_service.py index 2a9fbd8..9b94f93 100644 --- a/dbus/gobject_service.py +++ b/dbus/gobject_service.py @@ -21,12 +21,15 @@ import gobject import dbus.service -class ExportedGObjectType(dbus.service.InterfaceType, gobject.GObjectMeta): +class ExportedGObjectType(gobject.GObjectMeta, dbus.service.InterfaceType): """A metaclass which inherits from both GObjectMeta and `dbus.service.InterfaceType`. Used as the metaclass for `ExportedGObject`. """ + def __init__(cls, name, bases, dct): + gobject.GObjectMeta.__init__(cls, name, bases, dct) + dbus.service.InterfaceType.__init__(cls, name, bases, dct) -class ExportedGObject(dbus.service.Object, gobject.GObject): +class ExportedGObject(gobject.GObject, dbus.service.Object): """A GObject which is exported on the D-Bus. Because GObject and `dbus.service.Object` both have custom metaclasses, @@ -35,3 +38,9 @@ class ExportedGObject(dbus.service.Object, gobject.GObject): to make it work correctly. """ __metaclass__ = ExportedGObjectType + + def __init__(self, conn=None, object_path=None, bus_name=None): + gobject.GObject.__init__(self) + dbus.service.Object.__init__(self, conn=conn, + object_path=object_path, + bus_name=bus_name) diff --git a/test/test-client.py b/test/test-client.py index b9d2242..04706df 100755 --- a/test/test-client.py +++ b/test/test-client.py @@ -70,6 +70,16 @@ class TestDBusBindings(unittest.TestCase): self.remote_object = self.bus.get_object(NAME, OBJECT) self.iface = dbus.Interface(self.remote_object, IFACE) + def testGObject(self): + print "Testing ExportedGObject... ", + remote_gobject = self.bus.get_object(NAME, OBJECT + '/GObject') + iface = dbus.Interface(remote_gobject, IFACE) + print "introspection, ", + remote_gobject.Introspect(dbus_interface=dbus.INTROSPECTABLE_IFACE) + print "method call, ", + self.assertEquals(iface.Echo('123'), '123') + print "... OK" + def testWeakRefs(self): # regression test for Sugar crash caused by smcv getting weak refs # wrong - pre-bugfix, this would segfault diff --git a/test/test-service.py b/test/test-service.py index 6c6880a..2ef1e22 100755 --- a/test/test-service.py +++ b/test/test-service.py @@ -36,6 +36,8 @@ import dbus.glib import gobject import random +from dbus.gobject_service import ExportedGObject + logging.basicConfig(filename=builddir + '/test/test-service.log', filemode='w') logging.getLogger().setLevel(1) @@ -46,6 +48,14 @@ NAME = "org.freedesktop.DBus.TestSuitePythonService" IFACE = "org.freedesktop.DBus.TestSuiteInterface" OBJECT = "/org/freedesktop/DBus/TestSuitePythonObject" +class TestGObject(ExportedGObject): + def __init__(self, bus_name, object_path=OBJECT + '/GObject'): + super(TestGObject, self).__init__(bus_name, object_path) + + @dbus.service.method(IFACE) + def Echo(self, arg): + return arg + class TestInterface(dbus.service.Interface): @dbus.service.method(IFACE, in_signature='', out_signature='b') def CheckInheritance(self): @@ -200,5 +210,6 @@ class TestObject(dbus.service.Object, TestInterface): session_bus = dbus.SessionBus() name = dbus.service.BusName(NAME, bus=session_bus) object = TestObject(name) +g_object = TestGObject(name) loop = gobject.MainLoop() loop.run() -- 2.11.4.GIT