Writing a C API test case for PyTuple

I am working on a writing C API test cases for PyTuple in this PR. 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.

1 Like

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.

1 Like