MCPcopy Create free account
hub / github.com/simplejson/simplejson / encoder_encode_float

Function encoder_encode_float

simplejson/_speedups.c:2788–2831  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2786}
2787
2788static PyObject *
2789encoder_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
2833static PyObject *
2834encoder_encode_string(PyEncoderObject *s, PyObject *obj)

Callers 2

ifFunction · 0.85
encoder_listencode_objFunction · 0.85

Calls 2

get_speedups_stateFunction · 0.85
_encoded_constFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…