From 3b60175c3169410d3b199c0b50a24ad93f42fcd5 Mon Sep 17 00:00:00 2001 From: "Edward A. James" Date: Wed, 24 Aug 2016 16:36:04 -0500 Subject: fix json dbus.Double encoding - bad formatting showed up in rest server queries - json used the highest level repr method, which will call the dbus type tp_repr method. Instead we should use the float specific repr method. resolves openbmc/openbmc#411 Change-Id: I04eea6d825fe3833accf1625e138abedb0aa7c95 Signed-off-by: Edward A. James --- .../python/python/issue_27934_use-float-repr.patch | 66 ++++++++++++++++++++++ .../recipes-devtools/python/python_2.7.11.bbappend | 1 + 2 files changed, 67 insertions(+) create mode 100644 meta-phosphor/common/recipes-devtools/python/python/issue_27934_use-float-repr.patch (limited to 'meta-phosphor/common/recipes-devtools/python') diff --git a/meta-phosphor/common/recipes-devtools/python/python/issue_27934_use-float-repr.patch b/meta-phosphor/common/recipes-devtools/python/python/issue_27934_use-float-repr.patch new file mode 100644 index 000000000..0f8bb3351 --- /dev/null +++ b/meta-phosphor/common/recipes-devtools/python/python/issue_27934_use-float-repr.patch @@ -0,0 +1,66 @@ +# HG changeset patch +# User Mark Dickinson +# Date 1472921100 -3600 +# Node ID 86d66a627b774d741d9a8834f16706ba3de61953 +# Parent e065aec0e6fa04000e3e57da45139e11df0c675f +Issue #27934: Use float.__repr__ instead of plain repr when JSON-encoding an instance of a float subclass. Thanks Eddie James. + +diff --git a/Lib/json/encoder.py b/Lib/json/encoder.py +--- a/Lib/json/encoder.py ++++ b/Lib/json/encoder.py +@@ -28,7 +28,7 @@ for i in range(0x20): + #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 +diff --git a/Lib/json/tests/test_float.py b/Lib/json/tests/test_float.py +--- a/Lib/json/tests/test_float.py ++++ b/Lib/json/tests/test_float.py +@@ -32,6 +32,17 @@ class TestFloat(object): + 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 +diff --git a/Misc/ACKS b/Misc/ACKS +--- a/Misc/ACKS ++++ b/Misc/ACKS +@@ -636,6 +636,7 @@ Manuel Jacob + David Jacobs + Kevin Jacobs + Kjetil Jacobsen ++Eddie James + Bertrand Janin + Geert Jansen + Jack Jansen +diff --git a/Modules/_json.c b/Modules/_json.c +--- a/Modules/_json.c ++++ b/Modules/_json.c +@@ -1960,8 +1960,8 @@ encoder_encode_float(PyEncoderObject *s, + 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 * + diff --git a/meta-phosphor/common/recipes-devtools/python/python_2.7.11.bbappend b/meta-phosphor/common/recipes-devtools/python/python_2.7.11.bbappend index 569e75397..f5c58e3d4 100644 --- a/meta-phosphor/common/recipes-devtools/python/python_2.7.11.bbappend +++ b/meta-phosphor/common/recipes-devtools/python/python_2.7.11.bbappend @@ -1,5 +1,6 @@ FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:" SRC_URI += "file://__init__.email_min.py" +SRC_URI += "file://issue_27934_use-float-repr.patch" do_install_append_class-target() { dir=${libdir}/python${PYTHON_MAJMIN}/email -- cgit v1.2.1