[patch][python] When an exception is raised by a signal handler, print it to stderr

John (J5) Palmieri johnp at redhat.com
Mon Feb 19 07:04:12 PST 2007


Looks good.  Please commit.

On Mon, 2007-02-19 at 14:23 +0000, Simon McVittie wrote:
> This partially fixes a regression since 0.7x.
> 
> There need to be more checks for exceptions so they all at least get printed
> to stderr - a lot of asynchronous code runs in slightly precarious
> circumstances, so we can't necessarily provide full tracebacks, but it's
> better than nothing. Places I can think of which may need exception
> printing added:
> 
> - asynchronous method-call reply and error handlers
> - the message filter mechanism (with which we implement signal handling,
>   but this patch should be merged first anyway, since it's the most
>   common case and the exception should be caught before it can disrupt
>   other signal handlers)
> 
> >From 4f88700588d80861c8520e56407d3a40d5bd86e6 Mon Sep 17 00:00:00 2001
> From: Simon McVittie <simon.mcvittie at collabora.co.uk>
> Date: Mon, 19 Feb 2007 14:17:08 +0000
> Subject: [PATCH] dbus/_dbus.py: When an exception is raised by a signal handler, print it to stderr
> 
> ---
>  dbus/_dbus.py |   54 ++++++++++++++++++++++++++++++++----------------------
>  1 files changed, 32 insertions(+), 22 deletions(-)
> 
> diff --git a/dbus/_dbus.py b/dbus/_dbus.py
> index 21ed122..8e3b536 100644
> --- a/dbus/_dbus.py
> +++ b/dbus/_dbus.py
> @@ -33,7 +33,9 @@ BusImplementation = _dbus_bindings.BusImplementation
>  
>  import os
>  import logging
> +import sys
>  import weakref
> +from traceback import print_exc
>  
>  from dbus.proxies import ProxyObject, BUS_DAEMON_NAME, BUS_DAEMON_PATH, \
>          BUS_DAEMON_IFACE
> @@ -187,28 +189,36 @@ class SignalMatch(object):
>              return False
>  
>          #logger.debug('%r: yes, I want to handle that signal', self)
> -        # minor optimization: if we already extracted the args with the right
> -        # calling convention to do the args match, don't bother doing so again
> -        if args is None or not self._utf8_strings or not self._byte_arrays:
> -            args = message.get_args_list(utf8_strings=self._utf8_strings,
> -                                         byte_arrays=self._byte_arrays)
> -        #logger.debug('%r: extracted signal arguments', self)
> -        kwargs = {}
> -        if self._sender_keyword is not None:
> -            kwargs[self._sender_keyword] = message.get_sender()
> -        if self._destination_keyword is not None:
> -            kwargs[self._destination_keyword] = message.get_destination()
> -        if self._path_keyword is not None:
> -            kwargs[self._path_keyword] = message.get_path()
> -        if self._member_keyword is not None:
> -            kwargs[self._member_keyword] = message.get_member()
> -        if self._interface_keyword is not None:
> -            kwargs[self._interface_keyword] = message.get_interface()
> -        if self._message_keyword is not None:
> -            kwargs[self._message_keyword] = message
> -        #logger.debug('%r: calling handler with %r and %r', self, args, kwargs)
> -        self._handler(*args, **kwargs)
> -        #logger.debug('%r: signal handled', self)
> +
> +        try:
> +            # minor optimization: if we already extracted the args with the right
> +            # calling convention to do the args match, don't bother doing so again
> +            if args is None or not self._utf8_strings or not self._byte_arrays:
> +                args = message.get_args_list(utf8_strings=self._utf8_strings,
> +                                             byte_arrays=self._byte_arrays)
> +            #logger.debug('%r: extracted signal arguments', self)
> +            kwargs = {}
> +            if self._sender_keyword is not None:
> +                kwargs[self._sender_keyword] = message.get_sender()
> +            if self._destination_keyword is not None:
> +                kwargs[self._destination_keyword] = message.get_destination()
> +            if self._path_keyword is not None:
> +                kwargs[self._path_keyword] = message.get_path()
> +            if self._member_keyword is not None:
> +                kwargs[self._member_keyword] = message.get_member()
> +            if self._interface_keyword is not None:
> +                kwargs[self._interface_keyword] = message.get_interface()
> +            if self._message_keyword is not None:
> +                kwargs[self._message_keyword] = message
> +            #logger.debug('%r: calling handler with %r and %r', self, args, kwargs)
> +            self._handler(*args, **kwargs)
> +            #logger.debug('%r: signal handled', self)
> +        except:
> +            # FIXME: need to decide whether dbus-python uses logging, or
> +            # stderr, or what, and make it consistent
> +            sys.stderr.write('Exception in handler for D-Bus signal:\n')
> +            print_exc()
> +
>          return True
>  
>      def remove(self):
-- 
John (J5) Palmieri <johnp at redhat.com>



More information about the dbus mailing list