static PyObject * record_repr(ApgRecordObject *v) { Py_ssize_t i, n; PyObject *keys_iter; _PyUnicodeWriter writer; n = Py_SIZE(v); assert(n > 0); keys_iter = PyObject_GetIter(v->mapping); if (keys_iter == NULL) { return NULL; } i = Py_ReprEnter((PyObject *)v); if (i != 0) { Py_DECREF(keys_iter); return i > 0 ? PyUnicode_FromString("<Record ...>") : NULL; } _PyUnicodeWriter_Init(&writer); writer.overallocate = 1; writer.min_length = 12; /* <Record a=1> */ if (_PyUnicodeWriter_WriteASCIIString(&writer, "<Record ", 8) < 0) { goto error; } for (i = 0; i < n; ++i) { PyObject *key; PyObject *key_repr; PyObject *val_repr; if (i > 0) { if (_PyUnicodeWriter_WriteChar(&writer, ' ') < 0) { goto error; } } if (Py_EnterRecursiveCall(" while getting the repr of a record")) { goto error; } val_repr = PyObject_Repr(v->ob_item[i]); Py_LeaveRecursiveCall(); if (val_repr == NULL) { goto error; } key = PyIter_Next(keys_iter); if (key == NULL) { Py_DECREF(val_repr); PyErr_SetString(PyExc_RuntimeError, "invalid record mapping"); goto error; } key_repr = PyObject_Str(key); Py_DECREF(key); if (key_repr == NULL) { Py_DECREF(val_repr); goto error; } if (_PyUnicodeWriter_WriteStr(&writer, key_repr) < 0) { Py_DECREF(key_repr); Py_DECREF(val_repr); goto error; } Py_DECREF(key_repr); if (_PyUnicodeWriter_WriteChar(&writer, '=') < 0) { Py_DECREF(val_repr); goto error; } if (_PyUnicodeWriter_WriteStr(&writer, val_repr) < 0) { Py_DECREF(val_repr); goto error; } Py_DECREF(val_repr); } writer.overallocate = 0; if (_PyUnicodeWriter_WriteChar(&writer, '>') < 0) { goto error; } Py_DECREF(keys_iter); Py_ReprLeave((PyObject *)v); return _PyUnicodeWriter_Finish(&writer); error: Py_DECREF(keys_iter); _PyUnicodeWriter_Dealloc(&writer); Py_ReprLeave((PyObject *)v); return NULL; }
static PyObject * tuplerepr(PyTupleObject *v) { Py_ssize_t i, n; _PyUnicodeWriter writer; n = Py_SIZE(v); if (n == 0) return PyUnicode_FromString("()"); /* While not mutable, it is still possible to end up with a cycle in a tuple through an object that stores itself within a tuple (and thus infinitely asks for the repr of itself). This should only be possible within a type. */ i = Py_ReprEnter((PyObject *)v); if (i != 0) { return i > 0 ? PyUnicode_FromString("(...)") : NULL; } _PyUnicodeWriter_Init(&writer); writer.overallocate = 1; if (Py_SIZE(v) > 1) { /* "(" + "1" + ", 2" * (len - 1) + ")" */ writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1; } else { /* "(1,)" */ writer.min_length = 4; } if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0) goto error; /* Do repr() on each element. */ for (i = 0; i < n; ++i) { PyObject *s; if (i > 0) { if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) goto error; } if (Py_EnterRecursiveCall(" while getting the repr of a tuple")) goto error; s = PyObject_Repr(v->ob_item[i]); Py_LeaveRecursiveCall(); if (s == NULL) goto error; if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) { Py_DECREF(s); goto error; } Py_DECREF(s); } writer.overallocate = 0; if (n > 1) { if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0) goto error; } else { if (_PyUnicodeWriter_WriteASCIIString(&writer, ",)", 2) < 0) goto error; } Py_ReprLeave((PyObject *)v); return _PyUnicodeWriter_Finish(&writer); error: _PyUnicodeWriter_Dealloc(&writer); Py_ReprLeave((PyObject *)v); return NULL; }