| 2786 | } |
| 2787 | |
| 2788 | static PyObject * |
| 2789 | encoder_encode_float(PyEncoderObject *s, PyObject *obj) |
| 2790 | { |
| 2791 | /* Return the JSON representation of a PyFloat */ |
| 2792 | _speedups_state *state = get_speedups_state(s->module_ref); |
| 2793 | double i = PyFloat_AS_DOUBLE(obj); |
| 2794 | if (!Py_IS_FINITE(i)) { |
| 2795 | if (!s->allow_or_ignore_nan) { |
| 2796 | PyErr_SetString(PyExc_ValueError, "Out of range float values are not JSON compliant"); |
| 2797 | return NULL; |
| 2798 | } |
| 2799 | if (s->allow_or_ignore_nan & JSON_IGNORE_NAN) { |
| 2800 | return _encoded_const(state, Py_None); |
| 2801 | } |
| 2802 | /* JSON_ALLOW_NAN is set */ |
| 2803 | else if (i > 0) { |
| 2804 | Py_INCREF(state->JSON_Infinity); |
| 2805 | return state->JSON_Infinity; |
| 2806 | } |
| 2807 | else if (i < 0) { |
| 2808 | Py_INCREF(state->JSON_NegInfinity); |
| 2809 | return state->JSON_NegInfinity; |
| 2810 | } |
| 2811 | else { |
| 2812 | Py_INCREF(state->JSON_NaN); |
| 2813 | return state->JSON_NaN; |
| 2814 | } |
| 2815 | } |
| 2816 | /* Use a better float format here? */ |
| 2817 | if (PyFloat_CheckExact(obj)) { |
| 2818 | return PyObject_Repr(obj); |
| 2819 | } |
| 2820 | else { |
| 2821 | /* See #118, do not trust custom str/repr */ |
| 2822 | PyObject *res; |
| 2823 | PyObject *tmp = PyObject_CallOneArg((PyObject *)&PyFloat_Type, obj); |
| 2824 | if (tmp == NULL) { |
| 2825 | return NULL; |
| 2826 | } |
| 2827 | res = PyObject_Repr(tmp); |
| 2828 | Py_DECREF(tmp); |
| 2829 | return res; |
| 2830 | } |
| 2831 | } |
| 2832 | |
| 2833 | static PyObject * |
| 2834 | encoder_encode_string(PyEncoderObject *s, PyObject *obj) |
no test coverage detected
searching dependent graphs…