Skip to content

Commit 4ac2d51

Browse files
committed
progress
1 parent 548afb7 commit 4ac2d51

File tree

1 file changed

+82
-2
lines changed

1 file changed

+82
-2
lines changed

Objects/recordobject.c

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,93 @@ PyAPI_FUNC(PyObject *) PyRecord_New(Py_ssize_t size)
5454
static void
5555
record_dealloc(PyRecordObject *op)
5656
{
57-
// TODO
57+
Py_ssize_t len = Py_SIZE(op);
58+
PyObject_GC_UnTrack(op);
59+
Py_TRASHCAN_BEGIN(op, record_dealloc)
60+
Py_XDECREF(op->names);
61+
if (len > 0) {
62+
Py_ssize_t i = len;
63+
while (--i >= 0) {
64+
Py_XDECREF(op->ob_item[i]);
65+
}
66+
}
67+
Py_TYPE(op)->tp_free((PyObject *)op);
68+
Py_TRASHCAN_END
5869
}
5970

6071
static PyObject *
6172
record_repr(PyRecordObject *v)
6273
{
63-
// TODO
74+
Py_ssize_t i, n;
75+
_PyUnicodeWriter writer;
76+
77+
n = Py_SIZE(v);
78+
if (n == 0)
79+
return PyUnicode_FromString("record()");
80+
81+
/* While not mutable, it is still possible to end up with a cycle in a
82+
record through an object that stores itself within a record (and thus
83+
infinitely asks for the repr of itself). This should only be
84+
possible within a type. */
85+
i = Py_ReprEnter((PyObject *)v);
86+
if (i != 0) {
87+
return i > 0 ? PyUnicode_FromString("record(...)") : NULL;
88+
}
89+
90+
_PyUnicodeWriter_Init(&writer);
91+
writer.overallocate = 1;
92+
if (Py_SIZE(v) > 1) {
93+
/* "(" + "1" + ", 2" * (len - 1) + ")" */
94+
writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
95+
}
96+
else {
97+
/* "(1,)" */
98+
writer.min_length = 4;
99+
}
100+
101+
if (_PyUnicodeWriter_WriteASCIIString(&writer, "record(", 7) < 0)
102+
goto error;
103+
104+
/* Do repr() on each element. */
105+
for (i = 0; i < n; ++i) {
106+
PyObject *s;
107+
PyObject *name;
108+
109+
if (i > 0) {
110+
if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
111+
goto error;
112+
}
113+
114+
name = PyTuple_GET_ITEM(v->names, i);
115+
s = PyObject_Repr(v->ob_item[i]);
116+
if (s == NULL)
117+
goto error;
118+
119+
if (_PyUnicodeWriter_WriteStr(&writer, name) < 0) {
120+
Py_DECREF(s);
121+
goto error;
122+
}
123+
if (_PyUnicodeWriter_WriteChar(&writer, '=') < 0) {
124+
Py_DECREF(s);
125+
goto error;
126+
}
127+
if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
128+
Py_DECREF(s);
129+
goto error;
130+
}
131+
Py_DECREF(s);
132+
}
133+
134+
writer.overallocate = 0;
135+
if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0)
136+
goto error;
137+
138+
Py_ReprLeave((PyObject *)v);
139+
return _PyUnicodeWriter_Finish(&writer);
140+
141+
error:
142+
_PyUnicodeWriter_Dealloc(&writer);
143+
Py_ReprLeave((PyObject *)v);
64144
return NULL;
65145
}
66146

0 commit comments

Comments
 (0)