Skip to content

Commit a17820d

Browse files
committed
Clean up Decimal to utf8 convertion and switch to using PyObject_Format() to suppress scientific notation
1 parent 1c6781d commit a17820d

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

pandas/_libs/src/vendored/ujson/python/objToJSON.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -376,25 +376,21 @@ static char *PyTimeToJSON(JSOBJ _obj, JSONTypeContext *tc, size_t *outLen) {
376376
static char *PyDecimalToUTF8Callback(JSOBJ _obj, JSONTypeContext *tc,
377377
size_t *len) {
378378
PyObject *obj = (PyObject *)_obj;
379-
PyObject *str = PyObject_Str(obj);
379+
PyObject *format_spec = PyUnicode_FromStringAndSize("f", 1);
380+
PyObject *str = PyObject_Format(obj, format_spec);
381+
Py_DECREF(format_spec);
382+
380383
if (str == NULL) {
381-
*len = 0;
382-
if (!PyErr_Occurred()) {
383-
PyErr_SetString(PyExc_ValueError, "Failed to convert decimal");
384-
}
385384
((JSONObjectEncoder *)tc->encoder)->errorMsg = "";
386385
return NULL;
387386
}
388-
if (PyUnicode_Check(str)) {
389-
PyObject *tmp = str;
390-
str = PyUnicode_AsUTF8String(str);
391-
Py_DECREF(tmp);
392-
}
393387

394388
GET_TC(tc)->newObj = str;
395389

396-
*len = PyBytes_GET_SIZE(str);
397-
char *outValue = PyBytes_AS_STRING(str);
390+
Py_ssize_t s_len;
391+
char *outValue = (char *)PyUnicode_AsUTF8AndSize(str, &s_len);
392+
*len = s_len;
393+
398394
return outValue;
399395
}
400396

0 commit comments

Comments
 (0)