From 6ff89bf0d05a550cf1aff2053579fc1f0192cd97 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 5 Jun 2012 19:07:51 +0100 Subject: Py3: correctly guess the signature of ObjectPath(...) and Signature(...) Under Python 2, ObjectPath and Signature are subtypes of str (= bytes), and the existing type-guessing worked. The type-guessing code assumed that all unicode objects were just strings, but that assumption became false in the Python 3 port: ObjectPath and Signature are still subtypes of str, but str now means unicode, not bytes. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=50740 --- _dbus_bindings/message-append.c | 14 ++++++++++++-- test/test-standalone.py | 2 ++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/_dbus_bindings/message-append.c b/_dbus_bindings/message-append.c index f4f843b..df3190d 100644 --- a/_dbus_bindings/message-append.c +++ b/_dbus_bindings/message-append.c @@ -249,8 +249,16 @@ _signature_string_from_pyobject(PyObject *obj, long *variant_level_ptr) return NATIVESTR_FROMSTR(DBUS_TYPE_INT64_AS_STRING); } #endif /* PY3 */ - else if (PyUnicode_Check(obj)) - return NATIVESTR_FROMSTR(DBUS_TYPE_STRING_AS_STRING); + else if (PyUnicode_Check(obj)) { + /* Object paths and signatures are unicode subtypes in Python 3 + * (the first two cases will never be true in Python 2) */ + if (DBusPyObjectPath_Check(obj)) + return NATIVESTR_FROMSTR(DBUS_TYPE_OBJECT_PATH_AS_STRING); + else if (DBusPySignature_Check(obj)) + return NATIVESTR_FROMSTR(DBUS_TYPE_SIGNATURE_AS_STRING); + else + return NATIVESTR_FROMSTR(DBUS_TYPE_STRING_AS_STRING); + } #if defined(DBUS_TYPE_UNIX_FD) else if (DBusPyUnixFd_Check(obj)) return NATIVESTR_FROMSTR(DBUS_TYPE_UNIX_FD_AS_STRING); @@ -266,6 +274,8 @@ _signature_string_from_pyobject(PyObject *obj, long *variant_level_ptr) return NATIVESTR_FROMSTR(DBUS_TYPE_DOUBLE_AS_STRING); } else if (PyBytes_Check(obj)) { + /* Object paths and signatures are bytes subtypes in Python 2 + * (the first two cases will never be true in Python 3) */ if (DBusPyObjectPath_Check(obj)) return NATIVESTR_FROMSTR(DBUS_TYPE_OBJECT_PATH_AS_STRING); else if (DBusPySignature_Check(obj)) diff --git a/test/test-standalone.py b/test/test-standalone.py index 95b636a..6f403ee 100755 --- a/test/test-standalone.py +++ b/test/test-standalone.py @@ -322,6 +322,8 @@ class TestMessageMarshalling(unittest.TestCase): aeq(Message.guess_signature(('a',)), '(s)') aeq(Message.guess_signature(['a']), 'as') aeq(Message.guess_signature({'a':'b'}), 'a{ss}') + aeq(Message.guess_signature(types.ObjectPath('/')), 'o') + aeq(Message.guess_signature(types.Signature('x')), 'g') def test_guess_signature_python_ints(self): aeq = self.assertEqual -- cgit v1.2.1