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

Function encoder_stringify_key

simplejson/_speedups.c:1028–1075  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1026#endif /* PY_MAJOR_VERSION < 3 */
1027
1028static PyObject *
1029encoder_stringify_key(PyEncoderObject *s, PyObject *key)
1030{
1031 _speedups_state *state = get_speedups_state(s->module_ref);
1032 if (PyUnicode_Check(key)) {
1033 Py_INCREF(key);
1034 return key;
1035 }
1036#if PY_MAJOR_VERSION >= 3
1037 else if (PyBytes_Check(key) && s->encoding != Py_None) {
1038 const char *encoding = PyUnicode_AsUTF8(s->encoding);
1039 if (encoding == NULL)
1040 return NULL;
1041 return PyUnicode_Decode(
1042 PyBytes_AS_STRING(key),
1043 PyBytes_GET_SIZE(key),
1044 encoding,
1045 NULL);
1046 }
1047#else /* PY_MAJOR_VERSION >= 3 */
1048 else if (PyString_Check(key)) {
1049 Py_INCREF(key);
1050 return key;
1051 }
1052#endif /* PY_MAJOR_VERSION < 3 */
1053 else if (PyFloat_Check(key)) {
1054 return encoder_encode_float(s, key);
1055 }
1056 else if (key == Py_True || key == Py_False || key == Py_None) {
1057 /* This must come before the PyInt_Check because
1058 True and False are also 1 and 0.*/
1059 return _encoded_const(state, key);
1060 }
1061 else if (PyInt_Check(key) || PyLong_Check(key)) {
1062 return encoder_long_to_str(key);
1063 }
1064 else if (s->use_decimal && PyObject_TypeCheck(key, (PyTypeObject *)s->Decimal)) {
1065 return PyObject_Str(key);
1066 }
1067 if (s->skipkeys) {
1068 Py_INCREF(Py_None);
1069 return Py_None;
1070 }
1071 PyErr_Format(PyExc_TypeError,
1072 "keys must be str, int, float, bool or None, "
1073 "not %.100s", key->ob_type->tp_name);
1074 return NULL;
1075}
1076
1077/* Call list.sort(**item_sort_kw) on `lst`. Returns 0 on success,
1078 * -1 on error. Factored out so the fast and slow paths of

Callers 2

encoder_dict_iteritemsFunction · 0.85
encoder_encode_dict_keyFunction · 0.85

Calls 1

get_speedups_stateFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…