From 1d7a711c492f4bef56ae611d8758eff48252ac71 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 15 Nov 2011 16:07:57 +0000 Subject: [PATCH] Use DBusBasicValue instead of reinventing it, if dbus is new enough If we don't find it, continue to reinvent it, but move the reinvention to an internal header so it's at least the same in both files that want it. --- _dbus_bindings/Makefile.am | 1 + _dbus_bindings/compat-internal.h | 52 ++++++++++++++++++++++++++++++++++ _dbus_bindings/message-append.c | 56 ++++++++++++++++--------------------- _dbus_bindings/message-get-args.c | 56 ++++++++++++++---------------------- configure.ac | 5 +++ 5 files changed, 104 insertions(+), 66 deletions(-) create mode 100644 _dbus_bindings/compat-internal.h diff --git a/_dbus_bindings/Makefile.am b/_dbus_bindings/Makefile.am index 891b35d..771ccc6 100644 --- a/_dbus_bindings/Makefile.am +++ b/_dbus_bindings/Makefile.am @@ -7,6 +7,7 @@ _dbus_bindings_la_SOURCES = \ abstract.c \ bus.c \ bytes.c \ + compat-internal.h \ conn.c \ conn-internal.h \ conn-methods.c \ diff --git a/_dbus_bindings/compat-internal.h b/_dbus_bindings/compat-internal.h new file mode 100644 index 0000000..143f23a --- /dev/null +++ b/_dbus_bindings/compat-internal.h @@ -0,0 +1,52 @@ +/* Old D-Bus compatibility: implementation internals + * + * Copyright © 2006-2011 Collabora Ltd. + * Copyright © 2011 Nokia Corporation + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#ifndef DBUS_BINDINGS_COMPAT_INTERNAL_H +#define DBUS_BINDINGS_COMPAT_INTERNAL_H + +#include "config.h" +#include "dbus_bindings-internal.h" + +#ifndef HAVE_DBUSBASICVALUE +typedef union { + dbus_bool_t bool_val; + double dbl; + dbus_uint16_t u16; + dbus_int16_t i16; + dbus_uint32_t u32; + dbus_int32_t i32; +#if defined(DBUS_HAVE_INT64) && defined(HAVE_LONG_LONG) + dbus_uint64_t u64; + dbus_int64_t i64; +#endif + const char *str; + unsigned char byt; + float f; + int fd; +} DBusBasicValue; +#endif + +#endif diff --git a/_dbus_bindings/message-append.c b/_dbus_bindings/message-append.c index c2c4616..591dc30 100644 --- a/_dbus_bindings/message-append.c +++ b/_dbus_bindings/message-append.c @@ -29,6 +29,7 @@ #include #define DBG_IS_TOO_VERBOSE +#include "compat-internal.h" #include "types-internal.h" #include "message-internal.h" @@ -885,18 +886,7 @@ _message_iter_append_pyobject(DBusMessageIter *appender, dbus_bool_t *more) { int sig_type = dbus_signature_iter_get_current_type(sig_iter); - union { - dbus_bool_t b; - double d; - dbus_uint16_t uint16; - dbus_int16_t int16; - dbus_uint32_t uint32; - dbus_int32_t int32; -#if defined(DBUS_HAVE_INT64) && defined(HAVE_LONG_LONG) - dbus_uint64_t uint64; - dbus_int64_t int64; -#endif - } u; + DBusBasicValue u; int ret = -1; #ifdef USING_DBG @@ -912,13 +902,13 @@ _message_iter_append_pyobject(DBusMessageIter *appender, case DBUS_TYPE_BOOLEAN: if (PyObject_IsTrue(obj)) { - u.b = 1; + u.bool_val = 1; } else { - u.b = 0; + u.bool_val = 0; } - DBG("Performing actual append: bool(%ld)", (long)u.b); - if (!dbus_message_iter_append_basic(appender, sig_type, &u.b)) { + DBG("Performing actual append: bool(%ld)", (long)u.bool_val); + if (!dbus_message_iter_append_basic(appender, sig_type, &u.bool_val)) { PyErr_NoMemory(); ret = -1; break; @@ -927,13 +917,13 @@ _message_iter_append_pyobject(DBusMessageIter *appender, break; case DBUS_TYPE_DOUBLE: - u.d = PyFloat_AsDouble(obj); + u.dbl = PyFloat_AsDouble(obj); if (PyErr_Occurred()) { ret = -1; break; } - DBG("Performing actual append: double(%f)", u.d); - if (!dbus_message_iter_append_basic(appender, sig_type, &u.d)) { + DBG("Performing actual append: double(%f)", u.dbl); + if (!dbus_message_iter_append_basic(appender, sig_type, &u.dbl)) { PyErr_NoMemory(); ret = -1; break; @@ -943,12 +933,14 @@ _message_iter_append_pyobject(DBusMessageIter *appender, #ifdef WITH_DBUS_FLOAT32 case DBUS_TYPE_FLOAT: - u.d = PyFloat_AsDouble(obj); + u.dbl = PyFloat_AsDouble(obj); if (PyErr_Occurred()) { ret = -1; break; } - u.f = (float)u.d; + /* FIXME: DBusBasicValue will need to grow a float member if + * float32 becomes supported */ + u.f = (float)u.dbl; DBG("Performing actual append: float(%f)", u.f); if (!dbus_message_iter_append_basic(appender, sig_type, &u.f)) { PyErr_NoMemory(); @@ -961,14 +953,14 @@ _message_iter_append_pyobject(DBusMessageIter *appender, /* The integer types are all basically the same - we delegate to intNN_range_check() */ -#define PROCESS_INTEGER(size) \ - u.size = dbus_py_##size##_range_check(obj);\ - if (u.size == (dbus_##size##_t)(-1) && PyErr_Occurred()) {\ +#define PROCESS_INTEGER(size, member) \ + u.member = dbus_py_##size##_range_check(obj);\ + if (u.member == (dbus_##size##_t)(-1) && PyErr_Occurred()) {\ ret = -1; \ break; \ }\ - DBG("Performing actual append: " #size "(%lld)", (long long)u.size); \ - if (!dbus_message_iter_append_basic(appender, sig_type, &u.size)) {\ + DBG("Performing actual append: " #size "(%lld)", (long long)u.member); \ + if (!dbus_message_iter_append_basic(appender, sig_type, &u.member)) {\ PyErr_NoMemory();\ ret = -1;\ break;\ @@ -976,23 +968,23 @@ _message_iter_append_pyobject(DBusMessageIter *appender, ret = 0; case DBUS_TYPE_INT16: - PROCESS_INTEGER(int16) + PROCESS_INTEGER(int16, i16) break; case DBUS_TYPE_UINT16: - PROCESS_INTEGER(uint16) + PROCESS_INTEGER(uint16, u16) break; case DBUS_TYPE_INT32: - PROCESS_INTEGER(int32) + PROCESS_INTEGER(int32, i32) break; case DBUS_TYPE_UINT32: - PROCESS_INTEGER(uint32) + PROCESS_INTEGER(uint32, u32) break; #if defined(DBUS_HAVE_INT64) && defined(HAVE_LONG_LONG) case DBUS_TYPE_INT64: - PROCESS_INTEGER(int64) + PROCESS_INTEGER(int64, i64) break; case DBUS_TYPE_UINT64: - PROCESS_INTEGER(uint64) + PROCESS_INTEGER(uint64, u64) break; #else case DBUS_TYPE_INT64: diff --git a/_dbus_bindings/message-get-args.c b/_dbus_bindings/message-get-args.c index 5f3d89c..fa267c8 100644 --- a/_dbus_bindings/message-get-args.c +++ b/_dbus_bindings/message-get-args.c @@ -27,6 +27,7 @@ #define PY_SIZE_T_CLEAN 1 #define DBG_IS_TOO_VERBOSE +#include "compat-internal.h" #include "types-internal.h" #include "message-internal.h" @@ -186,22 +187,7 @@ _message_iter_get_pyobject(DBusMessageIter *iter, Message_get_args_options *opts, long variant_level) { - union { - const char *s; - unsigned char y; - dbus_bool_t b; - double d; - float f; - dbus_uint16_t u16; - dbus_int16_t i16; - dbus_uint32_t u32; - dbus_int32_t i32; -#if defined(DBUS_HAVE_INT64) && defined(HAVE_LONG_LONG) - dbus_uint64_t u64; - dbus_int64_t i64; -#endif - int fd; - } u; + DBusBasicValue u; int type = dbus_message_iter_get_arg_type(iter); PyObject *args = NULL; PyObject *kwargs = NULL; @@ -237,16 +223,16 @@ _message_iter_get_pyobject(DBusMessageIter *iter, switch (type) { case DBUS_TYPE_STRING: DBG("%s", "found a string"); - dbus_message_iter_get_basic(iter, &u.s); + dbus_message_iter_get_basic(iter, &u.str); if (opts->utf8_strings) { - args = Py_BuildValue("(s)", u.s); + args = Py_BuildValue("(s)", u.str); if (!args) break; ret = PyObject_Call((PyObject *)&DBusPyUTF8String_Type, args, kwargs); } else { - args = Py_BuildValue("(N)", PyUnicode_DecodeUTF8(u.s, - strlen(u.s), + args = Py_BuildValue("(N)", PyUnicode_DecodeUTF8(u.str, + strlen(u.str), NULL)); if (!args) { break; @@ -258,24 +244,24 @@ _message_iter_get_pyobject(DBusMessageIter *iter, case DBUS_TYPE_SIGNATURE: DBG("%s", "found a signature"); - dbus_message_iter_get_basic(iter, &u.s); - args = Py_BuildValue("(s)", u.s); + dbus_message_iter_get_basic(iter, &u.str); + args = Py_BuildValue("(s)", u.str); if (!args) break; ret = PyObject_Call((PyObject *)&DBusPySignature_Type, args, kwargs); break; case DBUS_TYPE_OBJECT_PATH: DBG("%s", "found an object path"); - dbus_message_iter_get_basic(iter, &u.s); - args = Py_BuildValue("(s)", u.s); + dbus_message_iter_get_basic(iter, &u.str); + args = Py_BuildValue("(s)", u.str); if (!args) break; ret = PyObject_Call((PyObject *)&DBusPyObjectPath_Type, args, kwargs); break; case DBUS_TYPE_DOUBLE: DBG("%s", "found a double"); - dbus_message_iter_get_basic(iter, &u.d); - args = Py_BuildValue("(f)", u.d); + dbus_message_iter_get_basic(iter, &u.dbl); + args = Py_BuildValue("(f)", u.dbl); if (!args) break; ret = PyObject_Call((PyObject *)&DBusPyDouble_Type, args, kwargs); break; @@ -283,6 +269,8 @@ _message_iter_get_pyobject(DBusMessageIter *iter, #ifdef WITH_DBUS_FLOAT32 case DBUS_TYPE_FLOAT: DBG("%s", "found a float"); + /* FIXME: DBusBasicValue will need to grow a float member if + * float32 becomes supported */ dbus_message_iter_get_basic(iter, &u.f); args = Py_BuildValue("(f)", (double)u.f); if (!args) break; @@ -364,8 +352,8 @@ _message_iter_get_pyobject(DBusMessageIter *iter, case DBUS_TYPE_BYTE: DBG("%s", "found a byte"); - dbus_message_iter_get_basic(iter, &u.y); - args = Py_BuildValue("(l)", (long)u.y); + dbus_message_iter_get_basic(iter, &u.byt); + args = Py_BuildValue("(l)", (long)u.byt); if (!args) break; ret = PyObject_Call((PyObject *)&DBusPyByte_Type, args, kwargs); @@ -373,8 +361,8 @@ _message_iter_get_pyobject(DBusMessageIter *iter, case DBUS_TYPE_BOOLEAN: DBG("%s", "found a bool"); - dbus_message_iter_get_basic(iter, &u.b); - args = Py_BuildValue("(l)", (long)u.b); + dbus_message_iter_get_basic(iter, &u.bool_val); + args = Py_BuildValue("(l)", (long)u.bool_val); if (!args) break; ret = PyObject_Call((PyObject *)&DBusPyBoolean_Type, args, kwargs); @@ -400,15 +388,15 @@ _message_iter_get_pyobject(DBusMessageIter *iter, DBG("%s", "actually, a byte array..."); dbus_message_iter_recurse(iter, &sub); dbus_message_iter_get_fixed_array(&sub, - (const unsigned char **)&u.s, + (const unsigned char **)&u.str, &n); - if (n == 0 && u.s == NULL) { + if (n == 0 && u.str == NULL) { /* fd.o #21831: s# turns (NULL, 0) into None, but * dbus_message_iter_get_fixed_array produces (NULL, 0) * for an empty byte-blob... */ - u.s = ""; + u.str = ""; } - args = Py_BuildValue("(s#)", u.s, (Py_ssize_t)n); + args = Py_BuildValue("(s#)", u.str, (Py_ssize_t)n); if (!args) break; ret = PyObject_Call((PyObject *)&DBusPyByteArray_Type, args, kwargs); diff --git a/configure.ac b/configure.ac index e37bc2d..bb79393 100644 --- a/configure.ac +++ b/configure.ac @@ -151,6 +151,11 @@ AM_CONDITIONAL([ENABLE_DOCS], [test "$enable_html_docs" != no]) PKG_CHECK_MODULES(DBUS, [dbus-1 >= 1.4]) PKG_CHECK_MODULES(DBUS_GLIB, [dbus-glib-1 >= 0.70]) +dbuspy_save_CFLAGS="$CFLAGS" +CFLAGS="$CFLAGS $DBUS_CFLAGS" +AC_CHECK_TYPES([DBusBasicValue], [], [], [#include ]) +CFLAGS="$dbuspy_save_CFLAGS" + TP_COMPILER_WARNINGS([CFLAGS_WARNINGS], [test] dbus_python_released [= 0], [all \ extra \ -- 1.7.7.2