Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9055001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:50:09+00:00 2026-06-16T13:50:09+00:00

I’m trying to dynamically add signals to a D-Bus service using dbus-python. It provides

  • 0

I’m trying to dynamically add signals to a D-Bus service using dbus-python. It provides a decorator for this that works fine if the signal names are known at module load time; however, I don’t know what name to export to D-Bus until runtime.

To illustrate the problem, what I’d like to do is the moral equivalent of:

import dbus
import dbus.service
import gobject
from dbus.mainloop.glib import DBusGMainLoop

class Event(dbus.service.Object):
    def __init__(self, name):
        self.name = name
        self.busName = dbus.service.BusName('com.acme.EventManager',
                                            bus=dbus.SessionBus())
        dbus.service.Object.__init__(self,
                                     self.busName,
                                     '/com/acme/EventManager/' +
                                     self.name)

        self.signame = 'com.acme.EventManager.' + self.name

    # THIS DOES NOT WORK: this decorator is parsed before the Event
    # class, and 'self' wouldn't exist here, anyway...
    @dbus.service.signal(dbus_interface=self.signame, signature='v')
    def emit(self, data):
        print "In %s event, got: %s " % (self.name, data)

if __name__ == "__main__":
    DBusGMainLoop(set_as_default=True)
    bus = dbus.SessionBus()
    loop = gobject.MainLoop()
    connect = Event('Connect')
    disconnect = Event('Disconnect')
    loop.run()

Not surprisingly, this generates:

@dbus.service.signal(dbus_interface=self.signame, signature='v')
NameError: name 'self' is not defined

I thought I could just dispense with the syntactic sugar provided by the @ decoration operator and patch Event manually after the class has been defined, something like this:

import dbus
import dbus.service
import gobject
from dbus.mainloop.glib import DBusGMainLoop

class Event(dbus.service.Object):
    def __init__(self, name):
        self.name = name
        self.busName = dbus.service.BusName('com.acme.EventManager',
                                            bus=dbus.SessionBus())
        dbus.service.Object.__init__(self,
                                     self.busName,
                                     '/com/acme/EventManager/' +
                                     self.name)

        self.signame = 'com.acme.EventManager.' + self.name

    def emit(self, data):
        print "In %s event, got: %s " % (self.name, data)

if __name__ == "__main__":
    DBusGMainLoop(set_as_default=True)
    bus = dbus.SessionBus()
    loop = gobject.MainLoop()
    e1 = Event('Connect')
    e1.emit = dbus.service.signal(dbus_interface=e1.signame,
                              signature='v')(e1.emit)
    loop.run()

This runs without errors, but it fails to export the signals to D-Bus. When I run D-Feet, I see the object path /com/acme/EventManager/Connect but it has no interface methods aside from Introspect().

To see if I could learn something about what was going on, I examined the value of the function passed to the dbus.service.signal decorator in a debugger. For this typical use case:

@dbus.service.signal(dbus_interface='com.acme.foo', signature='v')
def emit(self, data):
    pass

the function passed in to the decorator (in the variable func) looks like this:

>>> func
>>> <function emit at 0x99fbed4>

But when I manually invoke the decorator function (as in the the e1.emit = assignment in the second example above), I see:

>>> func
>>> <bound method Event.emit of <__main__.Event at /com/acme/EventManager/Connect at 0x9b3348c>>

So… it seems that, for the normal use case, dbus.service.signal expects to receive a free function—not an unbound function, but a function that, for all intents and purposes, looks like it was defined using:

def emit():
    pass

This behavior is utterly mystifying to me. I’ve read lots of tutorials on decorators, and thought I understood them pretty well, but I have been stumped on this for hours. If this decorator expects to be called with ‘raw’ function, how can I convert an object method such that I can invoke it manually?

From this question, I see that types.MethodType() can be used to convert a free function to a bound method. But it seems I need to do the opposite(?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:50:11+00:00Added an answer on June 16, 2026 at 1:50 pm

    I think one way to do what you want is by using a factory function — however the following is untested because I don’t have the D-Bus module installed.

    The root of the problem is you’re attempting to use a decorator at class definition time that requires data which isn’t being provided until instances of that class are created. One workaround for that is to define the class inside a function and use closures so the data is available when needed. Note that the factory function returns instances of the class created, not the class itself, although it could if desired.

    import dbus
    import dbus.service
    import gobject
    from dbus.mainloop.glib import DBusGMainLoop
    
    def event_factory(event_name):
        class Event(dbus.service.Object):
            def __init__(self):
                self.busName = dbus.service.BusName('com.acme.EventManager',
                                                    bus=dbus.SessionBus())
                dbus.service.Object.__init__(self,
                                             self.busName,
                                             '/com/acme/EventManager/'+event_name)
    
            @dbus.service.signal(dbus_interface='com.acme.EventManager.'+event_name,
                                 signature='v')
            def emit(self, data):
                print "In %s event, got: %s " % (event_name, data)
    
        return Event() # return an instance of the class
    
    if __name__ == "__main__":
        DBusGMainLoop(set_as_default=True)
        bus = dbus.SessionBus()
        loop = gobject.MainLoop()
        connect = event_factory('Connect')
        disconnect = event_factory('Disconnect')
        loop.run()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.