listobject.h 2.71 KB
Newer Older
1

Guido van Rossum's avatar
Guido van Rossum committed
2 3 4 5 6 7 8
/* List object interface */

/*
Another generally useful object type is an list of object pointers.
This is a mutable type: the list items can be changed, and items can be
added or removed.  Out-of-range indices or non-list objects are ignored.

9
*** WARNING *** PyList_SetItem does not increment the new item's reference
Guido van Rossum's avatar
Guido van Rossum committed
10 11
count, but does decrement the reference count of the item it replaces,
if not nil.  It does *decrement* the reference count if it is *not*
12
inserted in the list.  Similarly, PyList_GetItem does not increment the
Guido van Rossum's avatar
Guido van Rossum committed
13 14 15
returned item's reference count.
*/

16 17 18 19 20 21
#ifndef Py_LISTOBJECT_H
#define Py_LISTOBJECT_H
#ifdef __cplusplus
extern "C" {
#endif

22
#ifndef Py_LIMITED_API
Guido van Rossum's avatar
Guido van Rossum committed
23
typedef struct {
24
    PyObject_VAR_HEAD
Neal Norwitz's avatar
Neal Norwitz committed
25
    /* Vector of pointers to list elements.  list[0] is ob_item[0], etc. */
26
    PyObject **ob_item;
27 28 29 30 31 32 33

    /* ob_item contains space for 'allocated' elements.  The number
     * currently in use is ob_size.
     * Invariants:
     *     0 <= ob_size <= allocated
     *     len(list) == ob_size
     *     ob_item == NULL implies ob_size == allocated == 0
34
     * list.sort() temporarily sets allocated to -1 to detect mutations.
35 36 37
     *
     * Items must normally not be NULL, except during construction when
     * the list is not yet visible outside the function that builds it.
38
     */
Martin v. Löwis's avatar
Martin v. Löwis committed
39
    Py_ssize_t allocated;
40
} PyListObject;
41
#endif
Guido van Rossum's avatar
Guido van Rossum committed
42

43
PyAPI_DATA(PyTypeObject) PyList_Type;
44 45 46
PyAPI_DATA(PyTypeObject) PyListIter_Type;
PyAPI_DATA(PyTypeObject) PyListRevIter_Type;
PyAPI_DATA(PyTypeObject) PySortWrapper_Type;
Guido van Rossum's avatar
Guido van Rossum committed
47

48
#define PyList_Check(op) \
49 50
		PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_LIST_SUBCLASS)
#define PyList_CheckExact(op) (Py_TYPE(op) == &PyList_Type)
Guido van Rossum's avatar
Guido van Rossum committed
51

Martin v. Löwis's avatar
Martin v. Löwis committed
52 53 54 55 56
PyAPI_FUNC(PyObject *) PyList_New(Py_ssize_t size);
PyAPI_FUNC(Py_ssize_t) PyList_Size(PyObject *);
PyAPI_FUNC(PyObject *) PyList_GetItem(PyObject *, Py_ssize_t);
PyAPI_FUNC(int) PyList_SetItem(PyObject *, Py_ssize_t, PyObject *);
PyAPI_FUNC(int) PyList_Insert(PyObject *, Py_ssize_t, PyObject *);
57
PyAPI_FUNC(int) PyList_Append(PyObject *, PyObject *);
Martin v. Löwis's avatar
Martin v. Löwis committed
58 59
PyAPI_FUNC(PyObject *) PyList_GetSlice(PyObject *, Py_ssize_t, Py_ssize_t);
PyAPI_FUNC(int) PyList_SetSlice(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
60 61 62
PyAPI_FUNC(int) PyList_Sort(PyObject *);
PyAPI_FUNC(int) PyList_Reverse(PyObject *);
PyAPI_FUNC(PyObject *) PyList_AsTuple(PyObject *);
63
#ifndef Py_LIMITED_API
64
PyAPI_FUNC(PyObject *) _PyList_Extend(PyListObject *, PyObject *);
65 66

PyAPI_FUNC(int) PyList_ClearFreeList(void);
67
#endif
Guido van Rossum's avatar
Guido van Rossum committed
68 69

/* Macro, trading safety for speed */
70
#ifndef Py_LIMITED_API
71
#define PyList_GET_ITEM(op, i) (((PyListObject *)(op))->ob_item[i])
72
#define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
73
#define PyList_GET_SIZE(op)    Py_SIZE(op)
74
#endif
75 76 77 78 79

#ifdef __cplusplus
}
#endif
#endif /* !Py_LISTOBJECT_H */