tupleobject.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. /* Tuple object implementation */
  2. #include "Python.h"
  3. #include "pycore_abstract.h" // _PyIndex_Check()
  4. #include "pycore_gc.h" // _PyObject_GC_IS_TRACKED()
  5. #include "pycore_initconfig.h" // _PyStatus_OK()
  6. #include "pycore_object.h" // _PyObject_GC_TRACK(), _Py_FatalRefcountError()
  7. /*[clinic input]
  8. class tuple "PyTupleObject *" "&PyTuple_Type"
  9. [clinic start generated code]*/
  10. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=f051ba3cfdf9a189]*/
  11. #include "clinic/tupleobject.c.h"
  12. static inline PyTupleObject * maybe_freelist_pop(Py_ssize_t);
  13. static inline int maybe_freelist_push(PyTupleObject *);
  14. /* Allocate an uninitialized tuple object. Before making it public, following
  15. steps must be done:
  16. - Initialize its items.
  17. - Call _PyObject_GC_TRACK() on it.
  18. Because the empty tuple is always reused and it's already tracked by GC,
  19. this function must not be called with size == 0 (unless from PyTuple_New()
  20. which wraps this function).
  21. */
  22. static PyTupleObject *
  23. tuple_alloc(Py_ssize_t size)
  24. {
  25. if (size < 0) {
  26. PyErr_BadInternalCall();
  27. return NULL;
  28. }
  29. #ifdef Py_DEBUG
  30. assert(size != 0); // The empty tuple is statically allocated.
  31. #endif
  32. PyTupleObject *op = maybe_freelist_pop(size);
  33. if (op == NULL) {
  34. /* Check for overflow */
  35. if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - (sizeof(PyTupleObject) -
  36. sizeof(PyObject *))) / sizeof(PyObject *)) {
  37. return (PyTupleObject *)PyErr_NoMemory();
  38. }
  39. op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
  40. if (op == NULL)
  41. return NULL;
  42. }
  43. return op;
  44. }
  45. // The empty tuple singleton is not tracked by the GC.
  46. // It does not contain any Python object.
  47. // Note that tuple subclasses have their own empty instances.
  48. static inline PyObject *
  49. tuple_get_empty(void)
  50. {
  51. return Py_NewRef(&_Py_SINGLETON(tuple_empty));
  52. }
  53. PyObject *
  54. PyTuple_New(Py_ssize_t size)
  55. {
  56. PyTupleObject *op;
  57. if (size == 0) {
  58. return tuple_get_empty();
  59. }
  60. op = tuple_alloc(size);
  61. if (op == NULL) {
  62. return NULL;
  63. }
  64. for (Py_ssize_t i = 0; i < size; i++) {
  65. op->ob_item[i] = NULL;
  66. }
  67. _PyObject_GC_TRACK(op);
  68. return (PyObject *) op;
  69. }
  70. Py_ssize_t
  71. PyTuple_Size(PyObject *op)
  72. {
  73. if (!PyTuple_Check(op)) {
  74. PyErr_BadInternalCall();
  75. return -1;
  76. }
  77. else
  78. return Py_SIZE(op);
  79. }
  80. PyObject *
  81. PyTuple_GetItem(PyObject *op, Py_ssize_t i)
  82. {
  83. if (!PyTuple_Check(op)) {
  84. PyErr_BadInternalCall();
  85. return NULL;
  86. }
  87. if (i < 0 || i >= Py_SIZE(op)) {
  88. PyErr_SetString(PyExc_IndexError, "tuple index out of range");
  89. return NULL;
  90. }
  91. return ((PyTupleObject *)op) -> ob_item[i];
  92. }
  93. int
  94. PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
  95. {
  96. PyObject **p;
  97. if (!PyTuple_Check(op) || Py_REFCNT(op) != 1) {
  98. Py_XDECREF(newitem);
  99. PyErr_BadInternalCall();
  100. return -1;
  101. }
  102. if (i < 0 || i >= Py_SIZE(op)) {
  103. Py_XDECREF(newitem);
  104. PyErr_SetString(PyExc_IndexError,
  105. "tuple assignment index out of range");
  106. return -1;
  107. }
  108. p = ((PyTupleObject *)op) -> ob_item + i;
  109. Py_XSETREF(*p, newitem);
  110. return 0;
  111. }
  112. void
  113. _PyTuple_MaybeUntrack(PyObject *op)
  114. {
  115. PyTupleObject *t;
  116. Py_ssize_t i, n;
  117. if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
  118. return;
  119. t = (PyTupleObject *) op;
  120. n = Py_SIZE(t);
  121. for (i = 0; i < n; i++) {
  122. PyObject *elt = PyTuple_GET_ITEM(t, i);
  123. /* Tuple with NULL elements aren't
  124. fully constructed, don't untrack
  125. them yet. */
  126. if (!elt ||
  127. _PyObject_GC_MAY_BE_TRACKED(elt))
  128. return;
  129. }
  130. _PyObject_GC_UNTRACK(op);
  131. }
  132. PyObject *
  133. PyTuple_Pack(Py_ssize_t n, ...)
  134. {
  135. Py_ssize_t i;
  136. PyObject *o;
  137. PyObject **items;
  138. va_list vargs;
  139. if (n == 0) {
  140. return tuple_get_empty();
  141. }
  142. va_start(vargs, n);
  143. PyTupleObject *result = tuple_alloc(n);
  144. if (result == NULL) {
  145. va_end(vargs);
  146. return NULL;
  147. }
  148. items = result->ob_item;
  149. for (i = 0; i < n; i++) {
  150. o = va_arg(vargs, PyObject *);
  151. items[i] = Py_NewRef(o);
  152. }
  153. va_end(vargs);
  154. _PyObject_GC_TRACK(result);
  155. return (PyObject *)result;
  156. }
  157. /* Methods */
  158. static void
  159. tupledealloc(PyTupleObject *op)
  160. {
  161. if (Py_SIZE(op) == 0) {
  162. /* The empty tuple is statically allocated. */
  163. if (op == &_Py_SINGLETON(tuple_empty)) {
  164. #ifdef Py_DEBUG
  165. _Py_FatalRefcountError("deallocating the empty tuple singleton");
  166. #else
  167. return;
  168. #endif
  169. }
  170. #ifdef Py_DEBUG
  171. /* tuple subclasses have their own empty instances. */
  172. assert(!PyTuple_CheckExact(op));
  173. #endif
  174. }
  175. PyObject_GC_UnTrack(op);
  176. Py_TRASHCAN_BEGIN(op, tupledealloc)
  177. Py_ssize_t i = Py_SIZE(op);
  178. while (--i >= 0) {
  179. Py_XDECREF(op->ob_item[i]);
  180. }
  181. // This will abort on the empty singleton (if there is one).
  182. if (!maybe_freelist_push(op)) {
  183. Py_TYPE(op)->tp_free((PyObject *)op);
  184. }
  185. Py_TRASHCAN_END
  186. }
  187. static PyObject *
  188. tuplerepr(PyTupleObject *v)
  189. {
  190. Py_ssize_t i, n;
  191. _PyUnicodeWriter writer;
  192. n = Py_SIZE(v);
  193. if (n == 0)
  194. return PyUnicode_FromString("()");
  195. /* While not mutable, it is still possible to end up with a cycle in a
  196. tuple through an object that stores itself within a tuple (and thus
  197. infinitely asks for the repr of itself). This should only be
  198. possible within a type. */
  199. i = Py_ReprEnter((PyObject *)v);
  200. if (i != 0) {
  201. return i > 0 ? PyUnicode_FromString("(...)") : NULL;
  202. }
  203. _PyUnicodeWriter_Init(&writer);
  204. writer.overallocate = 1;
  205. if (Py_SIZE(v) > 1) {
  206. /* "(" + "1" + ", 2" * (len - 1) + ")" */
  207. writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
  208. }
  209. else {
  210. /* "(1,)" */
  211. writer.min_length = 4;
  212. }
  213. if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0)
  214. goto error;
  215. /* Do repr() on each element. */
  216. for (i = 0; i < n; ++i) {
  217. PyObject *s;
  218. if (i > 0) {
  219. if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
  220. goto error;
  221. }
  222. s = PyObject_Repr(v->ob_item[i]);
  223. if (s == NULL)
  224. goto error;
  225. if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
  226. Py_DECREF(s);
  227. goto error;
  228. }
  229. Py_DECREF(s);
  230. }
  231. writer.overallocate = 0;
  232. if (n > 1) {
  233. if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0)
  234. goto error;
  235. }
  236. else {
  237. if (_PyUnicodeWriter_WriteASCIIString(&writer, ",)", 2) < 0)
  238. goto error;
  239. }
  240. Py_ReprLeave((PyObject *)v);
  241. return _PyUnicodeWriter_Finish(&writer);
  242. error:
  243. _PyUnicodeWriter_Dealloc(&writer);
  244. Py_ReprLeave((PyObject *)v);
  245. return NULL;
  246. }
  247. /* Hash for tuples. This is a slightly simplified version of the xxHash
  248. non-cryptographic hash:
  249. - we do not use any parallelism, there is only 1 accumulator.
  250. - we drop the final mixing since this is just a permutation of the
  251. output space: it does not help against collisions.
  252. - at the end, we mangle the length with a single constant.
  253. For the xxHash specification, see
  254. https://github.com/Cyan4973/xxHash/blob/master/doc/xxhash_spec.md
  255. Below are the official constants from the xxHash specification. Optimizing
  256. compilers should emit a single "rotate" instruction for the
  257. _PyHASH_XXROTATE() expansion. If that doesn't happen for some important
  258. platform, the macro could be changed to expand to a platform-specific rotate
  259. spelling instead.
  260. */
  261. #if SIZEOF_PY_UHASH_T > 4
  262. #define _PyHASH_XXPRIME_1 ((Py_uhash_t)11400714785074694791ULL)
  263. #define _PyHASH_XXPRIME_2 ((Py_uhash_t)14029467366897019727ULL)
  264. #define _PyHASH_XXPRIME_5 ((Py_uhash_t)2870177450012600261ULL)
  265. #define _PyHASH_XXROTATE(x) ((x << 31) | (x >> 33)) /* Rotate left 31 bits */
  266. #else
  267. #define _PyHASH_XXPRIME_1 ((Py_uhash_t)2654435761UL)
  268. #define _PyHASH_XXPRIME_2 ((Py_uhash_t)2246822519UL)
  269. #define _PyHASH_XXPRIME_5 ((Py_uhash_t)374761393UL)
  270. #define _PyHASH_XXROTATE(x) ((x << 13) | (x >> 19)) /* Rotate left 13 bits */
  271. #endif
  272. /* Tests have shown that it's not worth to cache the hash value, see
  273. https://bugs.python.org/issue9685 */
  274. static Py_hash_t
  275. tuplehash(PyTupleObject *v)
  276. {
  277. Py_ssize_t i, len = Py_SIZE(v);
  278. PyObject **item = v->ob_item;
  279. Py_uhash_t acc = _PyHASH_XXPRIME_5;
  280. for (i = 0; i < len; i++) {
  281. Py_uhash_t lane = PyObject_Hash(item[i]);
  282. if (lane == (Py_uhash_t)-1) {
  283. return -1;
  284. }
  285. acc += lane * _PyHASH_XXPRIME_2;
  286. acc = _PyHASH_XXROTATE(acc);
  287. acc *= _PyHASH_XXPRIME_1;
  288. }
  289. /* Add input length, mangled to keep the historical value of hash(()). */
  290. acc += len ^ (_PyHASH_XXPRIME_5 ^ 3527539UL);
  291. if (acc == (Py_uhash_t)-1) {
  292. return 1546275796;
  293. }
  294. return acc;
  295. }
  296. static Py_ssize_t
  297. tuplelength(PyTupleObject *a)
  298. {
  299. return Py_SIZE(a);
  300. }
  301. static int
  302. tuplecontains(PyTupleObject *a, PyObject *el)
  303. {
  304. Py_ssize_t i;
  305. int cmp;
  306. for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
  307. cmp = PyObject_RichCompareBool(PyTuple_GET_ITEM(a, i), el, Py_EQ);
  308. return cmp;
  309. }
  310. static PyObject *
  311. tupleitem(PyTupleObject *a, Py_ssize_t i)
  312. {
  313. if (i < 0 || i >= Py_SIZE(a)) {
  314. PyErr_SetString(PyExc_IndexError, "tuple index out of range");
  315. return NULL;
  316. }
  317. return Py_NewRef(a->ob_item[i]);
  318. }
  319. PyObject *
  320. _PyTuple_FromArray(PyObject *const *src, Py_ssize_t n)
  321. {
  322. if (n == 0) {
  323. return tuple_get_empty();
  324. }
  325. PyTupleObject *tuple = tuple_alloc(n);
  326. if (tuple == NULL) {
  327. return NULL;
  328. }
  329. PyObject **dst = tuple->ob_item;
  330. for (Py_ssize_t i = 0; i < n; i++) {
  331. PyObject *item = src[i];
  332. dst[i] = Py_NewRef(item);
  333. }
  334. _PyObject_GC_TRACK(tuple);
  335. return (PyObject *)tuple;
  336. }
  337. PyObject *
  338. _PyTuple_FromArraySteal(PyObject *const *src, Py_ssize_t n)
  339. {
  340. if (n == 0) {
  341. return tuple_get_empty();
  342. }
  343. PyTupleObject *tuple = tuple_alloc(n);
  344. if (tuple == NULL) {
  345. for (Py_ssize_t i = 0; i < n; i++) {
  346. Py_DECREF(src[i]);
  347. }
  348. return NULL;
  349. }
  350. PyObject **dst = tuple->ob_item;
  351. for (Py_ssize_t i = 0; i < n; i++) {
  352. PyObject *item = src[i];
  353. dst[i] = item;
  354. }
  355. _PyObject_GC_TRACK(tuple);
  356. return (PyObject *)tuple;
  357. }
  358. static PyObject *
  359. tupleslice(PyTupleObject *a, Py_ssize_t ilow,
  360. Py_ssize_t ihigh)
  361. {
  362. if (ilow < 0)
  363. ilow = 0;
  364. if (ihigh > Py_SIZE(a))
  365. ihigh = Py_SIZE(a);
  366. if (ihigh < ilow)
  367. ihigh = ilow;
  368. if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
  369. return Py_NewRef(a);
  370. }
  371. return _PyTuple_FromArray(a->ob_item + ilow, ihigh - ilow);
  372. }
  373. PyObject *
  374. PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
  375. {
  376. if (op == NULL || !PyTuple_Check(op)) {
  377. PyErr_BadInternalCall();
  378. return NULL;
  379. }
  380. return tupleslice((PyTupleObject *)op, i, j);
  381. }
  382. static PyObject *
  383. tupleconcat(PyTupleObject *a, PyObject *bb)
  384. {
  385. Py_ssize_t size;
  386. Py_ssize_t i;
  387. PyObject **src, **dest;
  388. PyTupleObject *np;
  389. if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
  390. return Py_NewRef(bb);
  391. }
  392. if (!PyTuple_Check(bb)) {
  393. PyErr_Format(PyExc_TypeError,
  394. "can only concatenate tuple (not \"%.200s\") to tuple",
  395. Py_TYPE(bb)->tp_name);
  396. return NULL;
  397. }
  398. PyTupleObject *b = (PyTupleObject *)bb;
  399. if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) {
  400. return Py_NewRef(a);
  401. }
  402. assert((size_t)Py_SIZE(a) + (size_t)Py_SIZE(b) < PY_SSIZE_T_MAX);
  403. size = Py_SIZE(a) + Py_SIZE(b);
  404. if (size == 0) {
  405. return tuple_get_empty();
  406. }
  407. np = tuple_alloc(size);
  408. if (np == NULL) {
  409. return NULL;
  410. }
  411. src = a->ob_item;
  412. dest = np->ob_item;
  413. for (i = 0; i < Py_SIZE(a); i++) {
  414. PyObject *v = src[i];
  415. dest[i] = Py_NewRef(v);
  416. }
  417. src = b->ob_item;
  418. dest = np->ob_item + Py_SIZE(a);
  419. for (i = 0; i < Py_SIZE(b); i++) {
  420. PyObject *v = src[i];
  421. dest[i] = Py_NewRef(v);
  422. }
  423. _PyObject_GC_TRACK(np);
  424. return (PyObject *)np;
  425. }
  426. static PyObject *
  427. tuplerepeat(PyTupleObject *a, Py_ssize_t n)
  428. {
  429. const Py_ssize_t input_size = Py_SIZE(a);
  430. if (input_size == 0 || n == 1) {
  431. if (PyTuple_CheckExact(a)) {
  432. /* Since tuples are immutable, we can return a shared
  433. copy in this case */
  434. return Py_NewRef(a);
  435. }
  436. }
  437. if (input_size == 0 || n <= 0) {
  438. return tuple_get_empty();
  439. }
  440. assert(n>0);
  441. if (input_size > PY_SSIZE_T_MAX / n)
  442. return PyErr_NoMemory();
  443. Py_ssize_t output_size = input_size * n;
  444. PyTupleObject *np = tuple_alloc(output_size);
  445. if (np == NULL)
  446. return NULL;
  447. PyObject **dest = np->ob_item;
  448. if (input_size == 1) {
  449. PyObject *elem = a->ob_item[0];
  450. _Py_RefcntAdd(elem, n);
  451. PyObject **dest_end = dest + output_size;
  452. while (dest < dest_end) {
  453. *dest++ = elem;
  454. }
  455. }
  456. else {
  457. PyObject **src = a->ob_item;
  458. PyObject **src_end = src + input_size;
  459. while (src < src_end) {
  460. _Py_RefcntAdd(*src, n);
  461. *dest++ = *src++;
  462. }
  463. _Py_memory_repeat((char *)np->ob_item, sizeof(PyObject *)*output_size,
  464. sizeof(PyObject *)*input_size);
  465. }
  466. _PyObject_GC_TRACK(np);
  467. return (PyObject *) np;
  468. }
  469. /*[clinic input]
  470. tuple.index
  471. value: object
  472. start: slice_index(accept={int}) = 0
  473. stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
  474. /
  475. Return first index of value.
  476. Raises ValueError if the value is not present.
  477. [clinic start generated code]*/
  478. static PyObject *
  479. tuple_index_impl(PyTupleObject *self, PyObject *value, Py_ssize_t start,
  480. Py_ssize_t stop)
  481. /*[clinic end generated code: output=07b6f9f3cb5c33eb input=fb39e9874a21fe3f]*/
  482. {
  483. Py_ssize_t i;
  484. if (start < 0) {
  485. start += Py_SIZE(self);
  486. if (start < 0)
  487. start = 0;
  488. }
  489. if (stop < 0) {
  490. stop += Py_SIZE(self);
  491. }
  492. else if (stop > Py_SIZE(self)) {
  493. stop = Py_SIZE(self);
  494. }
  495. for (i = start; i < stop; i++) {
  496. int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
  497. if (cmp > 0)
  498. return PyLong_FromSsize_t(i);
  499. else if (cmp < 0)
  500. return NULL;
  501. }
  502. PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
  503. return NULL;
  504. }
  505. /*[clinic input]
  506. tuple.count
  507. value: object
  508. /
  509. Return number of occurrences of value.
  510. [clinic start generated code]*/
  511. static PyObject *
  512. tuple_count(PyTupleObject *self, PyObject *value)
  513. /*[clinic end generated code: output=aa927affc5a97605 input=531721aff65bd772]*/
  514. {
  515. Py_ssize_t count = 0;
  516. Py_ssize_t i;
  517. for (i = 0; i < Py_SIZE(self); i++) {
  518. int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
  519. if (cmp > 0)
  520. count++;
  521. else if (cmp < 0)
  522. return NULL;
  523. }
  524. return PyLong_FromSsize_t(count);
  525. }
  526. static int
  527. tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
  528. {
  529. Py_ssize_t i;
  530. for (i = Py_SIZE(o); --i >= 0; )
  531. Py_VISIT(o->ob_item[i]);
  532. return 0;
  533. }
  534. static PyObject *
  535. tuplerichcompare(PyObject *v, PyObject *w, int op)
  536. {
  537. PyTupleObject *vt, *wt;
  538. Py_ssize_t i;
  539. Py_ssize_t vlen, wlen;
  540. if (!PyTuple_Check(v) || !PyTuple_Check(w))
  541. Py_RETURN_NOTIMPLEMENTED;
  542. vt = (PyTupleObject *)v;
  543. wt = (PyTupleObject *)w;
  544. vlen = Py_SIZE(vt);
  545. wlen = Py_SIZE(wt);
  546. /* Note: the corresponding code for lists has an "early out" test
  547. * here when op is EQ or NE and the lengths differ. That pays there,
  548. * but Tim was unable to find any real code where EQ/NE tuple
  549. * compares don't have the same length, so testing for it here would
  550. * have cost without benefit.
  551. */
  552. /* Search for the first index where items are different.
  553. * Note that because tuples are immutable, it's safe to reuse
  554. * vlen and wlen across the comparison calls.
  555. */
  556. for (i = 0; i < vlen && i < wlen; i++) {
  557. int k = PyObject_RichCompareBool(vt->ob_item[i],
  558. wt->ob_item[i], Py_EQ);
  559. if (k < 0)
  560. return NULL;
  561. if (!k)
  562. break;
  563. }
  564. if (i >= vlen || i >= wlen) {
  565. /* No more items to compare -- compare sizes */
  566. Py_RETURN_RICHCOMPARE(vlen, wlen, op);
  567. }
  568. /* We have an item that differs -- shortcuts for EQ/NE */
  569. if (op == Py_EQ) {
  570. Py_RETURN_FALSE;
  571. }
  572. if (op == Py_NE) {
  573. Py_RETURN_TRUE;
  574. }
  575. /* Compare the final item again using the proper operator */
  576. return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
  577. }
  578. static PyObject *
  579. tuple_subtype_new(PyTypeObject *type, PyObject *iterable);
  580. /*[clinic input]
  581. @classmethod
  582. tuple.__new__ as tuple_new
  583. iterable: object(c_default="NULL") = ()
  584. /
  585. Built-in immutable sequence.
  586. If no argument is given, the constructor returns an empty tuple.
  587. If iterable is specified the tuple is initialized from iterable's items.
  588. If the argument is a tuple, the return value is the same object.
  589. [clinic start generated code]*/
  590. static PyObject *
  591. tuple_new_impl(PyTypeObject *type, PyObject *iterable)
  592. /*[clinic end generated code: output=4546d9f0d469bce7 input=86963bcde633b5a2]*/
  593. {
  594. if (type != &PyTuple_Type)
  595. return tuple_subtype_new(type, iterable);
  596. if (iterable == NULL) {
  597. return tuple_get_empty();
  598. }
  599. else {
  600. return PySequence_Tuple(iterable);
  601. }
  602. }
  603. static PyObject *
  604. tuple_vectorcall(PyObject *type, PyObject * const*args,
  605. size_t nargsf, PyObject *kwnames)
  606. {
  607. if (!_PyArg_NoKwnames("tuple", kwnames)) {
  608. return NULL;
  609. }
  610. Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
  611. if (!_PyArg_CheckPositional("tuple", nargs, 0, 1)) {
  612. return NULL;
  613. }
  614. if (nargs) {
  615. return tuple_new_impl(_PyType_CAST(type), args[0]);
  616. }
  617. else {
  618. return tuple_get_empty();
  619. }
  620. }
  621. static PyObject *
  622. tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
  623. {
  624. PyObject *tmp, *newobj, *item;
  625. Py_ssize_t i, n;
  626. assert(PyType_IsSubtype(type, &PyTuple_Type));
  627. // tuple subclasses must implement the GC protocol
  628. assert(_PyType_IS_GC(type));
  629. tmp = tuple_new_impl(&PyTuple_Type, iterable);
  630. if (tmp == NULL)
  631. return NULL;
  632. assert(PyTuple_Check(tmp));
  633. /* This may allocate an empty tuple that is not the global one. */
  634. newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
  635. if (newobj == NULL) {
  636. Py_DECREF(tmp);
  637. return NULL;
  638. }
  639. for (i = 0; i < n; i++) {
  640. item = PyTuple_GET_ITEM(tmp, i);
  641. PyTuple_SET_ITEM(newobj, i, Py_NewRef(item));
  642. }
  643. Py_DECREF(tmp);
  644. // Don't track if a subclass tp_alloc is PyType_GenericAlloc()
  645. if (!_PyObject_GC_IS_TRACKED(newobj)) {
  646. _PyObject_GC_TRACK(newobj);
  647. }
  648. return newobj;
  649. }
  650. static PySequenceMethods tuple_as_sequence = {
  651. (lenfunc)tuplelength, /* sq_length */
  652. (binaryfunc)tupleconcat, /* sq_concat */
  653. (ssizeargfunc)tuplerepeat, /* sq_repeat */
  654. (ssizeargfunc)tupleitem, /* sq_item */
  655. 0, /* sq_slice */
  656. 0, /* sq_ass_item */
  657. 0, /* sq_ass_slice */
  658. (objobjproc)tuplecontains, /* sq_contains */
  659. };
  660. static PyObject*
  661. tuplesubscript(PyTupleObject* self, PyObject* item)
  662. {
  663. if (_PyIndex_Check(item)) {
  664. Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
  665. if (i == -1 && PyErr_Occurred())
  666. return NULL;
  667. if (i < 0)
  668. i += PyTuple_GET_SIZE(self);
  669. return tupleitem(self, i);
  670. }
  671. else if (PySlice_Check(item)) {
  672. Py_ssize_t start, stop, step, slicelength, i;
  673. size_t cur;
  674. PyObject* it;
  675. PyObject **src, **dest;
  676. if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
  677. return NULL;
  678. }
  679. slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start,
  680. &stop, step);
  681. if (slicelength <= 0) {
  682. return tuple_get_empty();
  683. }
  684. else if (start == 0 && step == 1 &&
  685. slicelength == PyTuple_GET_SIZE(self) &&
  686. PyTuple_CheckExact(self)) {
  687. return Py_NewRef(self);
  688. }
  689. else {
  690. PyTupleObject* result = tuple_alloc(slicelength);
  691. if (!result) return NULL;
  692. src = self->ob_item;
  693. dest = result->ob_item;
  694. for (cur = start, i = 0; i < slicelength;
  695. cur += step, i++) {
  696. it = Py_NewRef(src[cur]);
  697. dest[i] = it;
  698. }
  699. _PyObject_GC_TRACK(result);
  700. return (PyObject *)result;
  701. }
  702. }
  703. else {
  704. PyErr_Format(PyExc_TypeError,
  705. "tuple indices must be integers or slices, not %.200s",
  706. Py_TYPE(item)->tp_name);
  707. return NULL;
  708. }
  709. }
  710. /*[clinic input]
  711. tuple.__getnewargs__
  712. [clinic start generated code]*/
  713. static PyObject *
  714. tuple___getnewargs___impl(PyTupleObject *self)
  715. /*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/
  716. {
  717. return Py_BuildValue("(N)", tupleslice(self, 0, Py_SIZE(self)));
  718. }
  719. static PyMethodDef tuple_methods[] = {
  720. TUPLE___GETNEWARGS___METHODDEF
  721. TUPLE_INDEX_METHODDEF
  722. TUPLE_COUNT_METHODDEF
  723. {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
  724. {NULL, NULL} /* sentinel */
  725. };
  726. static PyMappingMethods tuple_as_mapping = {
  727. (lenfunc)tuplelength,
  728. (binaryfunc)tuplesubscript,
  729. 0
  730. };
  731. static PyObject *tuple_iter(PyObject *seq);
  732. PyTypeObject PyTuple_Type = {
  733. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  734. "tuple",
  735. sizeof(PyTupleObject) - sizeof(PyObject *),
  736. sizeof(PyObject *),
  737. (destructor)tupledealloc, /* tp_dealloc */
  738. 0, /* tp_vectorcall_offset */
  739. 0, /* tp_getattr */
  740. 0, /* tp_setattr */
  741. 0, /* tp_as_async */
  742. (reprfunc)tuplerepr, /* tp_repr */
  743. 0, /* tp_as_number */
  744. &tuple_as_sequence, /* tp_as_sequence */
  745. &tuple_as_mapping, /* tp_as_mapping */
  746. (hashfunc)tuplehash, /* tp_hash */
  747. 0, /* tp_call */
  748. 0, /* tp_str */
  749. PyObject_GenericGetAttr, /* tp_getattro */
  750. 0, /* tp_setattro */
  751. 0, /* tp_as_buffer */
  752. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  753. Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS |
  754. _Py_TPFLAGS_MATCH_SELF | Py_TPFLAGS_SEQUENCE, /* tp_flags */
  755. tuple_new__doc__, /* tp_doc */
  756. (traverseproc)tupletraverse, /* tp_traverse */
  757. 0, /* tp_clear */
  758. tuplerichcompare, /* tp_richcompare */
  759. 0, /* tp_weaklistoffset */
  760. tuple_iter, /* tp_iter */
  761. 0, /* tp_iternext */
  762. tuple_methods, /* tp_methods */
  763. 0, /* tp_members */
  764. 0, /* tp_getset */
  765. 0, /* tp_base */
  766. 0, /* tp_dict */
  767. 0, /* tp_descr_get */
  768. 0, /* tp_descr_set */
  769. 0, /* tp_dictoffset */
  770. 0, /* tp_init */
  771. 0, /* tp_alloc */
  772. tuple_new, /* tp_new */
  773. PyObject_GC_Del, /* tp_free */
  774. .tp_vectorcall = tuple_vectorcall,
  775. };
  776. /* The following function breaks the notion that tuples are immutable:
  777. it changes the size of a tuple. We get away with this only if there
  778. is only one module referencing the object. You can also think of it
  779. as creating a new tuple object and destroying the old one, only more
  780. efficiently. In any case, don't use this if the tuple may already be
  781. known to some other part of the code. */
  782. int
  783. _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
  784. {
  785. PyTupleObject *v;
  786. PyTupleObject *sv;
  787. Py_ssize_t i;
  788. Py_ssize_t oldsize;
  789. v = (PyTupleObject *) *pv;
  790. if (v == NULL || !Py_IS_TYPE(v, &PyTuple_Type) ||
  791. (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
  792. *pv = 0;
  793. Py_XDECREF(v);
  794. PyErr_BadInternalCall();
  795. return -1;
  796. }
  797. oldsize = Py_SIZE(v);
  798. if (oldsize == newsize) {
  799. return 0;
  800. }
  801. if (newsize == 0) {
  802. Py_DECREF(v);
  803. *pv = tuple_get_empty();
  804. return 0;
  805. }
  806. if (oldsize == 0) {
  807. #ifdef Py_DEBUG
  808. assert(v == &_Py_SINGLETON(tuple_empty));
  809. #endif
  810. /* The empty tuple is statically allocated so we never
  811. resize it in-place. */
  812. Py_DECREF(v);
  813. *pv = PyTuple_New(newsize);
  814. return *pv == NULL ? -1 : 0;
  815. }
  816. if (_PyObject_GC_IS_TRACKED(v)) {
  817. _PyObject_GC_UNTRACK(v);
  818. }
  819. #ifdef Py_TRACE_REFS
  820. _Py_ForgetReference((PyObject *) v);
  821. #endif
  822. /* DECREF items deleted by shrinkage */
  823. for (i = newsize; i < oldsize; i++) {
  824. Py_CLEAR(v->ob_item[i]);
  825. }
  826. sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
  827. if (sv == NULL) {
  828. *pv = NULL;
  829. #ifdef Py_REF_DEBUG
  830. _Py_DecRefTotal(_PyInterpreterState_GET());
  831. #endif
  832. PyObject_GC_Del(v);
  833. return -1;
  834. }
  835. _Py_NewReferenceNoTotal((PyObject *) sv);
  836. /* Zero out items added by growing */
  837. if (newsize > oldsize)
  838. memset(&sv->ob_item[oldsize], 0,
  839. sizeof(*sv->ob_item) * (newsize - oldsize));
  840. *pv = (PyObject *) sv;
  841. _PyObject_GC_TRACK(sv);
  842. return 0;
  843. }
  844. static void maybe_freelist_clear(PyInterpreterState *, int);
  845. void
  846. _PyTuple_Fini(PyInterpreterState *interp)
  847. {
  848. maybe_freelist_clear(interp, 1);
  849. }
  850. void
  851. _PyTuple_ClearFreeList(PyInterpreterState *interp)
  852. {
  853. maybe_freelist_clear(interp, 0);
  854. }
  855. /*********************** Tuple Iterator **************************/
  856. static void
  857. tupleiter_dealloc(_PyTupleIterObject *it)
  858. {
  859. _PyObject_GC_UNTRACK(it);
  860. Py_XDECREF(it->it_seq);
  861. PyObject_GC_Del(it);
  862. }
  863. static int
  864. tupleiter_traverse(_PyTupleIterObject *it, visitproc visit, void *arg)
  865. {
  866. Py_VISIT(it->it_seq);
  867. return 0;
  868. }
  869. static PyObject *
  870. tupleiter_next(_PyTupleIterObject *it)
  871. {
  872. PyTupleObject *seq;
  873. PyObject *item;
  874. assert(it != NULL);
  875. seq = it->it_seq;
  876. if (seq == NULL)
  877. return NULL;
  878. assert(PyTuple_Check(seq));
  879. if (it->it_index < PyTuple_GET_SIZE(seq)) {
  880. item = PyTuple_GET_ITEM(seq, it->it_index);
  881. ++it->it_index;
  882. return Py_NewRef(item);
  883. }
  884. it->it_seq = NULL;
  885. Py_DECREF(seq);
  886. return NULL;
  887. }
  888. static PyObject *
  889. tupleiter_len(_PyTupleIterObject *it, PyObject *Py_UNUSED(ignored))
  890. {
  891. Py_ssize_t len = 0;
  892. if (it->it_seq)
  893. len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
  894. return PyLong_FromSsize_t(len);
  895. }
  896. PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
  897. static PyObject *
  898. tupleiter_reduce(_PyTupleIterObject *it, PyObject *Py_UNUSED(ignored))
  899. {
  900. PyObject *iter = _PyEval_GetBuiltin(&_Py_ID(iter));
  901. /* _PyEval_GetBuiltin can invoke arbitrary code,
  902. * call must be before access of iterator pointers.
  903. * see issue #101765 */
  904. if (it->it_seq)
  905. return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index);
  906. else
  907. return Py_BuildValue("N(())", iter);
  908. }
  909. static PyObject *
  910. tupleiter_setstate(_PyTupleIterObject *it, PyObject *state)
  911. {
  912. Py_ssize_t index = PyLong_AsSsize_t(state);
  913. if (index == -1 && PyErr_Occurred())
  914. return NULL;
  915. if (it->it_seq != NULL) {
  916. if (index < 0)
  917. index = 0;
  918. else if (index > PyTuple_GET_SIZE(it->it_seq))
  919. index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
  920. it->it_index = index;
  921. }
  922. Py_RETURN_NONE;
  923. }
  924. PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
  925. PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
  926. static PyMethodDef tupleiter_methods[] = {
  927. {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
  928. {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
  929. {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
  930. {NULL, NULL} /* sentinel */
  931. };
  932. PyTypeObject PyTupleIter_Type = {
  933. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  934. "tuple_iterator", /* tp_name */
  935. sizeof(_PyTupleIterObject), /* tp_basicsize */
  936. 0, /* tp_itemsize */
  937. /* methods */
  938. (destructor)tupleiter_dealloc, /* tp_dealloc */
  939. 0, /* tp_vectorcall_offset */
  940. 0, /* tp_getattr */
  941. 0, /* tp_setattr */
  942. 0, /* tp_as_async */
  943. 0, /* tp_repr */
  944. 0, /* tp_as_number */
  945. 0, /* tp_as_sequence */
  946. 0, /* tp_as_mapping */
  947. 0, /* tp_hash */
  948. 0, /* tp_call */
  949. 0, /* tp_str */
  950. PyObject_GenericGetAttr, /* tp_getattro */
  951. 0, /* tp_setattro */
  952. 0, /* tp_as_buffer */
  953. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
  954. 0, /* tp_doc */
  955. (traverseproc)tupleiter_traverse, /* tp_traverse */
  956. 0, /* tp_clear */
  957. 0, /* tp_richcompare */
  958. 0, /* tp_weaklistoffset */
  959. PyObject_SelfIter, /* tp_iter */
  960. (iternextfunc)tupleiter_next, /* tp_iternext */
  961. tupleiter_methods, /* tp_methods */
  962. 0,
  963. };
  964. static PyObject *
  965. tuple_iter(PyObject *seq)
  966. {
  967. _PyTupleIterObject *it;
  968. if (!PyTuple_Check(seq)) {
  969. PyErr_BadInternalCall();
  970. return NULL;
  971. }
  972. it = PyObject_GC_New(_PyTupleIterObject, &PyTupleIter_Type);
  973. if (it == NULL)
  974. return NULL;
  975. it->it_index = 0;
  976. it->it_seq = (PyTupleObject *)Py_NewRef(seq);
  977. _PyObject_GC_TRACK(it);
  978. return (PyObject *)it;
  979. }
  980. /*************
  981. * freelists *
  982. *************/
  983. #define STATE (interp->tuple)
  984. #define FREELIST_FINALIZED (STATE.numfree[0] < 0)
  985. static inline PyTupleObject *
  986. maybe_freelist_pop(Py_ssize_t size)
  987. {
  988. #if PyTuple_NFREELISTS > 0
  989. PyInterpreterState *interp = _PyInterpreterState_GET();
  990. #ifdef Py_DEBUG
  991. /* maybe_freelist_pop() must not be called after maybe_freelist_fini(). */
  992. assert(!FREELIST_FINALIZED);
  993. #endif
  994. if (size == 0) {
  995. return NULL;
  996. }
  997. assert(size > 0);
  998. if (size <= PyTuple_MAXSAVESIZE) {
  999. Py_ssize_t index = size - 1;
  1000. PyTupleObject *op = STATE.free_list[index];
  1001. if (op != NULL) {
  1002. /* op is the head of a linked list, with the first item
  1003. pointing to the next node. Here we pop off the old head. */
  1004. STATE.free_list[index] = (PyTupleObject *) op->ob_item[0];
  1005. STATE.numfree[index]--;
  1006. /* Inlined _PyObject_InitVar() without _PyType_HasFeature() test */
  1007. #ifdef Py_TRACE_REFS
  1008. /* maybe_freelist_push() ensures these were already set. */
  1009. // XXX Can we drop these? See commit 68055ce6fe01 (GvR, Dec 1998).
  1010. Py_SET_SIZE(op, size);
  1011. Py_SET_TYPE(op, &PyTuple_Type);
  1012. #endif
  1013. _Py_NewReference((PyObject *)op);
  1014. /* END inlined _PyObject_InitVar() */
  1015. OBJECT_STAT_INC(from_freelist);
  1016. return op;
  1017. }
  1018. }
  1019. #endif
  1020. return NULL;
  1021. }
  1022. static inline int
  1023. maybe_freelist_push(PyTupleObject *op)
  1024. {
  1025. #if PyTuple_NFREELISTS > 0
  1026. PyInterpreterState *interp = _PyInterpreterState_GET();
  1027. #ifdef Py_DEBUG
  1028. /* maybe_freelist_push() must not be called after maybe_freelist_fini(). */
  1029. assert(!FREELIST_FINALIZED);
  1030. #endif
  1031. if (Py_SIZE(op) == 0) {
  1032. return 0;
  1033. }
  1034. Py_ssize_t index = Py_SIZE(op) - 1;
  1035. if (index < PyTuple_NFREELISTS
  1036. && STATE.numfree[index] < PyTuple_MAXFREELIST
  1037. && Py_IS_TYPE(op, &PyTuple_Type))
  1038. {
  1039. /* op is the head of a linked list, with the first item
  1040. pointing to the next node. Here we set op as the new head. */
  1041. op->ob_item[0] = (PyObject *) STATE.free_list[index];
  1042. STATE.free_list[index] = op;
  1043. STATE.numfree[index]++;
  1044. OBJECT_STAT_INC(to_freelist);
  1045. return 1;
  1046. }
  1047. #endif
  1048. return 0;
  1049. }
  1050. static void
  1051. maybe_freelist_clear(PyInterpreterState *interp, int fini)
  1052. {
  1053. #if PyTuple_NFREELISTS > 0
  1054. for (Py_ssize_t i = 0; i < PyTuple_NFREELISTS; i++) {
  1055. PyTupleObject *p = STATE.free_list[i];
  1056. STATE.free_list[i] = NULL;
  1057. STATE.numfree[i] = fini ? -1 : 0;
  1058. while (p) {
  1059. PyTupleObject *q = p;
  1060. p = (PyTupleObject *)(p->ob_item[0]);
  1061. PyObject_GC_Del(q);
  1062. }
  1063. }
  1064. #endif
  1065. }
  1066. /* Print summary info about the state of the optimized allocator */
  1067. void
  1068. _PyTuple_DebugMallocStats(FILE *out)
  1069. {
  1070. #if PyTuple_NFREELISTS > 0
  1071. PyInterpreterState *interp = _PyInterpreterState_GET();
  1072. for (int i = 0; i < PyTuple_NFREELISTS; i++) {
  1073. int len = i + 1;
  1074. char buf[128];
  1075. PyOS_snprintf(buf, sizeof(buf),
  1076. "free %d-sized PyTupleObject", len);
  1077. _PyDebugAllocatorStats(out, buf, STATE.numfree[i],
  1078. _PyObject_VAR_SIZE(&PyTuple_Type, len));
  1079. }
  1080. #endif
  1081. }
  1082. #undef STATE
  1083. #undef FREELIST_FINALIZED