Skip to content

Commit 0222024

Browse files
committed
Merge pull request #9805 from behzadnouri/json-seg-fault
BUG: bug in json lib when frame has length zero
2 parents 29f0bfc + 99d47a1 commit 0222024

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

doc/source/whatsnew/v0.16.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Bug Fixes
6161
~~~~~~~~~
6262

6363
- Fixed bug (:issue:`9542`) where labels did not appear properly in legend of ``DataFrame.plot()``. Passing ``label=`` args also now works, and series indices are no longer mutated.
64+
- Bug in json serialization when frame has length zero.(:issue:`9805`)
6465

6566

6667
- Bug in ``scatter_matrix`` draws unexpected axis ticklabels (:issue:`5662`)

pandas/io/tests/test_json/test_pandas.py

+10
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,16 @@ def test_frame_to_json_except(self):
321321
df = DataFrame([1, 2, 3])
322322
self.assertRaises(ValueError, df.to_json, orient="garbage")
323323

324+
def test_frame_empty(self):
325+
df = DataFrame(columns=['jim', 'joe'])
326+
self.assertFalse(df._is_mixed_type)
327+
assert_frame_equal(read_json(df.to_json()), df)
328+
329+
# mixed type
330+
df['joe'] = df['joe'].astype('i8')
331+
self.assertTrue(df._is_mixed_type)
332+
assert_frame_equal(read_json(df.to_json()), df)
333+
324334
def test_v12_compat(self):
325335
df = DataFrame(
326336
[[1.56808523, 0.65727391, 1.81021139, -0.17251653],

pandas/src/ujson/python/objToJSON.c

+15-12
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ static void *PyTimeToJSON(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size_
457457
PyErr_SetString(PyExc_ValueError, "Failed to convert time");
458458
return NULL;
459459
}
460-
if (PyUnicode_Check(str))
460+
if (PyUnicode_Check(str))
461461
{
462462
tmp = str;
463463
str = PyUnicode_AsUTF8String(str);
@@ -479,7 +479,7 @@ static int NpyTypeToJSONType(PyObject* obj, JSONTypeContext* tc, int npyType, vo
479479
{
480480
PRINTMARK();
481481
castfunc = PyArray_GetCastFunc(PyArray_DescrFromType(npyType), NPY_DOUBLE);
482-
if (!castfunc)
482+
if (!castfunc)
483483
{
484484
PyErr_Format (
485485
PyExc_ValueError,
@@ -501,7 +501,7 @@ static int NpyTypeToJSONType(PyObject* obj, JSONTypeContext* tc, int npyType, vo
501501
{
502502
PRINTMARK();
503503
castfunc = PyArray_GetCastFunc(PyArray_DescrFromType(npyType), NPY_INT64);
504-
if (!castfunc)
504+
if (!castfunc)
505505
{
506506
PyErr_Format (
507507
PyExc_ValueError,
@@ -584,7 +584,12 @@ void NpyArr_iterBegin(JSOBJ _obj, JSONTypeContext *tc)
584584
obj = (PyArrayObject *) _obj;
585585
}
586586

587-
if (PyArray_SIZE(obj) > 0)
587+
if (PyArray_SIZE(obj) < 0)
588+
{
589+
PRINTMARK();
590+
GET_TC(tc)->iterNext = NpyArr_iterNextNone;
591+
}
592+
else
588593
{
589594
PRINTMARK();
590595
npyarr = PyObject_Malloc(sizeof(NpyArrContext));
@@ -624,11 +629,6 @@ void NpyArr_iterBegin(JSOBJ _obj, JSONTypeContext *tc)
624629
npyarr->columnLabels = GET_TC(tc)->columnLabels;
625630
npyarr->rowLabels = GET_TC(tc)->rowLabels;
626631
}
627-
else
628-
{
629-
PRINTMARK();
630-
GET_TC(tc)->iterNext = NpyArr_iterNextNone;
631-
}
632632
}
633633

634634
void NpyArr_iterEnd(JSOBJ obj, JSONTypeContext *tc)
@@ -1054,8 +1054,11 @@ void PdBlock_iterBegin(JSOBJ _obj, JSONTypeContext *tc)
10541054
npyarr = GET_TC(tc)->npyarr;
10551055

10561056
// set the dataptr to our desired column and initialise
1057-
npyarr->dataptr += npyarr->stride * idx;
1058-
NpyArr_iterNext(obj, tc);
1057+
if (npyarr != NULL)
1058+
{
1059+
npyarr->dataptr += npyarr->stride * idx;
1060+
NpyArr_iterNext(obj, tc);
1061+
}
10591062
GET_TC(tc)->itemValue = NULL;
10601063
((PyObjectEncoder*) tc->encoder)->npyCtxtPassthru = NULL;
10611064

@@ -2624,7 +2627,7 @@ PyObject* objToJSON(PyObject* self, PyObject *args, PyObject *kwargs)
26242627

26252628
if (odefHandler != NULL && odefHandler != Py_None)
26262629
{
2627-
if (!PyCallable_Check(odefHandler))
2630+
if (!PyCallable_Check(odefHandler))
26282631
{
26292632
PyErr_SetString (PyExc_TypeError, "Default handler is not callable");
26302633
return NULL;

0 commit comments

Comments
 (0)