structseq.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /* Implementation helper: a struct that looks like a tuple.
  2. See timemodule and posixmodule for example uses.
  3. The structseq helper is considered an internal CPython implementation
  4. detail. Docs for modules using structseqs should call them
  5. "named tuples" (be sure to include a space between the two
  6. words and add a link back to the term in Docs/glossary.rst).
  7. */
  8. #include "Python.h"
  9. #include "pycore_tuple.h" // _PyTuple_FromArray()
  10. #include "pycore_object.h" // _PyObject_GC_TRACK()
  11. #include "structmember.h" // PyMemberDef
  12. #include "pycore_structseq.h" // PyStructSequence_InitType()
  13. #include "pycore_initconfig.h" // _PyStatus_OK()
  14. static const char visible_length_key[] = "n_sequence_fields";
  15. static const char real_length_key[] = "n_fields";
  16. static const char unnamed_fields_key[] = "n_unnamed_fields";
  17. static const char match_args_key[] = "__match_args__";
  18. /* Fields with this name have only a field index, not a field name.
  19. They are only allowed for indices < n_visible_fields. */
  20. const char * const PyStructSequence_UnnamedField = "unnamed field";
  21. static Py_ssize_t
  22. get_type_attr_as_size(PyTypeObject *tp, PyObject *name)
  23. {
  24. PyObject *v = PyDict_GetItemWithError(_PyType_GetDict(tp), name);
  25. if (v == NULL && !PyErr_Occurred()) {
  26. PyErr_Format(PyExc_TypeError,
  27. "Missed attribute '%U' of type %s",
  28. name, tp->tp_name);
  29. return -1;
  30. }
  31. return PyLong_AsSsize_t(v);
  32. }
  33. #define VISIBLE_SIZE(op) Py_SIZE(op)
  34. #define VISIBLE_SIZE_TP(tp) \
  35. get_type_attr_as_size(tp, &_Py_ID(n_sequence_fields))
  36. #define REAL_SIZE_TP(tp) \
  37. get_type_attr_as_size(tp, &_Py_ID(n_fields))
  38. #define REAL_SIZE(op) REAL_SIZE_TP(Py_TYPE(op))
  39. #define UNNAMED_FIELDS_TP(tp) \
  40. get_type_attr_as_size(tp, &_Py_ID(n_unnamed_fields))
  41. #define UNNAMED_FIELDS(op) UNNAMED_FIELDS_TP(Py_TYPE(op))
  42. PyObject *
  43. PyStructSequence_New(PyTypeObject *type)
  44. {
  45. PyStructSequence *obj;
  46. Py_ssize_t size = REAL_SIZE_TP(type), i;
  47. if (size < 0) {
  48. return NULL;
  49. }
  50. Py_ssize_t vsize = VISIBLE_SIZE_TP(type);
  51. if (vsize < 0) {
  52. return NULL;
  53. }
  54. obj = PyObject_GC_NewVar(PyStructSequence, type, size);
  55. if (obj == NULL)
  56. return NULL;
  57. /* Hack the size of the variable object, so invisible fields don't appear
  58. to Python code. */
  59. Py_SET_SIZE(obj, vsize);
  60. for (i = 0; i < size; i++)
  61. obj->ob_item[i] = NULL;
  62. return (PyObject*)obj;
  63. }
  64. void
  65. PyStructSequence_SetItem(PyObject* op, Py_ssize_t i, PyObject* v)
  66. {
  67. PyStructSequence_SET_ITEM(op, i, v);
  68. }
  69. PyObject*
  70. PyStructSequence_GetItem(PyObject* op, Py_ssize_t i)
  71. {
  72. return PyStructSequence_GET_ITEM(op, i);
  73. }
  74. static int
  75. structseq_traverse(PyStructSequence *obj, visitproc visit, void *arg)
  76. {
  77. if (Py_TYPE(obj)->tp_flags & Py_TPFLAGS_HEAPTYPE) {
  78. Py_VISIT(Py_TYPE(obj));
  79. }
  80. Py_ssize_t i, size;
  81. size = REAL_SIZE(obj);
  82. for (i = 0; i < size; ++i) {
  83. Py_VISIT(obj->ob_item[i]);
  84. }
  85. return 0;
  86. }
  87. static void
  88. structseq_dealloc(PyStructSequence *obj)
  89. {
  90. Py_ssize_t i, size;
  91. PyObject_GC_UnTrack(obj);
  92. PyTypeObject *tp = Py_TYPE(obj);
  93. size = REAL_SIZE(obj);
  94. for (i = 0; i < size; ++i) {
  95. Py_XDECREF(obj->ob_item[i]);
  96. }
  97. PyObject_GC_Del(obj);
  98. if (_PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) {
  99. Py_DECREF(tp);
  100. }
  101. }
  102. /*[clinic input]
  103. class structseq "PyStructSequence *" "NULL"
  104. [clinic start generated code]*/
  105. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=9d781c6922c77752]*/
  106. #include "clinic/structseq.c.h"
  107. /*[clinic input]
  108. @classmethod
  109. structseq.__new__ as structseq_new
  110. sequence as arg: object
  111. dict: object(c_default="NULL") = {}
  112. [clinic start generated code]*/
  113. static PyObject *
  114. structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict)
  115. /*[clinic end generated code: output=baa082e788b171da input=90532511101aa3fb]*/
  116. {
  117. PyObject *ob;
  118. PyStructSequence *res = NULL;
  119. Py_ssize_t len, min_len, max_len, i, n_unnamed_fields;
  120. min_len = VISIBLE_SIZE_TP(type);
  121. if (min_len < 0) {
  122. return NULL;
  123. }
  124. max_len = REAL_SIZE_TP(type);
  125. if (max_len < 0) {
  126. return NULL;
  127. }
  128. n_unnamed_fields = UNNAMED_FIELDS_TP(type);
  129. if (n_unnamed_fields < 0) {
  130. return NULL;
  131. }
  132. arg = PySequence_Fast(arg, "constructor requires a sequence");
  133. if (!arg) {
  134. return NULL;
  135. }
  136. if (dict && !PyDict_Check(dict)) {
  137. PyErr_Format(PyExc_TypeError,
  138. "%.500s() takes a dict as second arg, if any",
  139. type->tp_name);
  140. Py_DECREF(arg);
  141. return NULL;
  142. }
  143. len = PySequence_Fast_GET_SIZE(arg);
  144. if (min_len != max_len) {
  145. if (len < min_len) {
  146. PyErr_Format(PyExc_TypeError,
  147. "%.500s() takes an at least %zd-sequence (%zd-sequence given)",
  148. type->tp_name, min_len, len);
  149. Py_DECREF(arg);
  150. return NULL;
  151. }
  152. if (len > max_len) {
  153. PyErr_Format(PyExc_TypeError,
  154. "%.500s() takes an at most %zd-sequence (%zd-sequence given)",
  155. type->tp_name, max_len, len);
  156. Py_DECREF(arg);
  157. return NULL;
  158. }
  159. }
  160. else {
  161. if (len != min_len) {
  162. PyErr_Format(PyExc_TypeError,
  163. "%.500s() takes a %zd-sequence (%zd-sequence given)",
  164. type->tp_name, min_len, len);
  165. Py_DECREF(arg);
  166. return NULL;
  167. }
  168. }
  169. res = (PyStructSequence*) PyStructSequence_New(type);
  170. if (res == NULL) {
  171. Py_DECREF(arg);
  172. return NULL;
  173. }
  174. for (i = 0; i < len; ++i) {
  175. PyObject *v = PySequence_Fast_GET_ITEM(arg, i);
  176. res->ob_item[i] = Py_NewRef(v);
  177. }
  178. Py_DECREF(arg);
  179. for (; i < max_len; ++i) {
  180. if (dict == NULL) {
  181. ob = Py_None;
  182. }
  183. else {
  184. ob = _PyDict_GetItemStringWithError(dict,
  185. type->tp_members[i-n_unnamed_fields].name);
  186. if (ob == NULL) {
  187. if (PyErr_Occurred()) {
  188. Py_DECREF(res);
  189. return NULL;
  190. }
  191. ob = Py_None;
  192. }
  193. }
  194. res->ob_item[i] = Py_NewRef(ob);
  195. }
  196. _PyObject_GC_TRACK(res);
  197. return (PyObject*) res;
  198. }
  199. static PyObject *
  200. structseq_repr(PyStructSequence *obj)
  201. {
  202. PyTypeObject *typ = Py_TYPE(obj);
  203. _PyUnicodeWriter writer;
  204. /* Write "typename(" */
  205. PyObject *type_name = PyUnicode_DecodeUTF8(typ->tp_name,
  206. strlen(typ->tp_name),
  207. NULL);
  208. if (type_name == NULL) {
  209. return NULL;
  210. }
  211. _PyUnicodeWriter_Init(&writer);
  212. writer.overallocate = 1;
  213. /* count 5 characters per item: "x=1, " */
  214. writer.min_length = (PyUnicode_GET_LENGTH(type_name) + 1
  215. + VISIBLE_SIZE(obj) * 5 + 1);
  216. if (_PyUnicodeWriter_WriteStr(&writer, type_name) < 0) {
  217. Py_DECREF(type_name);
  218. goto error;
  219. }
  220. Py_DECREF(type_name);
  221. if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0) {
  222. goto error;
  223. }
  224. for (Py_ssize_t i=0; i < VISIBLE_SIZE(obj); i++) {
  225. if (i > 0) {
  226. /* Write ", " */
  227. if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) {
  228. goto error;
  229. }
  230. }
  231. /* Write "name=repr" */
  232. const char *name_utf8 = typ->tp_members[i].name;
  233. if (name_utf8 == NULL) {
  234. PyErr_Format(PyExc_SystemError, "In structseq_repr(), member %zd name is NULL"
  235. " for type %.500s", i, typ->tp_name);
  236. goto error;
  237. }
  238. PyObject *name = PyUnicode_DecodeUTF8(name_utf8, strlen(name_utf8), NULL);
  239. if (name == NULL) {
  240. goto error;
  241. }
  242. if (_PyUnicodeWriter_WriteStr(&writer, name) < 0) {
  243. Py_DECREF(name);
  244. goto error;
  245. }
  246. Py_DECREF(name);
  247. if (_PyUnicodeWriter_WriteChar(&writer, '=') < 0) {
  248. goto error;
  249. }
  250. PyObject *value = PyStructSequence_GET_ITEM(obj, i);
  251. assert(value != NULL);
  252. PyObject *repr = PyObject_Repr(value);
  253. if (repr == NULL) {
  254. goto error;
  255. }
  256. if (_PyUnicodeWriter_WriteStr(&writer, repr) < 0) {
  257. Py_DECREF(repr);
  258. goto error;
  259. }
  260. Py_DECREF(repr);
  261. }
  262. if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0) {
  263. goto error;
  264. }
  265. return _PyUnicodeWriter_Finish(&writer);
  266. error:
  267. _PyUnicodeWriter_Dealloc(&writer);
  268. return NULL;
  269. }
  270. static PyObject *
  271. structseq_reduce(PyStructSequence* self, PyObject *Py_UNUSED(ignored))
  272. {
  273. PyObject* tup = NULL;
  274. PyObject* dict = NULL;
  275. PyObject* result;
  276. Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields, i;
  277. n_fields = REAL_SIZE(self);
  278. if (n_fields < 0) {
  279. return NULL;
  280. }
  281. n_visible_fields = VISIBLE_SIZE(self);
  282. n_unnamed_fields = UNNAMED_FIELDS(self);
  283. if (n_unnamed_fields < 0) {
  284. return NULL;
  285. }
  286. tup = _PyTuple_FromArray(self->ob_item, n_visible_fields);
  287. if (!tup)
  288. goto error;
  289. dict = PyDict_New();
  290. if (!dict)
  291. goto error;
  292. for (i = n_visible_fields; i < n_fields; i++) {
  293. const char *n = Py_TYPE(self)->tp_members[i-n_unnamed_fields].name;
  294. if (PyDict_SetItemString(dict, n, self->ob_item[i]) < 0)
  295. goto error;
  296. }
  297. result = Py_BuildValue("(O(OO))", Py_TYPE(self), tup, dict);
  298. Py_DECREF(tup);
  299. Py_DECREF(dict);
  300. return result;
  301. error:
  302. Py_XDECREF(tup);
  303. Py_XDECREF(dict);
  304. return NULL;
  305. }
  306. static PyMethodDef structseq_methods[] = {
  307. {"__reduce__", (PyCFunction)structseq_reduce, METH_NOARGS, NULL},
  308. {NULL, NULL}
  309. };
  310. static Py_ssize_t
  311. count_members(PyStructSequence_Desc *desc, Py_ssize_t *n_unnamed_members) {
  312. Py_ssize_t i;
  313. *n_unnamed_members = 0;
  314. for (i = 0; desc->fields[i].name != NULL; ++i) {
  315. if (desc->fields[i].name == PyStructSequence_UnnamedField) {
  316. (*n_unnamed_members)++;
  317. }
  318. }
  319. return i;
  320. }
  321. static int
  322. initialize_structseq_dict(PyStructSequence_Desc *desc, PyObject* dict,
  323. Py_ssize_t n_members, Py_ssize_t n_unnamed_members) {
  324. PyObject *v;
  325. #define SET_DICT_FROM_SIZE(key, value) \
  326. do { \
  327. v = PyLong_FromSsize_t(value); \
  328. if (v == NULL) { \
  329. return -1; \
  330. } \
  331. if (PyDict_SetItemString(dict, key, v) < 0) { \
  332. Py_DECREF(v); \
  333. return -1; \
  334. } \
  335. Py_DECREF(v); \
  336. } while (0)
  337. SET_DICT_FROM_SIZE(visible_length_key, desc->n_in_sequence);
  338. SET_DICT_FROM_SIZE(real_length_key, n_members);
  339. SET_DICT_FROM_SIZE(unnamed_fields_key, n_unnamed_members);
  340. // Prepare and set __match_args__
  341. Py_ssize_t i, k;
  342. PyObject* keys = PyTuple_New(desc->n_in_sequence);
  343. if (keys == NULL) {
  344. return -1;
  345. }
  346. for (i = k = 0; i < desc->n_in_sequence; ++i) {
  347. if (desc->fields[i].name == PyStructSequence_UnnamedField) {
  348. continue;
  349. }
  350. PyObject* new_member = PyUnicode_FromString(desc->fields[i].name);
  351. if (new_member == NULL) {
  352. goto error;
  353. }
  354. PyTuple_SET_ITEM(keys, k, new_member);
  355. k++;
  356. }
  357. if (_PyTuple_Resize(&keys, k) == -1) {
  358. goto error;
  359. }
  360. if (PyDict_SetItemString(dict, match_args_key, keys) < 0) {
  361. goto error;
  362. }
  363. Py_DECREF(keys);
  364. return 0;
  365. error:
  366. Py_DECREF(keys);
  367. return -1;
  368. }
  369. static PyMemberDef *
  370. initialize_members(PyStructSequence_Desc *desc,
  371. Py_ssize_t n_members, Py_ssize_t n_unnamed_members)
  372. {
  373. PyMemberDef *members;
  374. members = PyMem_NEW(PyMemberDef, n_members - n_unnamed_members + 1);
  375. if (members == NULL) {
  376. PyErr_NoMemory();
  377. return NULL;
  378. }
  379. Py_ssize_t i, k;
  380. for (i = k = 0; i < n_members; ++i) {
  381. if (desc->fields[i].name == PyStructSequence_UnnamedField) {
  382. continue;
  383. }
  384. /* The names and docstrings in these MemberDefs are statically */
  385. /* allocated so it is expected that they'll outlive the MemberDef */
  386. members[k].name = desc->fields[i].name;
  387. members[k].type = T_OBJECT;
  388. members[k].offset = offsetof(PyStructSequence, ob_item)
  389. + i * sizeof(PyObject*);
  390. members[k].flags = READONLY;
  391. members[k].doc = desc->fields[i].doc;
  392. k++;
  393. }
  394. members[k].name = NULL;
  395. return members;
  396. }
  397. static void
  398. initialize_static_fields(PyTypeObject *type, PyStructSequence_Desc *desc,
  399. PyMemberDef *tp_members, unsigned long tp_flags)
  400. {
  401. type->tp_name = desc->name;
  402. type->tp_basicsize = sizeof(PyStructSequence) - sizeof(PyObject *);
  403. type->tp_itemsize = sizeof(PyObject *);
  404. type->tp_dealloc = (destructor)structseq_dealloc;
  405. type->tp_repr = (reprfunc)structseq_repr;
  406. type->tp_doc = desc->doc;
  407. type->tp_base = &PyTuple_Type;
  408. type->tp_methods = structseq_methods;
  409. type->tp_new = structseq_new;
  410. type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | tp_flags;
  411. type->tp_traverse = (traverseproc) structseq_traverse;
  412. type->tp_members = tp_members;
  413. }
  414. static int
  415. initialize_static_type(PyTypeObject *type, PyStructSequence_Desc *desc,
  416. Py_ssize_t n_members, Py_ssize_t n_unnamed_members) {
  417. /* initialize_static_fields() should have been called already. */
  418. if (PyType_Ready(type) < 0) {
  419. return -1;
  420. }
  421. Py_INCREF(type);
  422. if (initialize_structseq_dict(
  423. desc, _PyType_GetDict(type), n_members, n_unnamed_members) < 0) {
  424. Py_DECREF(type);
  425. return -1;
  426. }
  427. return 0;
  428. }
  429. int
  430. _PyStructSequence_InitBuiltinWithFlags(PyInterpreterState *interp,
  431. PyTypeObject *type,
  432. PyStructSequence_Desc *desc,
  433. unsigned long tp_flags)
  434. {
  435. Py_ssize_t n_unnamed_members;
  436. Py_ssize_t n_members = count_members(desc, &n_unnamed_members);
  437. PyMemberDef *members = NULL;
  438. if ((type->tp_flags & Py_TPFLAGS_READY) == 0) {
  439. assert(type->tp_name == NULL);
  440. assert(type->tp_members == NULL);
  441. assert(type->tp_base == NULL);
  442. members = initialize_members(desc, n_members, n_unnamed_members);
  443. if (members == NULL) {
  444. goto error;
  445. }
  446. initialize_static_fields(type, desc, members, tp_flags);
  447. _Py_SetImmortal(type);
  448. }
  449. #ifndef NDEBUG
  450. else {
  451. // Ensure that the type was initialized.
  452. assert(type->tp_name != NULL);
  453. assert(type->tp_members != NULL);
  454. assert(type->tp_base == &PyTuple_Type);
  455. assert((type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN));
  456. assert(_Py_IsImmortal(type));
  457. }
  458. #endif
  459. if (_PyStaticType_InitBuiltin(interp, type) < 0) {
  460. PyErr_Format(PyExc_RuntimeError,
  461. "Can't initialize builtin type %s",
  462. desc->name);
  463. goto error;
  464. }
  465. if (initialize_structseq_dict(
  466. desc, _PyType_GetDict(type), n_members, n_unnamed_members) < 0)
  467. {
  468. goto error;
  469. }
  470. return 0;
  471. error:
  472. if (members != NULL) {
  473. PyMem_Free(members);
  474. }
  475. return -1;
  476. }
  477. int
  478. PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc)
  479. {
  480. PyMemberDef *members;
  481. Py_ssize_t n_members, n_unnamed_members;
  482. #ifdef Py_TRACE_REFS
  483. /* if the type object was chained, unchain it first
  484. before overwriting its storage */
  485. if (type->ob_base.ob_base._ob_next) {
  486. _Py_ForgetReference((PyObject *)type);
  487. }
  488. #endif
  489. /* PyTypeObject has already been initialized */
  490. if (Py_REFCNT(type) != 0) {
  491. PyErr_BadInternalCall();
  492. return -1;
  493. }
  494. n_members = count_members(desc, &n_unnamed_members);
  495. members = initialize_members(desc, n_members, n_unnamed_members);
  496. if (members == NULL) {
  497. return -1;
  498. }
  499. initialize_static_fields(type, desc, members, 0);
  500. if (initialize_static_type(type, desc, n_members, n_unnamed_members) < 0) {
  501. PyMem_Free(members);
  502. return -1;
  503. }
  504. return 0;
  505. }
  506. void
  507. PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc)
  508. {
  509. (void)PyStructSequence_InitType2(type, desc);
  510. }
  511. /* This is exposed in the internal API, not the public API.
  512. It is only called on builtin static types, which are all
  513. initialized via _PyStructSequence_InitBuiltinWithFlags(). */
  514. void
  515. _PyStructSequence_FiniBuiltin(PyInterpreterState *interp, PyTypeObject *type)
  516. {
  517. // Ensure that the type is initialized
  518. assert(type->tp_name != NULL);
  519. assert(type->tp_base == &PyTuple_Type);
  520. assert((type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN));
  521. assert(_Py_IsImmortal(type));
  522. // Cannot delete a type if it still has subclasses
  523. if (_PyType_HasSubclasses(type)) {
  524. // XXX Shouldn't this be an error?
  525. return;
  526. }
  527. _PyStaticType_Dealloc(interp, type);
  528. if (_Py_IsMainInterpreter(interp)) {
  529. // Undo _PyStructSequence_InitBuiltinWithFlags().
  530. type->tp_name = NULL;
  531. PyMem_Free(type->tp_members);
  532. type->tp_members = NULL;
  533. type->tp_base = NULL;
  534. }
  535. }
  536. PyTypeObject *
  537. _PyStructSequence_NewType(PyStructSequence_Desc *desc, unsigned long tp_flags)
  538. {
  539. PyMemberDef *members;
  540. PyTypeObject *type;
  541. PyType_Slot slots[8];
  542. PyType_Spec spec;
  543. Py_ssize_t n_members, n_unnamed_members;
  544. /* Initialize MemberDefs */
  545. n_members = count_members(desc, &n_unnamed_members);
  546. members = initialize_members(desc, n_members, n_unnamed_members);
  547. if (members == NULL) {
  548. return NULL;
  549. }
  550. /* Initialize Slots */
  551. slots[0] = (PyType_Slot){Py_tp_dealloc, (destructor)structseq_dealloc};
  552. slots[1] = (PyType_Slot){Py_tp_repr, (reprfunc)structseq_repr};
  553. slots[2] = (PyType_Slot){Py_tp_doc, (void *)desc->doc};
  554. slots[3] = (PyType_Slot){Py_tp_methods, structseq_methods};
  555. slots[4] = (PyType_Slot){Py_tp_new, structseq_new};
  556. slots[5] = (PyType_Slot){Py_tp_members, members};
  557. slots[6] = (PyType_Slot){Py_tp_traverse, (traverseproc)structseq_traverse};
  558. slots[7] = (PyType_Slot){0, 0};
  559. /* Initialize Spec */
  560. /* The name in this PyType_Spec is statically allocated so it is */
  561. /* expected that it'll outlive the PyType_Spec */
  562. spec.name = desc->name;
  563. spec.basicsize = sizeof(PyStructSequence) - sizeof(PyObject *);
  564. spec.itemsize = sizeof(PyObject *);
  565. spec.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | tp_flags;
  566. spec.slots = slots;
  567. type = (PyTypeObject *)PyType_FromSpecWithBases(&spec, (PyObject *)&PyTuple_Type);
  568. PyMem_Free(members);
  569. if (type == NULL) {
  570. return NULL;
  571. }
  572. if (initialize_structseq_dict(
  573. desc, _PyType_GetDict(type), n_members, n_unnamed_members) < 0) {
  574. Py_DECREF(type);
  575. return NULL;
  576. }
  577. return type;
  578. }
  579. PyTypeObject *
  580. PyStructSequence_NewType(PyStructSequence_Desc *desc)
  581. {
  582. return _PyStructSequence_NewType(desc, 0);
  583. }