diff options
author | Simon McVittie <smcv@collabora.com> | 2019-11-21 09:45:47 +0000 |
---|---|---|
committer | Simon McVittie <smcv@collabora.com> | 2019-11-21 09:46:04 +0000 |
commit | 1537eef58ed5b0b79f97407db8387f1c1ed63887 (patch) | |
tree | eae80cbe4b790c5a20e577fa5862e62eb0291a92 | |
parent | 25e9a5f2c861d87e57d65453f5f95daba27f4b0c (diff) | |
download | dbus-python-1537eef58ed5b0b79f97407db8387f1c1ed63887.tar.gz |
abstract: Stringify long subclasses using long's repr
Python 3.8 removes the tp_str for various built-in types, so we would
print long-derived values as their repr (for example dbus.Int64(42)),
which is a regression. Print them as 42 instead.
Co-authored-by: matclab <mathieu@clabaut.net>
Signed-off-by: Simon McVittie <smcv@collabora.com>
Fixes: https://gitlab.freedesktop.org/dbus/dbus-python/issues/31
-rw-r--r-- | dbus_bindings/abstract.c | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/dbus_bindings/abstract.c b/dbus_bindings/abstract.c index 2e3b914..6123c3d 100644 --- a/dbus_bindings/abstract.c +++ b/dbus_bindings/abstract.c @@ -713,6 +713,19 @@ DBusPythonLong_tp_repr(PyObject *self) return my_repr; } +#ifdef PY3 +/* In Python >= 3.8 the tp_str for subclasses of built-in types prints + * the subclass repr(), which does not match dbus-python's historical + * behaviour. */ +static PyObject * +DBusPythonLong_tp_str(PyObject *self) +{ + return (PyLong_Type.tp_repr)(self); +} +#else +#define DBusPythonLong_tp_str 0 +#endif + static void DBusPyLongBase_tp_dealloc(PyObject *self) { @@ -736,7 +749,7 @@ PyTypeObject DBusPyLongBase_Type = { 0, /* tp_as_mapping */ 0, /* tp_hash */ 0, /* tp_call */ - 0, /* tp_str */ + DBusPythonLong_tp_str, /* tp_str */ dbus_py_variant_level_getattro, /* tp_getattro */ dbus_py_immutable_setattro, /* tp_setattro */ 0, /* tp_as_buffer */ |