From 02b6ab4830ede7464200114fec365eaffc73c63a Mon Sep 17 00:00:00 2001 From: Ronan Lamy Date: Sat, 28 Sep 2019 17:04:49 +0100 Subject: [PATCH 1/3] BUG: fix broken error message in ujson.encode() (GH18878) --- pandas/_libs/src/ujson/python/objToJSON.c | 6 ++---- pandas/tests/io/json/test_ujson.py | 4 +++- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/_libs/src/ujson/python/objToJSON.c b/pandas/_libs/src/ujson/python/objToJSON.c index 22c42acea0150..48712dc68829d 100644 --- a/pandas/_libs/src/ujson/python/objToJSON.c +++ b/pandas/_libs/src/ujson/python/objToJSON.c @@ -1986,11 +1986,9 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) { tc->type = JT_DOUBLE; return; } else if (PyArray_Check(obj) && PyArray_CheckScalar(obj)) { - tmpObj = PyObject_Repr(obj); PyErr_Format(PyExc_TypeError, - "%s (0d array) is not JSON serializable at the moment", - PyBytes_AS_STRING(tmpObj)); - Py_DECREF(tmpObj); + "%R (0d array) is not JSON serializable at the moment", + obj); goto INVALID; } diff --git a/pandas/tests/io/json/test_ujson.py b/pandas/tests/io/json/test_ujson.py index 69a246487ddf1..2cecf8f7b3bd6 100644 --- a/pandas/tests/io/json/test_ujson.py +++ b/pandas/tests/io/json/test_ujson.py @@ -780,8 +780,10 @@ def test_array_float(self): tm.assert_almost_equal(arr, arr_out) def test_0d_array(self): - with pytest.raises(TypeError): + msg = "array(1) (0d array) is not JSON serializable at the moment" + with pytest.raises(TypeError) as excinfo: ujson.encode(np.array(1)) + assert str(excinfo.value) == msg @pytest.mark.parametrize( "bad_input,exc_type,kwargs", From ddfda2871dc723a0662816c46a8edfd37d5df77e Mon Sep 17 00:00:00 2001 From: Ronan Lamy Date: Sat, 28 Sep 2019 19:57:25 +0100 Subject: [PATCH 2/3] Add whatsnew --- doc/source/whatsnew/v1.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index eb4b72d01d59a..b00d2157a9216 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -301,6 +301,7 @@ Other - Using :meth:`DataFrame.replace` with overlapping keys in a nested dictionary will no longer raise, now matching the behavior of a flat dictionary (:issue:`27660`) - :meth:`DataFrame.to_csv` and :meth:`Series.to_csv` now support dicts as ``compression`` argument with key ``'method'`` being the compression method and others as additional compression options when the compression method is ``'zip'``. (:issue:`26023`) - :meth:`Series.append` will no longer raise a ``TypeError`` when passed a tuple of ``Series`` (:issue:`28410`) +- Fix corrupted error message when calling ``pandas.libs._json.encode()`` on a 0d array (:issue:`18878`) .. _whatsnew_1000.contributors: From 1b59fc408d557b546a04c0c599fa79313d582837 Mon Sep 17 00:00:00 2001 From: Ronan Lamy Date: Sun, 6 Oct 2019 18:22:59 +0100 Subject: [PATCH 3/3] Use re.escape, add issue number --- pandas/tests/io/json/test_ujson.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/io/json/test_ujson.py b/pandas/tests/io/json/test_ujson.py index 2cecf8f7b3bd6..d6572ac7b7bfe 100644 --- a/pandas/tests/io/json/test_ujson.py +++ b/pandas/tests/io/json/test_ujson.py @@ -780,10 +780,10 @@ def test_array_float(self): tm.assert_almost_equal(arr, arr_out) def test_0d_array(self): - msg = "array(1) (0d array) is not JSON serializable at the moment" - with pytest.raises(TypeError) as excinfo: + # gh-18878 + msg = re.escape("array(1) (0d array) is not JSON serializable at the moment") + with pytest.raises(TypeError, match=msg): ujson.encode(np.array(1)) - assert str(excinfo.value) == msg @pytest.mark.parametrize( "bad_input,exc_type,kwargs",