diff options
author | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2012-06-05 19:07:51 +0100 |
---|---|---|
committer | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2012-06-05 19:07:51 +0100 |
commit | 6ff89bf0d05a550cf1aff2053579fc1f0192cd97 (patch) | |
tree | 31481b1b8d209d5b1f43af242c2d556a9cfcfed0 | |
parent | 2f08b9e644c8530c4d324c4bd9d8a0cdb24edc92 (diff) | |
download | dbus-python-6ff89bf0d05a550cf1aff2053579fc1f0192cd97.tar.gz |
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
-rw-r--r-- | _dbus_bindings/message-append.c | 14 | ||||
-rwxr-xr-x | 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 |