Skip to content

Commit bcda8f1

Browse files
authored
bpo-35081: Add Include/internal/pycore_object.h (GH-10640)
Move _PyObject_GC_TRACK() and _PyObject_GC_UNTRACK() from Include/objimpl.h to Include/internal/pycore_object.h.
1 parent aac1f81 commit bcda8f1

34 files changed

+93
-50
lines changed

Include/internal/pycore_object.h

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#ifndef Py_INTERNAL_OBJECT_H
2+
#define Py_INTERNAL_OBJECT_H
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
#if !defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_BUILTIN)
8+
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN defined"
9+
#endif
10+
11+
/* Tell the GC to track this object.
12+
*
13+
* NB: While the object is tracked by the collector, it must be safe to call the
14+
* ob_traverse method.
15+
*
16+
* Internal note: _PyRuntime.gc.generation0->_gc_prev doesn't have any bit flags
17+
* because it's not object header. So we don't use _PyGCHead_PREV() and
18+
* _PyGCHead_SET_PREV() for it to avoid unnecessary bitwise operations.
19+
*
20+
* The PyObject_GC_Track() function is the public version of this macro.
21+
*/
22+
#define _PyObject_GC_TRACK(o) do { \
23+
PyGC_Head *g = _Py_AS_GC(o); \
24+
if (g->_gc_next != 0) { \
25+
Py_FatalError("GC object already tracked"); \
26+
} \
27+
assert((g->_gc_prev & _PyGC_PREV_MASK_COLLECTING) == 0); \
28+
PyGC_Head *last = (PyGC_Head*)(_PyRuntime.gc.generation0->_gc_prev); \
29+
_PyGCHead_SET_NEXT(last, g); \
30+
_PyGCHead_SET_PREV(g, last); \
31+
_PyGCHead_SET_NEXT(g, _PyRuntime.gc.generation0); \
32+
_PyRuntime.gc.generation0->_gc_prev = (uintptr_t)g; \
33+
} while (0);
34+
35+
/* Tell the GC to stop tracking this object.
36+
*
37+
* Internal note: This may be called while GC. So _PyGC_PREV_MASK_COLLECTING must
38+
* be cleared. But _PyGC_PREV_MASK_FINALIZED bit is kept.
39+
*
40+
* The PyObject_GC_UnTrack() function is the public version of this macro.
41+
*/
42+
#define _PyObject_GC_UNTRACK(o) do { \
43+
PyGC_Head *g = _Py_AS_GC(o); \
44+
PyGC_Head *prev = _PyGCHead_PREV(g); \
45+
PyGC_Head *next = _PyGCHead_NEXT(g); \
46+
assert(next != NULL); \
47+
_PyGCHead_SET_NEXT(prev, next); \
48+
_PyGCHead_SET_PREV(next, prev); \
49+
g->_gc_next = 0; \
50+
g->_gc_prev &= _PyGC_PREV_MASK_FINALIZED; \
51+
} while (0);
52+
53+
#ifdef __cplusplus
54+
}
55+
#endif
56+
#endif /* !Py_INTERNAL_OBJECT_H */

Include/objimpl.h

-45
Original file line numberDiff line numberDiff line change
@@ -323,51 +323,6 @@ typedef struct {
323323
_PyGCHead_SET_FINALIZED(_Py_AS_GC(o))
324324
#endif /* !defined(Py_LIMITED_API) */
325325

326-
327-
#if defined(Py_BUILD_CORE) || defined(Py_BUILD_CORE_BUILTIN)
328-
/* Tell the GC to track this object.
329-
*
330-
* NB: While the object is tracked by the collector, it must be safe to call the
331-
* ob_traverse method.
332-
*
333-
* Internal note: _PyRuntime.gc.generation0->_gc_prev doesn't have any bit flags
334-
* because it's not object header. So we don't use _PyGCHead_PREV() and
335-
* _PyGCHead_SET_PREV() for it to avoid unnecessary bitwise operations.
336-
*
337-
* The PyObject_GC_Track() function is the public version of this macro.
338-
*/
339-
#define _PyObject_GC_TRACK(o) do { \
340-
PyGC_Head *g = _Py_AS_GC(o); \
341-
if (g->_gc_next != 0) { \
342-
Py_FatalError("GC object already tracked"); \
343-
} \
344-
assert((g->_gc_prev & _PyGC_PREV_MASK_COLLECTING) == 0); \
345-
PyGC_Head *last = (PyGC_Head*)(_PyRuntime.gc.generation0->_gc_prev); \
346-
_PyGCHead_SET_NEXT(last, g); \
347-
_PyGCHead_SET_PREV(g, last); \
348-
_PyGCHead_SET_NEXT(g, _PyRuntime.gc.generation0); \
349-
_PyRuntime.gc.generation0->_gc_prev = (uintptr_t)g; \
350-
} while (0);
351-
352-
/* Tell the GC to stop tracking this object.
353-
*
354-
* Internal note: This may be called while GC. So _PyGC_PREV_MASK_COLLECTING must
355-
* be cleared. But _PyGC_PREV_MASK_FINALIZED bit is kept.
356-
*
357-
* The PyObject_GC_UnTrack() function is the public version of this macro.
358-
*/
359-
#define _PyObject_GC_UNTRACK(o) do { \
360-
PyGC_Head *g = _Py_AS_GC(o); \
361-
PyGC_Head *prev = _PyGCHead_PREV(g); \
362-
PyGC_Head *next = _PyGCHead_NEXT(g); \
363-
assert(next != NULL); \
364-
_PyGCHead_SET_NEXT(prev, next); \
365-
_PyGCHead_SET_PREV(next, prev); \
366-
g->_gc_next = 0; \
367-
g->_gc_prev &= _PyGC_PREV_MASK_FINALIZED; \
368-
} while (0);
369-
#endif /* defined(Py_BUILD_CORE) || defined(Py_BUILD_CORE_BUILTIN) */
370-
371326
#ifndef Py_LIMITED_API
372327
PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t size);
373328
PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size);

Modules/_io/bufferedio.c

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#define PY_SSIZE_T_CLEAN
1111
#include "Python.h"
12+
#include "pycore_object.h"
1213
#include "pycore_pystate.h"
1314
#include "structmember.h"
1415
#include "pythread.h"

Modules/_io/bytesio.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "Python.h"
2+
#include "pycore_object.h"
23
#include "structmember.h" /* for offsetof() */
34
#include "_iomodule.h"
45

Modules/_io/fileio.c

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#define PY_SSIZE_T_CLEAN
44
#include "Python.h"
5+
#include "pycore_object.h"
56
#include "structmember.h"
67
#ifdef HAVE_SYS_TYPES_H
78
#include <sys/types.h>

Modules/_io/iobase.c

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#define PY_SSIZE_T_CLEAN
1212
#include "Python.h"
13+
#include "pycore_object.h"
1314
#include "structmember.h"
1415
#include "_iomodule.h"
1516

Modules/_io/stringio.c

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "Python.h"
33
#include "structmember.h"
44
#include "pycore_accu.h"
5+
#include "pycore_object.h"
56
#include "_iomodule.h"
67

78
/* Implementation note: the buffer is always at least one character longer

Modules/_io/textio.c

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#define PY_SSIZE_T_CLEAN
1010
#include "Python.h"
11+
#include "pycore_object.h"
1112
#include "structmember.h"
1213
#include "_iomodule.h"
1314

Modules/_io/winconsoleio.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#define PY_SSIZE_T_CLEAN
1010
#include "Python.h"
11+
#include "pycore_object.h"
1112

1213
#ifdef MS_WINDOWS
1314

@@ -556,7 +557,7 @@ read_console_w(HANDLE handle, DWORD maxlen, DWORD *readlen) {
556557
Py_BEGIN_ALLOW_THREADS
557558
DWORD off = 0;
558559
while (off < maxlen) {
559-
DWORD n = (DWORD)-1;
560+
DWORD n = (DWORD)-1;
560561
DWORD len = min(maxlen - off, BUFSIZ);
561562
SetLastError(0);
562563
BOOL res = ReadConsoleW(handle, &buf[off], len, &n, NULL);

Modules/gcmodule.c

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "Python.h"
2727
#include "pycore_context.h"
28+
#include "pycore_object.h"
2829
#include "pycore_pymem.h"
2930
#include "pycore_pystate.h"
3031
#include "frameobject.h" /* for PyFrame_ClearFreeList */

Objects/bytearrayobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#define PY_SSIZE_T_CLEAN
44
#include "Python.h"
5+
#include "pycore_object.h"
56
#include "pycore_pymem.h"
67
#include "pycore_pystate.h"
78
#include "structmember.h"

Objects/bytesobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define PY_SSIZE_T_CLEAN
44

55
#include "Python.h"
6+
#include "pycore_object.h"
67
#include "pycore_pymem.h"
78
#include "pycore_pystate.h"
89

Objects/call.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "Python.h"
2+
#include "pycore_object.h"
23
#include "pycore_pystate.h"
34
#include "frameobject.h"
45

Objects/cellobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Cell object implementation */
22

33
#include "Python.h"
4+
#include "pycore_object.h"
45
#include "pycore_pymem.h"
56
#include "pycore_pystate.h"
67

Objects/classobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Class object implementation (dead now except for methods) */
22

33
#include "Python.h"
4+
#include "pycore_object.h"
45
#include "pycore_pymem.h"
56
#include "pycore_pystate.h"
67
#include "structmember.h"

Objects/descrobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Descriptors -- a new, flexible way to describe attributes */
22

33
#include "Python.h"
4+
#include "pycore_object.h"
45
#include "pycore_pystate.h"
56
#include "structmember.h" /* Why is this not included in Python.h? */
67

Objects/dictobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ converting the dict to the combined table.
111111
#define PyDict_MINSIZE 8
112112

113113
#include "Python.h"
114+
#include "pycore_object.h"
114115
#include "pycore_pystate.h"
115116
#include "dict-common.h"
116117
#include "stringlib/eq.h" /* to get unicode_eq() */

Objects/exceptions.c

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#define PY_SSIZE_T_CLEAN
88
#include <Python.h>
9+
#include "pycore_object.h"
910
#include "pycore_pymem.h"
1011
#include "pycore_pystate.h"
1112
#include "structmember.h"

Objects/frameobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Frame object implementation */
22

33
#include "Python.h"
4+
#include "pycore_object.h"
45
#include "pycore_pystate.h"
56

67
#include "code.h"

Objects/funcobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* Function object implementation */
33

44
#include "Python.h"
5+
#include "pycore_object.h"
56
#include "pycore_pymem.h"
67
#include "pycore_pystate.h"
78
#include "code.h"

Objects/genobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Generator object implementation */
22

33
#include "Python.h"
4+
#include "pycore_object.h"
45
#include "pycore_pystate.h"
56
#include "frameobject.h"
67
#include "structmember.h"

Objects/iterobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Iterator objects */
22

33
#include "Python.h"
4+
#include "pycore_object.h"
45
#include "pycore_pymem.h"
56
#include "pycore_pystate.h"
67

Objects/listobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* List object implementation */
22

33
#include "Python.h"
4+
#include "pycore_object.h"
45
#include "pycore_pystate.h"
56
#include "pycore_accu.h"
67

Objects/memoryobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Memoryview object implementation */
22

33
#include "Python.h"
4+
#include "pycore_object.h"
45
#include "pycore_pymem.h"
56
#include "pycore_pystate.h"
67
#include "pystrhex.h"

Objects/methodobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* Method object implementation */
33

44
#include "Python.h"
5+
#include "pycore_object.h"
56
#include "pycore_pymem.h"
67
#include "pycore_pystate.h"
78
#include "structmember.h"

Objects/odictobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ Potential Optimizations
465465
*/
466466

467467
#include "Python.h"
468+
#include "pycore_object.h"
468469
#include "pycore_pystate.h"
469470
#include "structmember.h"
470471
#include "dict-common.h"

Objects/setobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
*/
3333

3434
#include "Python.h"
35+
#include "pycore_object.h"
3536
#include "pycore_pystate.h"
3637
#include "structmember.h"
3738

Objects/sliceobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ this type and there is exactly one in existence.
1414
*/
1515

1616
#include "Python.h"
17+
#include "pycore_object.h"
1718
#include "pycore_pymem.h"
1819
#include "pycore_pystate.h"
1920
#include "structmember.h"

Objects/tupleobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* Tuple object implementation */
33

44
#include "Python.h"
5+
#include "pycore_object.h"
56
#include "pycore_pystate.h"
67
#include "pycore_accu.h"
78

Objects/typeobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Type object implementation */
22

33
#include "Python.h"
4+
#include "pycore_object.h"
45
#include "pycore_pystate.h"
56
#include "frameobject.h"
67
#include "structmember.h"

Objects/unicodeobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4141
#define PY_SSIZE_T_CLEAN
4242
#include "Python.h"
4343
#include "pycore_fileutils.h"
44+
#include "pycore_object.h"
4445
#include "pycore_pystate.h"
4546
#include "ucnhash.h"
4647
#include "bytes_methods.h"

Python/ceval.c

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define PY_LOCAL_AGGRESSIVE
1111

1212
#include "Python.h"
13+
#include "pycore_object.h"
1314
#include "pycore_pystate.h"
1415

1516
#include "code.h"

Python/context.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include "Python.h"
22

3-
#include "structmember.h"
4-
#include "pycore_pystate.h"
53
#include "pycore_context.h"
64
#include "pycore_hamt.h"
5+
#include "pycore_object.h"
6+
#include "pycore_pystate.h"
7+
#include "structmember.h"
78

89

910
#define CONTEXT_FREELIST_MAXLEN 255

Python/hamt.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#include "Python.h"
22

3-
#include "structmember.h"
4-
#include "pycore_pystate.h"
53
#include "pycore_hamt.h"
4+
#include "pycore_object.h"
5+
#include "pycore_pystate.h"
6+
#include "structmember.h"
67

78
/*
89
This file provides an implemention of an immutable mapping using the

0 commit comments

Comments
 (0)