Skip to content

Commit 01d3598

Browse files
committed
Remove trailing commas in _PyObject_HEAD_INIT and _PyVarObject_HEAD_INIT
Removing the trailing comma allows the macros to be used in more places, which means fewer `#ifdef Py_NOGIL` checks. Note that the public API macros `PyObject_HEAD_INIT` and `PyVarObject_HEAD_INIT` are not changed.
1 parent 19b7ead commit 01d3598

File tree

9 files changed

+21
-44
lines changed

9 files changed

+21
-44
lines changed

Include/internal/pycore_long.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ _PyLong_FlipSign(PyLongObject *op) {
363363

364364
#define _PyLong_DIGIT_INIT(val) \
365365
{ \
366-
.ob_base = _PyObject_HEAD_INIT(&PyLong_Type) \
366+
.ob_base = _PyObject_HEAD_INIT(&PyLong_Type), \
367367
.long_value = { \
368368
.lv_tag = TAG_FROM_SIGN_AND_SIZE( \
369369
(val) == 0 ? 0 : ((val) < 0 ? -1 : 1), \

Include/internal/pycore_object.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ PyAPI_FUNC(int) _PyObject_IsFreed(PyObject *);
5858
{ \
5959
.ob_refcnt = _Py_IMMORTAL_REFCNT, \
6060
.ob_type = (type) \
61-
},
61+
}
6262
#define _PyVarObject_HEAD_INIT(type, size) \
6363
{ \
64-
.ob_base = _PyObject_HEAD_INIT(type) \
64+
.ob_base = _PyObject_HEAD_INIT(type), \
6565
.ob_size = size \
66-
},
66+
}
6767

6868
extern void _Py_NO_RETURN _Py_FatalRefcountErrorFunc(
6969
const char *func,

Include/internal/pycore_runtime_init.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ extern PyTypeObject _PyExc_MemoryError;
129129
.latin1 = _Py_str_latin1_INIT, \
130130
}, \
131131
.tuple_empty = { \
132-
.ob_base = _PyVarObject_HEAD_INIT(&PyTuple_Type, 0) \
132+
.ob_base = _PyVarObject_HEAD_INIT(&PyTuple_Type, 0), \
133133
}, \
134134
.hamt_bitmap_node_empty = { \
135-
.ob_base = _PyVarObject_HEAD_INIT(&_PyHamt_BitmapNode_Type, 0) \
135+
.ob_base = _PyVarObject_HEAD_INIT(&_PyHamt_BitmapNode_Type, 0), \
136136
}, \
137137
.context_token_missing = { \
138-
.ob_base = _PyObject_HEAD_INIT(&_PyContextTokenMissing_Type) \
138+
.ob_base = _PyObject_HEAD_INIT(&_PyContextTokenMissing_Type), \
139139
}, \
140140
}, \
141141
}, \
@@ -172,11 +172,11 @@ extern PyTypeObject _PyExc_MemoryError;
172172
.singletons = { \
173173
._not_used = 1, \
174174
.hamt_empty = { \
175-
.ob_base = _PyObject_HEAD_INIT(&_PyHamt_Type) \
175+
.ob_base = _PyObject_HEAD_INIT(&_PyHamt_Type), \
176176
.h_root = (PyHamtNode*)&_Py_SINGLETON(hamt_bitmap_node_empty), \
177177
}, \
178178
.last_resort_memory_error = { \
179-
_PyObject_HEAD_INIT(&_PyExc_MemoryError) \
179+
_PyObject_HEAD_INIT(&_PyExc_MemoryError), \
180180
}, \
181181
}, \
182182
}, \
@@ -205,7 +205,7 @@ extern PyTypeObject _PyExc_MemoryError;
205205

206206
#define _PyBytes_SIMPLE_INIT(CH, LEN) \
207207
{ \
208-
_PyVarObject_HEAD_INIT(&PyBytes_Type, (LEN)) \
208+
_PyVarObject_HEAD_INIT(&PyBytes_Type, (LEN)), \
209209
.ob_shash = -1, \
210210
.ob_sval = { (CH) }, \
211211
}
@@ -216,7 +216,7 @@ extern PyTypeObject _PyExc_MemoryError;
216216

217217
#define _PyUnicode_ASCII_BASE_INIT(LITERAL, ASCII) \
218218
{ \
219-
.ob_base = _PyObject_HEAD_INIT(&PyUnicode_Type) \
219+
.ob_base = _PyObject_HEAD_INIT(&PyUnicode_Type), \
220220
.length = sizeof(LITERAL) - 1, \
221221
.hash = -1, \
222222
.state = { \

Objects/object.c

+2-8
Original file line numberDiff line numberDiff line change
@@ -1925,10 +1925,7 @@ PyTypeObject _PyNone_Type = {
19251925
none_new, /*tp_new */
19261926
};
19271927

1928-
PyObject _Py_NoneStruct = {
1929-
{ _Py_IMMORTAL_REFCNT },
1930-
&_PyNone_Type
1931-
};
1928+
PyObject _Py_NoneStruct = _PyObject_HEAD_INIT(&_PyNone_Type);
19321929

19331930
/* NotImplemented is an object that can be used to signal that an
19341931
operation is not implemented for the given type combination. */
@@ -2027,10 +2024,7 @@ PyTypeObject _PyNotImplemented_Type = {
20272024
notimplemented_new, /*tp_new */
20282025
};
20292026

2030-
PyObject _Py_NotImplementedStruct = {
2031-
{ _Py_IMMORTAL_REFCNT },
2032-
&_PyNotImplemented_Type
2033-
};
2027+
PyObject _Py_NotImplementedStruct = _PyObject_HEAD_INIT(&_PyNotImplemented_Type);
20342028

20352029

20362030
PyStatus

Objects/setobject.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -2547,7 +2547,4 @@ static PyTypeObject _PySetDummy_Type = {
25472547
Py_TPFLAGS_DEFAULT, /*tp_flags */
25482548
};
25492549

2550-
static PyObject _dummy_struct = {
2551-
{ _Py_IMMORTAL_REFCNT },
2552-
&_PySetDummy_Type
2553-
};
2550+
static PyObject _dummy_struct = _PyObject_HEAD_INIT(&_PySetDummy_Type);

Objects/sliceobject.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,7 @@ PyTypeObject PyEllipsis_Type = {
9797
ellipsis_new, /* tp_new */
9898
};
9999

100-
PyObject _Py_EllipsisObject = {
101-
{ _Py_IMMORTAL_REFCNT },
102-
&PyEllipsis_Type
103-
};
100+
PyObject _Py_EllipsisObject = _PyObject_HEAD_INIT(&PyEllipsis_Type);
104101

105102

106103
/* Slice object implementation */

Python/instrumentation.c

+2-10
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,9 @@
1919
/* Uncomment this to dump debugging output when assertions fail */
2020
// #define INSTRUMENT_DEBUG 1
2121

22-
PyObject _PyInstrumentation_DISABLE =
23-
{
24-
.ob_refcnt = _Py_IMMORTAL_REFCNT,
25-
.ob_type = &PyBaseObject_Type
26-
};
22+
PyObject _PyInstrumentation_DISABLE = _PyObject_HEAD_INIT(&PyBaseObject_Type);
2723

28-
PyObject _PyInstrumentation_MISSING =
29-
{
30-
.ob_refcnt = _Py_IMMORTAL_REFCNT,
31-
.ob_type = &PyBaseObject_Type
32-
};
24+
PyObject _PyInstrumentation_MISSING = _PyObject_HEAD_INIT(&PyBaseObject_Type);
3325

3426
static const int8_t EVENT_FOR_OPCODE[256] = {
3527
[RETURN_CONST] = PY_MONITORING_EVENT_PY_RETURN,

Python/specialize.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2524,7 +2524,7 @@ static const PyBytesObject no_location = {
25242524
};
25252525

25262526
const struct _PyCode_DEF(8) _Py_InitCleanup = {
2527-
_PyVarObject_HEAD_INIT(&PyCode_Type, 4)
2527+
_PyVarObject_HEAD_INIT(&PyCode_Type, 4),
25282528
.co_consts = (PyObject *)&_Py_SINGLETON(tuple_empty),
25292529
.co_names = (PyObject *)&_Py_SINGLETON(tuple_empty),
25302530
.co_exceptiontable = (PyObject *)&_Py_SINGLETON(bytes_empty),

Tools/build/deepfreeze.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def __init__(self, file: TextIO) -> None:
115115
self.inits: list[str] = []
116116
self.identifiers, self.strings = self.get_identifiers_and_strings()
117117
self.write('#include "Python.h"')
118+
self.write('#include "internal/pycore_object.h"')
118119
self.write('#include "internal/pycore_gc.h"')
119120
self.write('#include "internal/pycore_code.h"')
120121
self.write('#include "internal/pycore_frame.h"')
@@ -154,14 +155,10 @@ def block(self, prefix: str, suffix: str = "") -> None:
154155
self.write("}" + suffix)
155156

156157
def object_head(self, typename: str) -> None:
157-
with self.block(".ob_base =", ","):
158-
self.write(f".ob_refcnt = _Py_IMMORTAL_REFCNT,")
159-
self.write(f".ob_type = &{typename},")
158+
self.write(f".ob_base = _PyObject_HEAD_INIT(&{typename}),")
160159

161160
def object_var_head(self, typename: str, size: int) -> None:
162-
with self.block(".ob_base =", ","):
163-
self.object_head(typename)
164-
self.write(f".ob_size = {size},")
161+
self.write(f".ob_base = _PyVarObject_HEAD_INIT(&{typename}, {size}),")
165162

166163
def field(self, obj: object, name: str) -> None:
167164
self.write(f".{name} = {getattr(obj, name)},")

0 commit comments

Comments
 (0)