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