#ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,))
INFINITY = float('inf')
-FLOAT_REPR = repr
+FLOAT_REPR = float.__repr__
def encode_basestring(s):
"""Return a JSON representation of a Python string
self.assertNotEqual(res[0], res[0])
self.assertRaises(ValueError, self.dumps, [val], allow_nan=False)
+ def test_float_subclasses_use_float_repr(self):
+ # Issue 27934.
+ class PeculiarFloat(float):
+ def __repr__(self):
+ return "I'm not valid JSON"
+ def __str__(self):
+ return "Neither am I"
+
+ val = PeculiarFloat(3.2)
+ self.assertEqual(self.loads(self.dumps(val)), val)
+
class TestPyFloat(TestFloat, PyTest): pass
class TestCFloat(TestFloat, CTest): pass
David Jacobs
Kevin Jacobs
Kjetil Jacobsen
+Eddie James
Bertrand Janin
Geert Jansen
Jack Jansen
Library
-------
+- Issue #27934: Use ``float.__repr__`` instead of plain ``repr`` when JSON-
+ encoding an instance of a float subclass. Thanks Eddie James.
+
- Issue #27861: Fixed a crash in sqlite3.Connection.cursor() when a factory
creates not a cursor. Patch by Xiang Zhang.
return PyString_FromString("NaN");
}
}
- /* Use a better float format here? */
- return PyObject_Repr(obj);
+ /* Make sure to use the base float class repr method */
+ return PyFloat_Type.tp_repr(obj);
}
static PyObject *