[issue27934] json float encoding incorrect for dbus.Double

Mark Dickinson report at bugs.python.org
Fri Sep 2 10:44:04 EDT 2016


Mark Dickinson added the comment:

> Dbus.Double is not a subclass of float unfortunately.

Okay, now I'm *really* confused. :-)

If it's not a subclass of `float`, then how do you end up in the `floatstr` code? Every path to that code that I can see in the source is via an `isinstance(<value>, float`) check.

I don't know dbus at all, but I just tried installing it under Macports (on OS X 10.9). Here's the package description, so you can tell me whether I'm actually installing the right thing, or something totally unrelated:

    dbus-python35 @1.2.0_2 (devel, python)
        Python bindings for the dbus message bus system.

Once I've done that, I get the following behaviour in Python:

Python 3.5.2 (default, Aug 16 2016, 08:43:53) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dbus
>>> dbus.Double
<class 'dbus.Double'>
>>> dbus.Double.__mro__
(<class 'dbus.Double'>, <class '_dbus_bindings._FloatBase'>, <class 'float'>, <class 'object'>)

So it looks as though at least for this version of dbus, we do have a subclass of `float`. Looking at an instance, I see the following:

>>> x = dbus.Double(4.3)
>>> isinstance(x, float)
True
>>> repr(x)
'dbus.Double(4.3)'
>>> str(x)
'4.3'
>>> float.__repr__(x)
'4.3'
>>> float.__str__(x)
'4.3'
>>> import json
>>> json.dumps(x)
'4.3'

So I'm still struggling to see what difference swapping out float.__repr__ for float.__str__ would make.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue27934>
_______________________________________


More information about the Python-bugs-list mailing list