/Doc/c-api/tuple.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 164 lines · 96 code · 68 blank · 0 comment · 0 complexity · 764ae9d04752bebe88d346fff615e564 MD5 · raw file

  1. .. highlightlang:: c
  2. .. _tupleobjects:
  3. Tuple Objects
  4. -------------
  5. .. index:: object: tuple
  6. .. ctype:: PyTupleObject
  7. This subtype of :ctype:`PyObject` represents a Python tuple object.
  8. .. cvar:: PyTypeObject PyTuple_Type
  9. .. index:: single: TupleType (in module types)
  10. This instance of :ctype:`PyTypeObject` represents the Python tuple type; it is
  11. the same object as ``tuple`` and ``types.TupleType`` in the Python layer..
  12. .. cfunction:: int PyTuple_Check(PyObject *p)
  13. Return true if *p* is a tuple object or an instance of a subtype of the tuple
  14. type.
  15. .. versionchanged:: 2.2
  16. Allowed subtypes to be accepted.
  17. .. cfunction:: int PyTuple_CheckExact(PyObject *p)
  18. Return true if *p* is a tuple object, but not an instance of a subtype of the
  19. tuple type.
  20. .. versionadded:: 2.2
  21. .. cfunction:: PyObject* PyTuple_New(Py_ssize_t len)
  22. Return a new tuple object of size *len*, or *NULL* on failure.
  23. .. versionchanged:: 2.5
  24. This function used an :ctype:`int` type for *len*. This might require
  25. changes in your code for properly supporting 64-bit systems.
  26. .. cfunction:: PyObject* PyTuple_Pack(Py_ssize_t n, ...)
  27. Return a new tuple object of size *n*, or *NULL* on failure. The tuple values
  28. are initialized to the subsequent *n* C arguments pointing to Python objects.
  29. ``PyTuple_Pack(2, a, b)`` is equivalent to ``Py_BuildValue("(OO)", a, b)``.
  30. .. versionadded:: 2.4
  31. .. versionchanged:: 2.5
  32. This function used an :ctype:`int` type for *n*. This might require
  33. changes in your code for properly supporting 64-bit systems.
  34. .. cfunction:: Py_ssize_t PyTuple_Size(PyObject *p)
  35. Take a pointer to a tuple object, and return the size of that tuple.
  36. .. versionchanged:: 2.5
  37. This function returned an :ctype:`int` type. This might require changes
  38. in your code for properly supporting 64-bit systems.
  39. .. cfunction:: Py_ssize_t PyTuple_GET_SIZE(PyObject *p)
  40. Return the size of the tuple *p*, which must be non-*NULL* and point to a tuple;
  41. no error checking is performed.
  42. .. versionchanged:: 2.5
  43. This function returned an :ctype:`int` type. This might require changes
  44. in your code for properly supporting 64-bit systems.
  45. .. cfunction:: PyObject* PyTuple_GetItem(PyObject *p, Py_ssize_t pos)
  46. Return the object at position *pos* in the tuple pointed to by *p*. If *pos* is
  47. out of bounds, return *NULL* and sets an :exc:`IndexError` exception.
  48. .. versionchanged:: 2.5
  49. This function used an :ctype:`int` type for *pos*. This might require
  50. changes in your code for properly supporting 64-bit systems.
  51. .. cfunction:: PyObject* PyTuple_GET_ITEM(PyObject *p, Py_ssize_t pos)
  52. Like :cfunc:`PyTuple_GetItem`, but does no checking of its arguments.
  53. .. versionchanged:: 2.5
  54. This function used an :ctype:`int` type for *pos*. This might require
  55. changes in your code for properly supporting 64-bit systems.
  56. .. cfunction:: PyObject* PyTuple_GetSlice(PyObject *p, Py_ssize_t low, Py_ssize_t high)
  57. Take a slice of the tuple pointed to by *p* from *low* to *high* and return it
  58. as a new tuple.
  59. .. versionchanged:: 2.5
  60. This function used an :ctype:`int` type for *low* and *high*. This might
  61. require changes in your code for properly supporting 64-bit systems.
  62. .. cfunction:: int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)
  63. Insert a reference to object *o* at position *pos* of the tuple pointed to by
  64. *p*. Return ``0`` on success.
  65. .. note::
  66. This function "steals" a reference to *o*.
  67. .. versionchanged:: 2.5
  68. This function used an :ctype:`int` type for *pos*. This might require
  69. changes in your code for properly supporting 64-bit systems.
  70. .. cfunction:: void PyTuple_SET_ITEM(PyObject *p, Py_ssize_t pos, PyObject *o)
  71. Like :cfunc:`PyTuple_SetItem`, but does no error checking, and should *only* be
  72. used to fill in brand new tuples.
  73. .. note::
  74. This function "steals" a reference to *o*.
  75. .. versionchanged:: 2.5
  76. This function used an :ctype:`int` type for *pos*. This might require
  77. changes in your code for properly supporting 64-bit systems.
  78. .. cfunction:: int _PyTuple_Resize(PyObject **p, Py_ssize_t newsize)
  79. Can be used to resize a tuple. *newsize* will be the new length of the tuple.
  80. Because tuples are *supposed* to be immutable, this should only be used if there
  81. is only one reference to the object. Do *not* use this if the tuple may already
  82. be known to some other part of the code. The tuple will always grow or shrink
  83. at the end. Think of this as destroying the old tuple and creating a new one,
  84. only more efficiently. Returns ``0`` on success. Client code should never
  85. assume that the resulting value of ``*p`` will be the same as before calling
  86. this function. If the object referenced by ``*p`` is replaced, the original
  87. ``*p`` is destroyed. On failure, returns ``-1`` and sets ``*p`` to *NULL*, and
  88. raises :exc:`MemoryError` or :exc:`SystemError`.
  89. .. versionchanged:: 2.2
  90. Removed unused third parameter, *last_is_sticky*.
  91. .. versionchanged:: 2.5
  92. This function used an :ctype:`int` type for *newsize*. This might
  93. require changes in your code for properly supporting 64-bit systems.
  94. .. cfunction:: int PyTuple_ClearFreeList(void)
  95. Clear the free list. Return the total number of freed items.
  96. .. versionadded:: 2.6