summaryrefslogtreecommitdiffstats
path: root/Modules/_json.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-04-10 11:41:19 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-04-10 11:41:19 (GMT)
commite0805cf10ea84b44a13ad5649267edba7cb83ee9 (patch)
treea40e8e3d1cd72b030628cd6d8bb5abe6632c22d6 /Modules/_json.c
parentfc43511867bfee131a3bfb18969ad02a1bb44a07 (diff)
downloadcpython-e0805cf10ea84b44a13ad5649267edba7cb83ee9.zip
cpython-e0805cf10ea84b44a13ad5649267edba7cb83ee9.tar.gz
cpython-e0805cf10ea84b44a13ad5649267edba7cb83ee9.tar.bz2
Issue #26719: More efficient formatting of ints and floats in json.
Diffstat (limited to 'Modules/_json.c')
-rw-r--r--Modules/_json.c56
1 files changed, 4 insertions, 52 deletions
diff --git a/Modules/_json.c b/Modules/_json.c
index f63d758..f82af34 100644
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -116,8 +116,6 @@ raise_errmsg(char *msg, PyObject *s, Py_ssize_t end);
static PyObject *
encoder_encode_string(PyEncoderObject *s, PyObject *obj);
static PyObject *
-encoder_encode_long(PyEncoderObject* s UNUSED, PyObject *obj);
-static PyObject *
encoder_encode_float(PyEncoderObject *s, PyObject *obj);
#define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '"')
@@ -1445,38 +1443,9 @@ _encoded_const(PyObject *obj)
}
static PyObject *
-encoder_encode_long(PyEncoderObject* s UNUSED, PyObject *obj)
-{
- /* Return the JSON representation of a PyLong and PyLong subclasses.
- Calls int() on PyLong subclasses in case the str() was changed.
- Added specifically to deal with IntEnum. See Issue18264. */
- PyObject *encoded, *longobj;
- if (PyLong_CheckExact(obj)) {
- encoded = PyObject_Str(obj);
- }
- else {
- longobj = PyNumber_Long(obj);
- if (longobj == NULL) {
- PyErr_SetString(
- PyExc_ValueError,
- "Unable to coerce int subclass to int"
- );
- return NULL;
- }
- encoded = PyObject_Str(longobj);
- Py_DECREF(longobj);
- }
- return encoded;
-}
-
-
-static PyObject *
encoder_encode_float(PyEncoderObject *s, PyObject *obj)
{
- /* Return the JSON representation of a PyFloat.
- Modified to call float() on float subclasses in case the subclass
- changes the repr. See Issue18264. */
- PyObject *encoded, *floatobj;
+ /* Return the JSON representation of a PyFloat. */
double i = PyFloat_AS_DOUBLE(obj);
if (!Py_IS_FINITE(i)) {
if (!s->allow_nan) {
@@ -1496,24 +1465,7 @@ encoder_encode_float(PyEncoderObject *s, PyObject *obj)
return PyUnicode_FromString("NaN");
}
}
- /* coerce float subclasses to float (primarily for Enum) */
- if (PyFloat_CheckExact(obj)) {
- /* Use a better float format here? */
- encoded = PyObject_Repr(obj);
- }
- else {
- floatobj = PyNumber_Float(obj);
- if (floatobj == NULL) {
- PyErr_SetString(
- PyExc_ValueError,
- "Unable to coerce float subclass to float"
- );
- return NULL;
- }
- encoded = PyObject_Repr(floatobj);
- Py_DECREF(floatobj);
- }
- return encoded;
+ return PyFloat_Type.tp_repr(obj);
}
static PyObject *
@@ -1557,7 +1509,7 @@ encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
return _steal_accumulate(acc, encoded);
}
else if (PyLong_Check(obj)) {
- PyObject *encoded = encoder_encode_long(s, obj);
+ PyObject *encoded = PyLong_Type.tp_str(obj);
if (encoded == NULL)
return -1;
return _steal_accumulate(acc, encoded);
@@ -1722,7 +1674,7 @@ encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
goto bail;
}
else if (PyLong_Check(key)) {
- kstr = encoder_encode_long(s, key);
+ kstr = PyLong_Type.tp_str(key);
if (kstr == NULL) {
goto bail;
}