# Writing a C API test case for PyTuple

**URL:** <https://discuss.python.org/t/writing-a-c-api-test-case-for-pytuple/37857>\
**Category:** Core Development\
**Tags:** help, c-api\
**Created:** [November 4, 2023, 3:42am UTC](https://discuss.python.org/t/writing-a-c-api-test-case-for-pytuple/37857 "2023-11-04T03:42:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rawwar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rawwar/32/7482_2.png) [@rawwar](https://discuss.python.org/u/rawwar)\
**Post date:** [November 4, 2023, 3:42am UTC](https://discuss.python.org/t/writing-a-c-api-test-case-for-pytuple/37857/1 "2023-11-04T03:42:25Z")

</div>

I am working on a writing C API test cases for PyTuple in [this PR](https://github.com/python/cpython/pull/111606). I am having hard time writing test cases for the following C API’s.

1. \_PyTuple\_Resize
2. PyTuple\_Pack
3. PyTuple\_SetItem
4. PyTuple\_SET\_ITEM

I would appreciate any ideas. Thank you.

---

<div class="post-metadata">

**Author:** ![storchaka](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/storchaka/32/217_2.png) [@storchaka](https://discuss.python.org/u/storchaka)\
**Post date:** [November 4, 2023, 1:24pm UTC](https://discuss.python.org/t/writing-a-c-api-test-case-for-pytuple/37857/2 "2023-11-04T13:24:09Z")

</div>

`PyTuple_Pack`. Look at `py_buildvalue` in `_testcapimodule.c` and the corresponding test.

`PyTuple_SetItem`. It is not safe to modify normal tuple. You have to create a copy of the specified tuple (using `PyTuple_New` and `PyTuple_SET_ITEM`) and then call `PyTuple_SetItem` for it. See for example how `unicode_copy` is used in `_testcapi/unicode.c`. Return the modified tuple.

`PyTuple_SET_ITEM`. The same as for `PyTuple_SetItem`, but you have to decref the old value.

`_PyTuple_Resize`. More or less the same, but it includes some corner cases, like gc-tracked and not gc-tracked tuple. I cannot say how to test this without trying to do this. You can only add simpler tests. It is surprizing that `_PyTuple_Resize` does not check that the tuple is not shared, perhaps it will be added later.
