Skip to content

Commit bec4186

Browse files
authored
bpo-35134: Create Include/cpython/listobject.h (GH-18395)
Move listobject.h code surrounded by "#ifndef Py_LIMITED_API" to a new Include/cpython/listobject.h header file. Add cpython/ header files to Makefile.pre.in and pythoncore project of PCbuild. Add _PyList_CAST() macro.
1 parent d2ec81a commit bec4186

File tree

5 files changed

+64
-43
lines changed

5 files changed

+64
-43
lines changed

Include/cpython/listobject.h

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#ifndef Py_CPYTHON_LISTOBJECT_H
2+
# error "this header file must not be included directly"
3+
#endif
4+
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
typedef struct {
10+
PyObject_VAR_HEAD
11+
/* Vector of pointers to list elements. list[0] is ob_item[0], etc. */
12+
PyObject **ob_item;
13+
14+
/* ob_item contains space for 'allocated' elements. The number
15+
* currently in use is ob_size.
16+
* Invariants:
17+
* 0 <= ob_size <= allocated
18+
* len(list) == ob_size
19+
* ob_item == NULL implies ob_size == allocated == 0
20+
* list.sort() temporarily sets allocated to -1 to detect mutations.
21+
*
22+
* Items must normally not be NULL, except during construction when
23+
* the list is not yet visible outside the function that builds it.
24+
*/
25+
Py_ssize_t allocated;
26+
} PyListObject;
27+
28+
PyAPI_FUNC(PyObject *) _PyList_Extend(PyListObject *, PyObject *);
29+
PyAPI_FUNC(int) PyList_ClearFreeList(void);
30+
PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out);
31+
32+
/* Macro, trading safety for speed */
33+
34+
/* Cast argument to PyTupleObject* type. */
35+
#define _PyList_CAST(op) (assert(PyList_Check(op)), (PyListObject *)(op))
36+
37+
#define PyList_GET_ITEM(op, i) (_PyList_CAST(op)->ob_item[i])
38+
#define PyList_SET_ITEM(op, i, v) (_PyList_CAST(op)->ob_item[i] = (v))
39+
#define PyList_GET_SIZE(op) Py_SIZE(_PyList_CAST(op))
40+
#define _PyList_ITEMS(op) (_PyList_CAST(op)->ob_item)
41+
42+
#ifdef __cplusplus
43+
}
44+
#endif

Include/listobject.h

+15-43
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1+
/* List object interface
12
2-
/* List object interface */
3+
Another generally useful object type is a list of object pointers.
4+
This is a mutable type: the list items can be changed, and items can be
5+
added or removed. Out-of-range indices or non-list objects are ignored.
36
4-
/*
5-
Another generally useful object type is a list of object pointers.
6-
This is a mutable type: the list items can be changed, and items can be
7-
added or removed. Out-of-range indices or non-list objects are ignored.
8-
9-
*** WARNING *** PyList_SetItem does not increment the new item's reference
10-
count, but does decrement the reference count of the item it replaces,
11-
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
13-
returned item's reference count.
7+
WARNING: PyList_SetItem does not increment the new item's reference count,
8+
but does decrement the reference count of the item it replaces, if not nil.
9+
It does *decrement* the reference count if it is *not* inserted in the list.
10+
Similarly, PyList_GetItem does not increment the returned item's reference
11+
count.
1412
*/
1513

1614
#ifndef Py_LISTOBJECT_H
@@ -19,27 +17,6 @@ returned item's reference count.
1917
extern "C" {
2018
#endif
2119

22-
#ifndef Py_LIMITED_API
23-
typedef struct {
24-
PyObject_VAR_HEAD
25-
/* Vector of pointers to list elements. list[0] is ob_item[0], etc. */
26-
PyObject **ob_item;
27-
28-
/* ob_item contains space for 'allocated' elements. The number
29-
* currently in use is ob_size.
30-
* Invariants:
31-
* 0 <= ob_size <= allocated
32-
* len(list) == ob_size
33-
* ob_item == NULL implies ob_size == allocated == 0
34-
* list.sort() temporarily sets allocated to -1 to detect mutations.
35-
*
36-
* Items must normally not be NULL, except during construction when
37-
* the list is not yet visible outside the function that builds it.
38-
*/
39-
Py_ssize_t allocated;
40-
} PyListObject;
41-
#endif
42-
4320
PyAPI_DATA(PyTypeObject) PyList_Type;
4421
PyAPI_DATA(PyTypeObject) PyListIter_Type;
4522
PyAPI_DATA(PyTypeObject) PyListRevIter_Type;
@@ -50,28 +27,23 @@ PyAPI_DATA(PyTypeObject) PyListRevIter_Type;
5027

5128
PyAPI_FUNC(PyObject *) PyList_New(Py_ssize_t size);
5229
PyAPI_FUNC(Py_ssize_t) PyList_Size(PyObject *);
30+
5331
PyAPI_FUNC(PyObject *) PyList_GetItem(PyObject *, Py_ssize_t);
5432
PyAPI_FUNC(int) PyList_SetItem(PyObject *, Py_ssize_t, PyObject *);
5533
PyAPI_FUNC(int) PyList_Insert(PyObject *, Py_ssize_t, PyObject *);
5634
PyAPI_FUNC(int) PyList_Append(PyObject *, PyObject *);
35+
5736
PyAPI_FUNC(PyObject *) PyList_GetSlice(PyObject *, Py_ssize_t, Py_ssize_t);
5837
PyAPI_FUNC(int) PyList_SetSlice(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
38+
5939
PyAPI_FUNC(int) PyList_Sort(PyObject *);
6040
PyAPI_FUNC(int) PyList_Reverse(PyObject *);
6141
PyAPI_FUNC(PyObject *) PyList_AsTuple(PyObject *);
62-
#ifndef Py_LIMITED_API
63-
PyAPI_FUNC(PyObject *) _PyList_Extend(PyListObject *, PyObject *);
64-
65-
PyAPI_FUNC(int) PyList_ClearFreeList(void);
66-
PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out);
67-
#endif
6842

69-
/* Macro, trading safety for speed */
7043
#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) (assert(PyList_Check(op)),Py_SIZE(op))
74-
#define _PyList_ITEMS(op) (((PyListObject *)(op))->ob_item)
44+
# define Py_CPYTHON_LISTOBJECT_H
45+
# include "cpython/listobject.h"
46+
# undef Py_CPYTHON_LISTOBJECT_H
7547
#endif
7648

7749
#ifdef __cplusplus

Makefile.pre.in

+1
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,7 @@ PYTHON_HEADERS= \
10631063
$(srcdir)/Include/cpython/import.h \
10641064
$(srcdir)/Include/cpython/initconfig.h \
10651065
$(srcdir)/Include/cpython/interpreteridobject.h \
1066+
$(srcdir)/Include/cpython/listobject.h \
10661067
$(srcdir)/Include/cpython/object.h \
10671068
$(srcdir)/Include/cpython/objimpl.h \
10681069
$(srcdir)/Include/cpython/pyerrors.h \

PCbuild/pythoncore.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
<ClInclude Include="..\Include\cpython\fileobject.h" />
133133
<ClInclude Include="..\Include\cpython\import.h" />
134134
<ClInclude Include="..\Include\cpython\initconfig.h" />
135+
<ClInclude Include="..\Include\cpython\listobject.h" />
135136
<ClInclude Include="..\Include\cpython\object.h" />
136137
<ClInclude Include="..\Include\cpython\objimpl.h" />
137138
<ClInclude Include="..\Include\cpython\pyerrors.h" />

PCbuild/pythoncore.vcxproj.filters

+3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@
9999
<ClInclude Include="..\Include\cpython\initconfig.h">
100100
<Filter>Include</Filter>
101101
</ClInclude>
102+
<ClInclude Include="..\Include\cpython\listobject.h">
103+
<Filter>Include</Filter>
104+
</ClInclude>
102105
<ClInclude Include="..\Include\cpython\object.h">
103106
<Filter>Include</Filter>
104107
</ClInclude>

0 commit comments

Comments
 (0)