1 | n/a | /* Implementation helper: a struct that looks like a tuple. See timemodule |
---|
2 | n/a | and posixmodule for example uses. */ |
---|
3 | n/a | |
---|
4 | n/a | #include "Python.h" |
---|
5 | n/a | #include "structmember.h" |
---|
6 | n/a | |
---|
7 | n/a | static const char visible_length_key[] = "n_sequence_fields"; |
---|
8 | n/a | static const char real_length_key[] = "n_fields"; |
---|
9 | n/a | static const char unnamed_fields_key[] = "n_unnamed_fields"; |
---|
10 | n/a | |
---|
11 | n/a | /* Fields with this name have only a field index, not a field name. |
---|
12 | n/a | They are only allowed for indices < n_visible_fields. */ |
---|
13 | n/a | char *PyStructSequence_UnnamedField = "unnamed field"; |
---|
14 | n/a | _Py_IDENTIFIER(n_sequence_fields); |
---|
15 | n/a | _Py_IDENTIFIER(n_fields); |
---|
16 | n/a | _Py_IDENTIFIER(n_unnamed_fields); |
---|
17 | n/a | |
---|
18 | n/a | #define VISIBLE_SIZE(op) Py_SIZE(op) |
---|
19 | n/a | #define VISIBLE_SIZE_TP(tp) PyLong_AsSsize_t( \ |
---|
20 | n/a | _PyDict_GetItemId((tp)->tp_dict, &PyId_n_sequence_fields)) |
---|
21 | n/a | |
---|
22 | n/a | #define REAL_SIZE_TP(tp) PyLong_AsSsize_t( \ |
---|
23 | n/a | _PyDict_GetItemId((tp)->tp_dict, &PyId_n_fields)) |
---|
24 | n/a | #define REAL_SIZE(op) REAL_SIZE_TP(Py_TYPE(op)) |
---|
25 | n/a | |
---|
26 | n/a | #define UNNAMED_FIELDS_TP(tp) PyLong_AsSsize_t( \ |
---|
27 | n/a | _PyDict_GetItemId((tp)->tp_dict, &PyId_n_unnamed_fields)) |
---|
28 | n/a | #define UNNAMED_FIELDS(op) UNNAMED_FIELDS_TP(Py_TYPE(op)) |
---|
29 | n/a | |
---|
30 | n/a | |
---|
31 | n/a | PyObject * |
---|
32 | n/a | PyStructSequence_New(PyTypeObject *type) |
---|
33 | n/a | { |
---|
34 | n/a | PyStructSequence *obj; |
---|
35 | n/a | Py_ssize_t size = REAL_SIZE_TP(type), i; |
---|
36 | n/a | |
---|
37 | n/a | obj = PyObject_GC_NewVar(PyStructSequence, type, size); |
---|
38 | n/a | if (obj == NULL) |
---|
39 | n/a | return NULL; |
---|
40 | n/a | /* Hack the size of the variable object, so invisible fields don't appear |
---|
41 | n/a | to Python code. */ |
---|
42 | n/a | Py_SIZE(obj) = VISIBLE_SIZE_TP(type); |
---|
43 | n/a | for (i = 0; i < size; i++) |
---|
44 | n/a | obj->ob_item[i] = NULL; |
---|
45 | n/a | |
---|
46 | n/a | return (PyObject*)obj; |
---|
47 | n/a | } |
---|
48 | n/a | |
---|
49 | n/a | void |
---|
50 | n/a | PyStructSequence_SetItem(PyObject* op, Py_ssize_t i, PyObject* v) |
---|
51 | n/a | { |
---|
52 | n/a | PyStructSequence_SET_ITEM(op, i, v); |
---|
53 | n/a | } |
---|
54 | n/a | |
---|
55 | n/a | PyObject* |
---|
56 | n/a | PyStructSequence_GetItem(PyObject* op, Py_ssize_t i) |
---|
57 | n/a | { |
---|
58 | n/a | return PyStructSequence_GET_ITEM(op, i); |
---|
59 | n/a | } |
---|
60 | n/a | |
---|
61 | n/a | static void |
---|
62 | n/a | structseq_dealloc(PyStructSequence *obj) |
---|
63 | n/a | { |
---|
64 | n/a | Py_ssize_t i, size; |
---|
65 | n/a | |
---|
66 | n/a | size = REAL_SIZE(obj); |
---|
67 | n/a | for (i = 0; i < size; ++i) { |
---|
68 | n/a | Py_XDECREF(obj->ob_item[i]); |
---|
69 | n/a | } |
---|
70 | n/a | PyObject_GC_Del(obj); |
---|
71 | n/a | } |
---|
72 | n/a | |
---|
73 | n/a | static PyObject * |
---|
74 | n/a | structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
75 | n/a | { |
---|
76 | n/a | PyObject *arg = NULL; |
---|
77 | n/a | PyObject *dict = NULL; |
---|
78 | n/a | PyObject *ob; |
---|
79 | n/a | PyStructSequence *res = NULL; |
---|
80 | n/a | Py_ssize_t len, min_len, max_len, i, n_unnamed_fields; |
---|
81 | n/a | static char *kwlist[] = {"sequence", "dict", 0}; |
---|
82 | n/a | |
---|
83 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:structseq", |
---|
84 | n/a | kwlist, &arg, &dict)) |
---|
85 | n/a | return NULL; |
---|
86 | n/a | |
---|
87 | n/a | arg = PySequence_Fast(arg, "constructor requires a sequence"); |
---|
88 | n/a | |
---|
89 | n/a | if (!arg) { |
---|
90 | n/a | return NULL; |
---|
91 | n/a | } |
---|
92 | n/a | |
---|
93 | n/a | if (dict && !PyDict_Check(dict)) { |
---|
94 | n/a | PyErr_Format(PyExc_TypeError, |
---|
95 | n/a | "%.500s() takes a dict as second arg, if any", |
---|
96 | n/a | type->tp_name); |
---|
97 | n/a | Py_DECREF(arg); |
---|
98 | n/a | return NULL; |
---|
99 | n/a | } |
---|
100 | n/a | |
---|
101 | n/a | len = PySequence_Fast_GET_SIZE(arg); |
---|
102 | n/a | min_len = VISIBLE_SIZE_TP(type); |
---|
103 | n/a | max_len = REAL_SIZE_TP(type); |
---|
104 | n/a | n_unnamed_fields = UNNAMED_FIELDS_TP(type); |
---|
105 | n/a | |
---|
106 | n/a | if (min_len != max_len) { |
---|
107 | n/a | if (len < min_len) { |
---|
108 | n/a | PyErr_Format(PyExc_TypeError, |
---|
109 | n/a | "%.500s() takes an at least %zd-sequence (%zd-sequence given)", |
---|
110 | n/a | type->tp_name, min_len, len); |
---|
111 | n/a | Py_DECREF(arg); |
---|
112 | n/a | return NULL; |
---|
113 | n/a | } |
---|
114 | n/a | |
---|
115 | n/a | if (len > max_len) { |
---|
116 | n/a | PyErr_Format(PyExc_TypeError, |
---|
117 | n/a | "%.500s() takes an at most %zd-sequence (%zd-sequence given)", |
---|
118 | n/a | type->tp_name, max_len, len); |
---|
119 | n/a | Py_DECREF(arg); |
---|
120 | n/a | return NULL; |
---|
121 | n/a | } |
---|
122 | n/a | } |
---|
123 | n/a | else { |
---|
124 | n/a | if (len != min_len) { |
---|
125 | n/a | PyErr_Format(PyExc_TypeError, |
---|
126 | n/a | "%.500s() takes a %zd-sequence (%zd-sequence given)", |
---|
127 | n/a | type->tp_name, min_len, len); |
---|
128 | n/a | Py_DECREF(arg); |
---|
129 | n/a | return NULL; |
---|
130 | n/a | } |
---|
131 | n/a | } |
---|
132 | n/a | |
---|
133 | n/a | res = (PyStructSequence*) PyStructSequence_New(type); |
---|
134 | n/a | if (res == NULL) { |
---|
135 | n/a | Py_DECREF(arg); |
---|
136 | n/a | return NULL; |
---|
137 | n/a | } |
---|
138 | n/a | for (i = 0; i < len; ++i) { |
---|
139 | n/a | PyObject *v = PySequence_Fast_GET_ITEM(arg, i); |
---|
140 | n/a | Py_INCREF(v); |
---|
141 | n/a | res->ob_item[i] = v; |
---|
142 | n/a | } |
---|
143 | n/a | for (; i < max_len; ++i) { |
---|
144 | n/a | if (dict && (ob = PyDict_GetItemString( |
---|
145 | n/a | dict, type->tp_members[i-n_unnamed_fields].name))) { |
---|
146 | n/a | } |
---|
147 | n/a | else { |
---|
148 | n/a | ob = Py_None; |
---|
149 | n/a | } |
---|
150 | n/a | Py_INCREF(ob); |
---|
151 | n/a | res->ob_item[i] = ob; |
---|
152 | n/a | } |
---|
153 | n/a | |
---|
154 | n/a | Py_DECREF(arg); |
---|
155 | n/a | return (PyObject*) res; |
---|
156 | n/a | } |
---|
157 | n/a | |
---|
158 | n/a | |
---|
159 | n/a | static PyObject * |
---|
160 | n/a | structseq_repr(PyStructSequence *obj) |
---|
161 | n/a | { |
---|
162 | n/a | /* buffer and type size were chosen well considered. */ |
---|
163 | n/a | #define REPR_BUFFER_SIZE 512 |
---|
164 | n/a | #define TYPE_MAXSIZE 100 |
---|
165 | n/a | |
---|
166 | n/a | PyTypeObject *typ = Py_TYPE(obj); |
---|
167 | n/a | Py_ssize_t i; |
---|
168 | n/a | int removelast = 0; |
---|
169 | n/a | Py_ssize_t len; |
---|
170 | n/a | char buf[REPR_BUFFER_SIZE]; |
---|
171 | n/a | char *endofbuf, *pbuf = buf; |
---|
172 | n/a | |
---|
173 | n/a | /* pointer to end of writeable buffer; safes space for "...)\0" */ |
---|
174 | n/a | endofbuf= &buf[REPR_BUFFER_SIZE-5]; |
---|
175 | n/a | |
---|
176 | n/a | /* "typename(", limited to TYPE_MAXSIZE */ |
---|
177 | n/a | len = strlen(typ->tp_name) > TYPE_MAXSIZE ? TYPE_MAXSIZE : |
---|
178 | n/a | strlen(typ->tp_name); |
---|
179 | n/a | strncpy(pbuf, typ->tp_name, len); |
---|
180 | n/a | pbuf += len; |
---|
181 | n/a | *pbuf++ = '('; |
---|
182 | n/a | |
---|
183 | n/a | for (i=0; i < VISIBLE_SIZE(obj); i++) { |
---|
184 | n/a | PyObject *val, *repr; |
---|
185 | n/a | const char *cname, *crepr; |
---|
186 | n/a | |
---|
187 | n/a | cname = typ->tp_members[i].name; |
---|
188 | n/a | if (cname == NULL) { |
---|
189 | n/a | PyErr_Format(PyExc_SystemError, "In structseq_repr(), member %d name is NULL" |
---|
190 | n/a | " for type %.500s", i, typ->tp_name); |
---|
191 | n/a | return NULL; |
---|
192 | n/a | } |
---|
193 | n/a | val = PyStructSequence_GET_ITEM(obj, i); |
---|
194 | n/a | repr = PyObject_Repr(val); |
---|
195 | n/a | if (repr == NULL) |
---|
196 | n/a | return NULL; |
---|
197 | n/a | crepr = PyUnicode_AsUTF8(repr); |
---|
198 | n/a | if (crepr == NULL) { |
---|
199 | n/a | Py_DECREF(repr); |
---|
200 | n/a | return NULL; |
---|
201 | n/a | } |
---|
202 | n/a | |
---|
203 | n/a | /* + 3: keep space for "=" and ", " */ |
---|
204 | n/a | len = strlen(cname) + strlen(crepr) + 3; |
---|
205 | n/a | if ((pbuf+len) <= endofbuf) { |
---|
206 | n/a | strcpy(pbuf, cname); |
---|
207 | n/a | pbuf += strlen(cname); |
---|
208 | n/a | *pbuf++ = '='; |
---|
209 | n/a | strcpy(pbuf, crepr); |
---|
210 | n/a | pbuf += strlen(crepr); |
---|
211 | n/a | *pbuf++ = ','; |
---|
212 | n/a | *pbuf++ = ' '; |
---|
213 | n/a | removelast = 1; |
---|
214 | n/a | Py_DECREF(repr); |
---|
215 | n/a | } |
---|
216 | n/a | else { |
---|
217 | n/a | strcpy(pbuf, "..."); |
---|
218 | n/a | pbuf += 3; |
---|
219 | n/a | removelast = 0; |
---|
220 | n/a | Py_DECREF(repr); |
---|
221 | n/a | break; |
---|
222 | n/a | } |
---|
223 | n/a | } |
---|
224 | n/a | if (removelast) { |
---|
225 | n/a | /* overwrite last ", " */ |
---|
226 | n/a | pbuf-=2; |
---|
227 | n/a | } |
---|
228 | n/a | *pbuf++ = ')'; |
---|
229 | n/a | *pbuf = '\0'; |
---|
230 | n/a | |
---|
231 | n/a | return PyUnicode_FromString(buf); |
---|
232 | n/a | } |
---|
233 | n/a | |
---|
234 | n/a | static PyObject * |
---|
235 | n/a | structseq_reduce(PyStructSequence* self) |
---|
236 | n/a | { |
---|
237 | n/a | PyObject* tup = NULL; |
---|
238 | n/a | PyObject* dict = NULL; |
---|
239 | n/a | PyObject* result; |
---|
240 | n/a | Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields, i; |
---|
241 | n/a | |
---|
242 | n/a | n_fields = REAL_SIZE(self); |
---|
243 | n/a | n_visible_fields = VISIBLE_SIZE(self); |
---|
244 | n/a | n_unnamed_fields = UNNAMED_FIELDS(self); |
---|
245 | n/a | tup = PyTuple_New(n_visible_fields); |
---|
246 | n/a | if (!tup) |
---|
247 | n/a | goto error; |
---|
248 | n/a | |
---|
249 | n/a | dict = PyDict_New(); |
---|
250 | n/a | if (!dict) |
---|
251 | n/a | goto error; |
---|
252 | n/a | |
---|
253 | n/a | for (i = 0; i < n_visible_fields; i++) { |
---|
254 | n/a | Py_INCREF(self->ob_item[i]); |
---|
255 | n/a | PyTuple_SET_ITEM(tup, i, self->ob_item[i]); |
---|
256 | n/a | } |
---|
257 | n/a | |
---|
258 | n/a | for (; i < n_fields; i++) { |
---|
259 | n/a | const char *n = Py_TYPE(self)->tp_members[i-n_unnamed_fields].name; |
---|
260 | n/a | if (PyDict_SetItemString(dict, n, self->ob_item[i]) < 0) |
---|
261 | n/a | goto error; |
---|
262 | n/a | } |
---|
263 | n/a | |
---|
264 | n/a | result = Py_BuildValue("(O(OO))", Py_TYPE(self), tup, dict); |
---|
265 | n/a | |
---|
266 | n/a | Py_DECREF(tup); |
---|
267 | n/a | Py_DECREF(dict); |
---|
268 | n/a | |
---|
269 | n/a | return result; |
---|
270 | n/a | |
---|
271 | n/a | error: |
---|
272 | n/a | Py_XDECREF(tup); |
---|
273 | n/a | Py_XDECREF(dict); |
---|
274 | n/a | return NULL; |
---|
275 | n/a | } |
---|
276 | n/a | |
---|
277 | n/a | static PyMethodDef structseq_methods[] = { |
---|
278 | n/a | {"__reduce__", (PyCFunction)structseq_reduce, METH_NOARGS, NULL}, |
---|
279 | n/a | {NULL, NULL} |
---|
280 | n/a | }; |
---|
281 | n/a | |
---|
282 | n/a | static PyTypeObject _struct_sequence_template = { |
---|
283 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
284 | n/a | NULL, /* tp_name */ |
---|
285 | n/a | sizeof(PyStructSequence) - sizeof(PyObject *), /* tp_basicsize */ |
---|
286 | n/a | sizeof(PyObject *), /* tp_itemsize */ |
---|
287 | n/a | (destructor)structseq_dealloc, /* tp_dealloc */ |
---|
288 | n/a | 0, /* tp_print */ |
---|
289 | n/a | 0, /* tp_getattr */ |
---|
290 | n/a | 0, /* tp_setattr */ |
---|
291 | n/a | 0, /* tp_reserved */ |
---|
292 | n/a | (reprfunc)structseq_repr, /* tp_repr */ |
---|
293 | n/a | 0, /* tp_as_number */ |
---|
294 | n/a | 0, /* tp_as_sequence */ |
---|
295 | n/a | 0, /* tp_as_mapping */ |
---|
296 | n/a | 0, /* tp_hash */ |
---|
297 | n/a | 0, /* tp_call */ |
---|
298 | n/a | 0, /* tp_str */ |
---|
299 | n/a | 0, /* tp_getattro */ |
---|
300 | n/a | 0, /* tp_setattro */ |
---|
301 | n/a | 0, /* tp_as_buffer */ |
---|
302 | n/a | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
303 | n/a | NULL, /* tp_doc */ |
---|
304 | n/a | 0, /* tp_traverse */ |
---|
305 | n/a | 0, /* tp_clear */ |
---|
306 | n/a | 0, /* tp_richcompare */ |
---|
307 | n/a | 0, /* tp_weaklistoffset */ |
---|
308 | n/a | 0, /* tp_iter */ |
---|
309 | n/a | 0, /* tp_iternext */ |
---|
310 | n/a | structseq_methods, /* tp_methods */ |
---|
311 | n/a | NULL, /* tp_members */ |
---|
312 | n/a | 0, /* tp_getset */ |
---|
313 | n/a | 0, /* tp_base */ |
---|
314 | n/a | 0, /* tp_dict */ |
---|
315 | n/a | 0, /* tp_descr_get */ |
---|
316 | n/a | 0, /* tp_descr_set */ |
---|
317 | n/a | 0, /* tp_dictoffset */ |
---|
318 | n/a | 0, /* tp_init */ |
---|
319 | n/a | 0, /* tp_alloc */ |
---|
320 | n/a | structseq_new, /* tp_new */ |
---|
321 | n/a | }; |
---|
322 | n/a | |
---|
323 | n/a | int |
---|
324 | n/a | PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc) |
---|
325 | n/a | { |
---|
326 | n/a | PyObject *dict; |
---|
327 | n/a | PyMemberDef* members; |
---|
328 | n/a | Py_ssize_t n_members, n_unnamed_members, i, k; |
---|
329 | n/a | PyObject *v; |
---|
330 | n/a | |
---|
331 | n/a | #ifdef Py_TRACE_REFS |
---|
332 | n/a | /* if the type object was chained, unchain it first |
---|
333 | n/a | before overwriting its storage */ |
---|
334 | n/a | if (type->ob_base.ob_base._ob_next) { |
---|
335 | n/a | _Py_ForgetReference((PyObject*)type); |
---|
336 | n/a | } |
---|
337 | n/a | #endif |
---|
338 | n/a | |
---|
339 | n/a | n_unnamed_members = 0; |
---|
340 | n/a | for (i = 0; desc->fields[i].name != NULL; ++i) |
---|
341 | n/a | if (desc->fields[i].name == PyStructSequence_UnnamedField) |
---|
342 | n/a | n_unnamed_members++; |
---|
343 | n/a | n_members = i; |
---|
344 | n/a | |
---|
345 | n/a | memcpy(type, &_struct_sequence_template, sizeof(PyTypeObject)); |
---|
346 | n/a | type->tp_base = &PyTuple_Type; |
---|
347 | n/a | type->tp_name = desc->name; |
---|
348 | n/a | type->tp_doc = desc->doc; |
---|
349 | n/a | |
---|
350 | n/a | members = PyMem_NEW(PyMemberDef, n_members-n_unnamed_members+1); |
---|
351 | n/a | if (members == NULL) { |
---|
352 | n/a | PyErr_NoMemory(); |
---|
353 | n/a | return -1; |
---|
354 | n/a | } |
---|
355 | n/a | |
---|
356 | n/a | for (i = k = 0; i < n_members; ++i) { |
---|
357 | n/a | if (desc->fields[i].name == PyStructSequence_UnnamedField) |
---|
358 | n/a | continue; |
---|
359 | n/a | members[k].name = desc->fields[i].name; |
---|
360 | n/a | members[k].type = T_OBJECT; |
---|
361 | n/a | members[k].offset = offsetof(PyStructSequence, ob_item) |
---|
362 | n/a | + i * sizeof(PyObject*); |
---|
363 | n/a | members[k].flags = READONLY; |
---|
364 | n/a | members[k].doc = desc->fields[i].doc; |
---|
365 | n/a | k++; |
---|
366 | n/a | } |
---|
367 | n/a | members[k].name = NULL; |
---|
368 | n/a | |
---|
369 | n/a | type->tp_members = members; |
---|
370 | n/a | |
---|
371 | n/a | if (PyType_Ready(type) < 0) |
---|
372 | n/a | return -1; |
---|
373 | n/a | Py_INCREF(type); |
---|
374 | n/a | |
---|
375 | n/a | dict = type->tp_dict; |
---|
376 | n/a | #define SET_DICT_FROM_SIZE(key, value) \ |
---|
377 | n/a | do { \ |
---|
378 | n/a | v = PyLong_FromSsize_t(value); \ |
---|
379 | n/a | if (v == NULL) \ |
---|
380 | n/a | return -1; \ |
---|
381 | n/a | if (PyDict_SetItemString(dict, key, v) < 0) { \ |
---|
382 | n/a | Py_DECREF(v); \ |
---|
383 | n/a | return -1; \ |
---|
384 | n/a | } \ |
---|
385 | n/a | Py_DECREF(v); \ |
---|
386 | n/a | } while (0) |
---|
387 | n/a | |
---|
388 | n/a | SET_DICT_FROM_SIZE(visible_length_key, desc->n_in_sequence); |
---|
389 | n/a | SET_DICT_FROM_SIZE(real_length_key, n_members); |
---|
390 | n/a | SET_DICT_FROM_SIZE(unnamed_fields_key, n_unnamed_members); |
---|
391 | n/a | |
---|
392 | n/a | return 0; |
---|
393 | n/a | } |
---|
394 | n/a | |
---|
395 | n/a | void |
---|
396 | n/a | PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc) |
---|
397 | n/a | { |
---|
398 | n/a | (void)PyStructSequence_InitType2(type, desc); |
---|
399 | n/a | } |
---|
400 | n/a | |
---|
401 | n/a | PyTypeObject* |
---|
402 | n/a | PyStructSequence_NewType(PyStructSequence_Desc *desc) |
---|
403 | n/a | { |
---|
404 | n/a | PyTypeObject *result; |
---|
405 | n/a | |
---|
406 | n/a | result = (PyTypeObject*)PyType_GenericAlloc(&PyType_Type, 0); |
---|
407 | n/a | if (result == NULL) |
---|
408 | n/a | return NULL; |
---|
409 | n/a | if (PyStructSequence_InitType2(result, desc) < 0) { |
---|
410 | n/a | Py_DECREF(result); |
---|
411 | n/a | return NULL; |
---|
412 | n/a | } |
---|
413 | n/a | return result; |
---|
414 | n/a | } |
---|
415 | n/a | |
---|
416 | n/a | int _PyStructSequence_Init(void) |
---|
417 | n/a | { |
---|
418 | n/a | if (_PyUnicode_FromId(&PyId_n_sequence_fields) == NULL |
---|
419 | n/a | || _PyUnicode_FromId(&PyId_n_fields) == NULL |
---|
420 | n/a | || _PyUnicode_FromId(&PyId_n_unnamed_fields) == NULL) |
---|
421 | n/a | return -1; |
---|
422 | n/a | |
---|
423 | n/a | return 0; |
---|
424 | n/a | } |
---|