Skip to content

Commit a72d15c

Browse files
authored
consistently use Py_TYPE, Py_REFCNT, and correct initializer macros (#3563)
This no-op change makes 2.7 more consistent with 3.x to ease comparison and backports.
1 parent 860839c commit a72d15c

22 files changed

+117
-138
lines changed

Include/frameobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ typedef struct _frame {
5454

5555
PyAPI_DATA(PyTypeObject) PyFrame_Type;
5656

57-
#define PyFrame_Check(op) ((op)->ob_type == &PyFrame_Type)
57+
#define PyFrame_Check(op) (Py_TYPE(op) == &PyFrame_Type)
5858
#define PyFrame_IsRestricted(f) \
5959
((f)->f_builtins != (f)->f_tstate->interp->builtins)
6060

Include/intobject.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ typedef struct {
2828
PyAPI_DATA(PyTypeObject) PyInt_Type;
2929

3030
#define PyInt_Check(op) \
31-
PyType_FastSubclass((op)->ob_type, Py_TPFLAGS_INT_SUBCLASS)
32-
#define PyInt_CheckExact(op) ((op)->ob_type == &PyInt_Type)
31+
PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_INT_SUBCLASS)
32+
#define PyInt_CheckExact(op) (Py_TYPE(op) == &PyInt_Type)
3333

3434
PyAPI_FUNC(PyObject *) PyInt_FromString(char*, char**, int);
3535
#ifdef Py_USING_UNICODE

Modules/_ctypes/ctypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ typedef struct {
108108
ffi_type *atypes[1];
109109
} CThunkObject;
110110
extern PyTypeObject PyCThunk_Type;
111-
#define CThunk_CheckExact(v) ((v)->ob_type == &PyCThunk_Type)
111+
#define CThunk_CheckExact(v) (Py_TYPE(v) == &PyCThunk_Type)
112112

113113
typedef struct {
114114
/* First part identical to tagCDataObject */

Modules/_elementtree.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,8 +1661,8 @@ static PyMappingMethods element_as_mapping = {
16611661
};
16621662

16631663
statichere PyTypeObject Element_Type = {
1664-
PyObject_HEAD_INIT(NULL)
1665-
0, "Element", sizeof(ElementObject), 0,
1664+
PyVarObject_HEAD_INIT(NULL, 0)
1665+
"Element", sizeof(ElementObject), 0,
16661666
/* methods */
16671667
(destructor)element_dealloc, /* tp_dealloc */
16681668
0, /* tp_print */
@@ -2031,8 +2031,8 @@ treebuilder_getattr(TreeBuilderObject* self, char* name)
20312031
}
20322032

20332033
statichere PyTypeObject TreeBuilder_Type = {
2034-
PyObject_HEAD_INIT(NULL)
2035-
0, "TreeBuilder", sizeof(TreeBuilderObject), 0,
2034+
PyVarObject_HEAD_INIT(NULL, 0)
2035+
"TreeBuilder", sizeof(TreeBuilderObject), 0,
20362036
/* methods */
20372037
(destructor)treebuilder_dealloc, /* tp_dealloc */
20382038
0, /* tp_print */
@@ -2897,8 +2897,8 @@ xmlparser_getattr(XMLParserObject* self, char* name)
28972897
}
28982898

28992899
statichere PyTypeObject XMLParser_Type = {
2900-
PyObject_HEAD_INIT(NULL)
2901-
0, "XMLParser", sizeof(XMLParserObject), 0,
2900+
PyVarObject_HEAD_INIT(NULL, 0)
2901+
"XMLParser", sizeof(XMLParserObject), 0,
29022902
/* methods */
29032903
(destructor)xmlparser_dealloc, /* tp_dealloc */
29042904
0, /* tp_print */

Modules/_json.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,8 +1767,7 @@ PyDoc_STRVAR(scanner_doc, "JSON scanner object");
17671767

17681768
static
17691769
PyTypeObject PyScannerType = {
1770-
PyObject_HEAD_INIT(NULL)
1771-
0, /* tp_internal */
1770+
PyVarObject_HEAD_INIT(NULL, 0)
17721771
"_json.Scanner", /* tp_name */
17731772
sizeof(PyScannerObject), /* tp_basicsize */
17741773
0, /* tp_itemsize */
@@ -2344,8 +2343,7 @@ PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable")
23442343

23452344
static
23462345
PyTypeObject PyEncoderType = {
2347-
PyObject_HEAD_INIT(NULL)
2348-
0, /* tp_internal */
2346+
PyVarObject_HEAD_INIT(NULL, 0)
23492347
"_json.Encoder", /* tp_name */
23502348
sizeof(PyEncoderObject), /* tp_basicsize */
23512349
0, /* tp_itemsize */

Modules/_lsprof.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -817,8 +817,7 @@ Profiler(custom_timer=None, time_unit=None, subcalls=True, builtins=True)\n\
817817
");
818818

819819
statichere PyTypeObject PyProfiler_Type = {
820-
PyObject_HEAD_INIT(NULL)
821-
0, /* ob_size */
820+
PyVarObject_HEAD_INIT(NULL, 0)
822821
"_lsprof.Profiler", /* tp_name */
823822
sizeof(ProfilerObject), /* tp_basicsize */
824823
0, /* tp_itemsize */

Modules/_sqlite/connection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ void pysqlite_connection_dealloc(pysqlite_Connection* self)
293293
Py_XDECREF(self->statements);
294294
Py_XDECREF(self->cursors);
295295

296-
self->ob_type->tp_free((PyObject*)self);
296+
Py_TYPE(self)->tp_free((PyObject*)self);
297297
}
298298

299299
/*

Modules/_sqlite/cursor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ static void pysqlite_cursor_dealloc(pysqlite_Cursor* self)
135135
PyObject_ClearWeakRefs((PyObject*)self);
136136
}
137137

138-
self->ob_type->tp_free((PyObject*)self);
138+
Py_TYPE(self)->tp_free((PyObject*)self);
139139
}
140140

141141
PyObject* _pysqlite_get_converter(PyObject* key)

Modules/_sre.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,8 +2712,8 @@ static PyMemberDef pattern_members[] = {
27122712
};
27132713

27142714
statichere PyTypeObject Pattern_Type = {
2715-
PyObject_HEAD_INIT(NULL)
2716-
0, "_" SRE_MODULE ".SRE_Pattern",
2715+
PyVarObject_HEAD_INIT(NULL, 0)
2716+
"_" SRE_MODULE ".SRE_Pattern",
27172717
sizeof(PatternObject), sizeof(SRE_CODE),
27182718
(destructor)pattern_dealloc, /*tp_dealloc*/
27192719
0, /* tp_print */
@@ -3952,8 +3952,8 @@ static PyMemberDef scanner_members[] = {
39523952
};
39533953

39543954
statichere PyTypeObject Scanner_Type = {
3955-
PyObject_HEAD_INIT(NULL)
3956-
0, "_" SRE_MODULE ".SRE_Scanner",
3955+
PyVarObject_HEAD_INIT(NULL, 0)
3956+
"_" SRE_MODULE ".SRE_Scanner",
39573957
sizeof(ScannerObject), 0,
39583958
(destructor)scanner_dealloc, /*tp_dealloc*/
39593959
0, /* tp_print */

Modules/_testcapimodule.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ test_dict_iteration(PyObject* self)
187187
* PyType_Ready if it hasn't already been called
188188
*/
189189
static PyTypeObject _HashInheritanceTester_Type = {
190-
PyObject_HEAD_INIT(NULL)
191-
0, /* Number of items for varobject */
190+
PyVarObject_HEAD_INIT(NULL, 0)
192191
"hashinheritancetester", /* Name of this type */
193192
sizeof(PyObject), /* Basic object size */
194193
0, /* Item size for varobject */
@@ -315,8 +314,7 @@ static PyBufferProcs memoryviewtester_as_buffer = {
315314
};
316315

317316
static PyTypeObject _MemoryViewTester_Type = {
318-
PyObject_HEAD_INIT(NULL)
319-
0, /* Number of items for varobject */
317+
PyVarObject_HEAD_INIT(NULL, 0)
320318
"memoryviewtester", /* Name of this type */
321319
sizeof(PyObject), /* Basic object size */
322320
0, /* Item size for varobject */

Modules/arraymodule.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,9 +1292,9 @@ array_tofile(arrayobject *self, PyObject *f)
12921292
PyErr_SetString(PyExc_TypeError, "arg must be open file");
12931293
return NULL;
12941294
}
1295-
if (self->ob_size > 0) {
1295+
if (Py_SIZE(self) > 0) {
12961296
if (fwrite(self->ob_item, self->ob_descr->itemsize,
1297-
self->ob_size, fp) != (size_t)self->ob_size) {
1297+
Py_SIZE(self), fp) != (size_t)Py_SIZE(self)) {
12981298
PyErr_SetFromErrno(PyExc_IOError);
12991299
clearerr(fp);
13001300
return NULL;
@@ -1348,7 +1348,7 @@ array_fromlist(arrayobject *self, PyObject *list)
13481348
if ((*self->ob_descr->setitem)(self,
13491349
Py_SIZE(self) - n + i, v) != 0) {
13501350
Py_SIZE(self) -= n;
1351-
if (itemsize && (self->ob_size > PY_SSIZE_T_MAX / itemsize)) {
1351+
if (itemsize && (Py_SIZE(self) > PY_SSIZE_T_MAX / itemsize)) {
13521352
return PyErr_NoMemory();
13531353
}
13541354
PyMem_RESIZE(item, char,
@@ -1444,7 +1444,7 @@ values,as if it had been read from a file using the fromfile() method).");
14441444
static PyObject *
14451445
array_tostring(arrayobject *self, PyObject *unused)
14461446
{
1447-
if (self->ob_size <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
1447+
if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
14481448
return PyString_FromStringAndSize(self->ob_item,
14491449
Py_SIZE(self) * self->ob_descr->itemsize);
14501450
} else {
@@ -2289,8 +2289,8 @@ initarray(void)
22892289
{
22902290
PyObject *m;
22912291

2292-
Arraytype.ob_type = &PyType_Type;
2293-
PyArrayIter_Type.ob_type = &PyType_Type;
2292+
Py_TYPE(&Arraytype) = &PyType_Type;
2293+
Py_TYPE(&PyArrayIter_Type) = &PyType_Type;
22942294
m = Py_InitModule3("array", a_methods, module_doc);
22952295
if (m == NULL)
22962296
return;

Modules/datetimemodule.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3039,8 +3039,7 @@ static char tzinfo_doc[] =
30393039
PyDoc_STR("Abstract base class for time zone info objects.");
30403040

30413041
statichere PyTypeObject PyDateTime_TZInfoType = {
3042-
PyObject_HEAD_INIT(NULL)
3043-
0, /* ob_size */
3042+
PyVarObject_HEAD_INIT(NULL, 0)
30443043
"datetime.tzinfo", /* tp_name */
30453044
sizeof(PyDateTime_TZInfo), /* tp_basicsize */
30463045
0, /* tp_itemsize */
@@ -3564,8 +3563,7 @@ static PyNumberMethods time_as_number = {
35643563
};
35653564

35663565
statichere PyTypeObject PyDateTime_TimeType = {
3567-
PyObject_HEAD_INIT(NULL)
3568-
0, /* ob_size */
3566+
PyVarObject_HEAD_INIT(NULL, 0)
35693567
"datetime.time", /* tp_name */
35703568
sizeof(PyDateTime_Time), /* tp_basicsize */
35713569
0, /* tp_itemsize */
@@ -4692,8 +4690,7 @@ static PyNumberMethods datetime_as_number = {
46924690
};
46934691

46944692
statichere PyTypeObject PyDateTime_DateTimeType = {
4695-
PyObject_HEAD_INIT(NULL)
4696-
0, /* ob_size */
4693+
PyVarObject_HEAD_INIT(NULL, 0)
46974694
"datetime.datetime", /* tp_name */
46984695
sizeof(PyDateTime_DateTime), /* tp_basicsize */
46994696
0, /* tp_itemsize */

Objects/bufferobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ get_buf(PyBufferObject *self, void **ptr, Py_ssize_t *size,
3434
else {
3535
Py_ssize_t count, offset;
3636
readbufferproc proc = 0;
37-
PyBufferProcs *bp = self->b_base->ob_type->tp_as_buffer;
37+
PyBufferProcs *bp = Py_TYPE(self->b_base)->tp_as_buffer;
3838
if ((*bp->bf_getsegcount)(self->b_base, NULL) != 1) {
3939
PyErr_SetString(PyExc_TypeError,
4040
"single-segment buffer object expected");
@@ -47,7 +47,7 @@ get_buf(PyBufferObject *self, void **ptr, Py_ssize_t *size,
4747
(buffer_type == ANY_BUFFER))
4848
proc = (readbufferproc)bp->bf_getwritebuffer;
4949
else if (buffer_type == CHAR_BUFFER) {
50-
if (!PyType_HasFeature(self->ob_type,
50+
if (!PyType_HasFeature(Py_TYPE(self),
5151
Py_TPFLAGS_HAVE_GETCHARBUFFER)) {
5252
PyErr_SetString(PyExc_TypeError,
5353
"Py_TPFLAGS_HAVE_GETCHARBUFFER needed");

0 commit comments

Comments
 (0)