diff options
author | Barry Warsaw <barry@python.org> | 2011-12-16 17:07:07 -0500 |
---|---|---|
committer | Barry Warsaw <barry@python.org> | 2011-12-16 17:07:07 -0500 |
commit | f8dab5af0bef5d26a51df41a564a5285c16a9cb5 (patch) | |
tree | 9d831ed9d22850f573d4d305365c09f798bcbece /dbus | |
parent | 2167b305db78b8345b0f5ea23bfa445fa7cfca4d (diff) |
More Python 3 porting, this time primarily to get test-client.py working.
Changes include:
- DBusException.get_dbus_message(): In Python 3, the str of the exception will
already be a unicode, so don't try to decode it unless it's a bytes object
(a.k.a. 8-bit str in Python 2).
- gobject_service.py: Switch to pygi and rewrite the metaclass instantiation
code to be portable between Python 2 and Python 3.
- run-test.sh: echo a few more useful environment variables
- test-client.py:
- Globally replace deprecated assertEquals with assertEqual
- Globally replace deprecated assert_ with assertTrue
- Use bytes objects for both 'ay' signatured methods on the server
- AcceptUnicodeString will return a native unicode, i.e. a str in Python 3
and a unicode in Python 2. Python 3 has no `unicode` built-in.
- Reformat some long lines for debugging.
- test-service.py:
- Open the log file in 'a' mode for easier tailing.
- AcceptUnicodeString will return a native unicode, i.e. a str in Python 3
and a unicode in Python 2. Python 3 has no `unicode` built-in.
- reformat some long lines for debugging.
- Put module-scope code into a main() function and add a bunch of logger
output for better debugging. `session_bus` must still be global though.
Wrap main() in a bit try/except to log all top-level exceptions.
Diffstat (limited to 'dbus')
-rw-r--r-- | dbus/exceptions.py | 5 | ||||
-rw-r--r-- | dbus/gobject_service.py | 76 |
2 files changed, 48 insertions, 33 deletions
diff --git a/dbus/exceptions.py b/dbus/exceptions.py index 8d84a29..5283369 100644 --- a/dbus/exceptions.py +++ b/dbus/exceptions.py @@ -27,6 +27,7 @@ __all__ = ('DBusException', 'MissingErrorHandlerException', 'IntrospectionParserException', 'UnknownMethodException', 'NameExistsException') + class DBusException(Exception): include_traceback = False @@ -57,7 +58,9 @@ class DBusException(Exception): def get_dbus_message(self): s = Exception.__str__(self) - return s.decode('utf-8', 'replace') + if isinstance(s, bytes): + return s.decode('utf-8', 'replace') + return s def get_dbus_name(self): return self._dbus_error_name diff --git a/dbus/gobject_service.py b/dbus/gobject_service.py index 61a7749..47a2b5f 100644 --- a/dbus/gobject_service.py +++ b/dbus/gobject_service.py @@ -22,9 +22,21 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. -import gobject +__all__ = ['ExportedGObject'] + +from gi.repository import GObject as gobject import dbus.service +# The odd syntax used here is required so that the code is compatible with +# both Python 2 and Python 3. It essentially creates a new class called +# ExportedGObject with a metaclass of ExportGObjectType and an __init__() +# function. +# +# Because GObject and `dbus.service.Object` both have custom metaclasses, the +# naive approach using simple multiple inheritance won't work. This class has +# `ExportedGObjectType` as its metaclass, which is sufficient to make it work +# correctly. + class ExportedGObjectType(gobject.GObjectMeta, dbus.service.InterfaceType): """A metaclass which inherits from both GObjectMeta and `dbus.service.InterfaceType`. Used as the metaclass for `ExportedGObject`. @@ -33,39 +45,39 @@ class ExportedGObjectType(gobject.GObjectMeta, dbus.service.InterfaceType): gobject.GObjectMeta.__init__(cls, name, bases, dct) dbus.service.InterfaceType.__init__(cls, name, bases, dct) -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, - the naive approach using simple multiple inheritance won't work. This - class has `ExportedGObjectType` as its metaclass, which is sufficient - to make it work correctly. - """ - __metaclass__ = ExportedGObjectType +def ExportedGObject__init__(self, conn=None, object_path=None, **kwargs): + """Initialize an exported GObject. + + :Parameters: + `conn` : dbus.connection.Connection + The D-Bus connection or bus + `object_path` : str + The object path at which to register this object. + :Keywords: + `bus_name` : dbus.service.BusName + A bus name to be held on behalf of this object, or None. + `gobject_properties` : dict + GObject properties to be set on the constructed object. - def __init__(self, conn=None, object_path=None, **kwargs): - """Initialize an exported GObject. + Any unrecognised keyword arguments will also be interpreted + as GObject properties. + """ + bus_name = kwargs.pop('bus_name', None) + gobject_properties = kwargs.pop('gobject_properties', None) - :Parameters: - `conn` : dbus.connection.Connection - The D-Bus connection or bus - `object_path` : str - The object path at which to register this object. - :Keywords: - `bus_name` : dbus.service.BusName - A bus name to be held on behalf of this object, or None. - `gobject_properties` : dict - GObject properties to be set on the constructed object. + if gobject_properties is not None: + kwargs.update(gobject_properties) + gobject.GObject.__init__(self, **kwargs) + dbus.service.Object.__init__(self, conn=conn, + object_path=object_path, + bus_name=bus_name) - Any unrecognised keyword arguments will also be interpreted - as GObject properties. - """ - bus_name = kwargs.pop('bus_name', None) - gobject_properties = kwargs.pop('gobject_properties', None) +ExportedGObject__doc__ = 'A GObject which is exported on the D-Bus.' - if gobject_properties is not None: - kwargs.update(gobject_properties) - gobject.GObject.__init__(self, **kwargs) - dbus.service.Object.__init__(self, conn=conn, - object_path=object_path, - bus_name=bus_name) +ExportedGObject = ExportedGObjectType( + 'ExportedGObject', + (gobject.GObject, dbus.service.Object), + {'__init__': ExportedGObject__init__, + '__doc__': ExportedGObject__doc__, + }) |