Skip to content

Commit 04fc4f2

Browse files
authored
bpo-40989: PyObject_INIT() becomes an alias to PyObject_Init() (GH-20901)
The PyObject_INIT() and PyObject_INIT_VAR() macros become aliases to, respectively, PyObject_Init() and PyObject_InitVar() functions. Rename _PyObject_INIT() and _PyObject_INIT_VAR() static inline functions to, respectively, _PyObject_Init() and _PyObject_InitVar(), and move them to pycore_object.h. Remove their return value: their return type becomes void. The _datetime module is now built with the Py_BUILD_CORE_MODULE macro defined. Remove an outdated comment on _Py_tracemalloc_config.
1 parent 7ab92d5 commit 04fc4f2

File tree

17 files changed

+113
-117
lines changed

17 files changed

+113
-117
lines changed

Include/cpython/objimpl.h

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@
3737
PyObject *op;
3838
3939
op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
40-
if (op == NULL)
41-
return PyErr_NoMemory();
40+
if (op == NULL) {
41+
return PyErr_NoMemory();
42+
}
4243
4344
PyObject_Init(op, &YourTypeStruct);
4445
@@ -51,40 +52,6 @@
5152
the 1st step is performed automatically for you, so in a C++ class
5253
constructor you would start directly with PyObject_Init/InitVar. */
5354

54-
55-
/* Inline functions trading binary compatibility for speed:
56-
PyObject_INIT() is the fast version of PyObject_Init(), and
57-
PyObject_INIT_VAR() is the fast version of PyObject_InitVar().
58-
59-
These inline functions must not be called with op=NULL. */
60-
static inline PyObject*
61-
_PyObject_INIT(PyObject *op, PyTypeObject *typeobj)
62-
{
63-
assert(op != NULL);
64-
Py_SET_TYPE(op, typeobj);
65-
if (PyType_GetFlags(typeobj) & Py_TPFLAGS_HEAPTYPE) {
66-
Py_INCREF(typeobj);
67-
}
68-
_Py_NewReference(op);
69-
return op;
70-
}
71-
72-
#define PyObject_INIT(op, typeobj) \
73-
_PyObject_INIT(_PyObject_CAST(op), (typeobj))
74-
75-
static inline PyVarObject*
76-
_PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
77-
{
78-
assert(op != NULL);
79-
Py_SET_SIZE(op, size);
80-
PyObject_INIT((PyObject *)op, typeobj);
81-
return op;
82-
}
83-
84-
#define PyObject_INIT_VAR(op, typeobj, size) \
85-
_PyObject_INIT_VAR(_PyVarObject_CAST(op), (typeobj), (size))
86-
87-
8855
/* This function returns the number of allocated memory blocks, regardless of size */
8956
PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void);
9057

Include/internal/pycore_object.h

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,37 @@ extern "C" {
1515
PyAPI_FUNC(int) _PyType_CheckConsistency(PyTypeObject *type);
1616
PyAPI_FUNC(int) _PyDict_CheckConsistency(PyObject *mp, int check_content);
1717

18+
// Fast inlined version of PyType_HasFeature()
19+
static inline int
20+
_PyType_HasFeature(PyTypeObject *type, unsigned long feature) {
21+
return ((type->tp_flags & feature) != 0);
22+
}
23+
24+
/* Inline functions trading binary compatibility for speed:
25+
_PyObject_Init() is the fast version of PyObject_Init(), and
26+
_PyObject_InitVar() is the fast version of PyObject_InitVar().
27+
28+
These inline functions must not be called with op=NULL. */
29+
static inline void
30+
_PyObject_Init(PyObject *op, PyTypeObject *typeobj)
31+
{
32+
assert(op != NULL);
33+
Py_SET_TYPE(op, typeobj);
34+
if (_PyType_HasFeature(typeobj, Py_TPFLAGS_HEAPTYPE)) {
35+
Py_INCREF(typeobj);
36+
}
37+
_Py_NewReference(op);
38+
}
39+
40+
static inline void
41+
_PyObject_InitVar(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
42+
{
43+
assert(op != NULL);
44+
Py_SET_SIZE(op, size);
45+
_PyObject_Init((PyObject *)op, typeobj);
46+
}
47+
48+
1849
/* Tell the GC to track this object.
1950
*
2051
* NB: While the object is tracked by the collector, it must be safe to call the
@@ -96,12 +127,6 @@ _PyObject_GET_WEAKREFS_LISTPTR(PyObject *op)
96127
return (PyObject **)((char *)op + offset);
97128
}
98129

99-
// Fast inlined version of PyType_HasFeature()
100-
static inline int
101-
_PyType_HasFeature(PyTypeObject *type, unsigned long feature) {
102-
return ((type->tp_flags & feature) != 0);
103-
}
104-
105130
// Fast inlined version of PyObject_IS_GC()
106131
static inline int
107132
_PyObject_IS_GC(PyObject *obj)

Include/internal/pycore_pymem.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ PyAPI_FUNC(int) _PyMem_GetAllocatorName(
6969
PYMEM_ALLOCATOR_NOT_SET does nothing. */
7070
PyAPI_FUNC(int) _PyMem_SetupAllocators(PyMemAllocatorName allocator);
7171

72-
/* bpo-35053: Expose _Py_tracemalloc_config for _Py_NewReference()
73-
which access directly _Py_tracemalloc_config.tracing for best
74-
performances. */
7572
struct _PyTraceMalloc_Config {
7673
/* Module initialized?
7774
Variable protected by the GIL */

Include/objimpl.h

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,14 @@ PyAPI_FUNC(void) PyObject_Free(void *ptr);
118118
/* Functions */
119119
PyAPI_FUNC(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
120120
PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *,
121-
PyTypeObject *, Py_ssize_t);
121+
PyTypeObject *, Py_ssize_t);
122+
123+
#define PyObject_INIT(op, typeobj) \
124+
PyObject_Init(_PyObject_CAST(op), (typeobj))
125+
#define PyObject_INIT_VAR(op, typeobj, size) \
126+
PyObject_InitVar(_PyVarObject_CAST(op), (typeobj), (size))
127+
128+
122129
PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *);
123130
PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
124131

@@ -136,19 +143,6 @@ PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
136143
#define PyObject_NEW_VAR(type, typeobj, n) PyObject_NewVar(type, typeobj, n)
137144

138145

139-
#ifdef Py_LIMITED_API
140-
/* Define PyObject_INIT() and PyObject_INIT_VAR() as aliases to PyObject_Init()
141-
and PyObject_InitVar() in the limited C API for compatibility with the
142-
CPython C API. */
143-
# define PyObject_INIT(op, typeobj) \
144-
PyObject_Init(_PyObject_CAST(op), (typeobj))
145-
# define PyObject_INIT_VAR(op, typeobj, size) \
146-
PyObject_InitVar(_PyVarObject_CAST(op), (typeobj), (size))
147-
#else
148-
/* PyObject_INIT() and PyObject_INIT_VAR() are defined in cpython/objimpl.h */
149-
#endif
150-
151-
152146
/*
153147
* Garbage Collection Support
154148
* ==========================
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The :c:func:`PyObject_INIT` and :c:func:`PyObject_INIT_VAR` macros become
2+
aliases to, respectively, :c:func:`PyObject_Init` and
3+
:c:func:`PyObject_InitVar` functions.

Modules/_datetimemodule.c

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define _PY_DATETIME_IMPL
99

1010
#include "Python.h"
11+
#include "pycore_object.h" // _PyObject_Init()
1112
#include "datetime.h"
1213
#include "structmember.h" // PyMemberDef
1314

@@ -638,30 +639,24 @@ normalize_datetime(int *year, int *month, int *day,
638639
static PyObject *
639640
time_alloc(PyTypeObject *type, Py_ssize_t aware)
640641
{
641-
PyObject *self;
642-
643-
self = (PyObject *)
644-
PyObject_MALLOC(aware ?
645-
sizeof(PyDateTime_Time) :
646-
sizeof(_PyDateTime_BaseTime));
647-
if (self == NULL)
648-
return (PyObject *)PyErr_NoMemory();
649-
(void)PyObject_INIT(self, type);
642+
size_t size = aware ? sizeof(PyDateTime_Time) : sizeof(_PyDateTime_BaseTime);
643+
PyObject *self = (PyObject *)PyObject_Malloc(size);
644+
if (self == NULL) {
645+
return PyErr_NoMemory();
646+
}
647+
_PyObject_Init(self, type);
650648
return self;
651649
}
652650

653651
static PyObject *
654652
datetime_alloc(PyTypeObject *type, Py_ssize_t aware)
655653
{
656-
PyObject *self;
657-
658-
self = (PyObject *)
659-
PyObject_MALLOC(aware ?
660-
sizeof(PyDateTime_DateTime) :
661-
sizeof(_PyDateTime_BaseDateTime));
662-
if (self == NULL)
663-
return (PyObject *)PyErr_NoMemory();
664-
(void)PyObject_INIT(self, type);
654+
size_t size = aware ? sizeof(PyDateTime_DateTime) : sizeof(_PyDateTime_BaseDateTime);
655+
PyObject *self = (PyObject *)PyObject_Malloc(size);
656+
if (self == NULL) {
657+
return PyErr_NoMemory();
658+
}
659+
_PyObject_Init(self, type);
665660
return self;
666661
}
667662

Modules/gcmodule.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,8 +2263,10 @@ PyObject *
22632263
_PyObject_GC_New(PyTypeObject *tp)
22642264
{
22652265
PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
2266-
if (op != NULL)
2267-
op = PyObject_INIT(op, tp);
2266+
if (op == NULL) {
2267+
return NULL;
2268+
}
2269+
_PyObject_Init(op, tp);
22682270
return op;
22692271
}
22702272

@@ -2280,8 +2282,10 @@ _PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
22802282
}
22812283
size = _PyObject_VAR_SIZE(tp, nitems);
22822284
op = (PyVarObject *) _PyObject_GC_Malloc(size);
2283-
if (op != NULL)
2284-
op = PyObject_INIT_VAR(op, tp, nitems);
2285+
if (op == NULL) {
2286+
return NULL;
2287+
}
2288+
_PyObject_InitVar(op, tp, nitems);
22852289
return op;
22862290
}
22872291

Objects/bytesobject.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ _PyBytes_FromSize(Py_ssize_t size, int use_calloc)
7979
op = (PyBytesObject *)PyObject_Calloc(1, PyBytesObject_SIZE + size);
8080
else
8181
op = (PyBytesObject *)PyObject_Malloc(PyBytesObject_SIZE + size);
82-
if (op == NULL)
82+
if (op == NULL) {
8383
return PyErr_NoMemory();
84-
(void)PyObject_INIT_VAR(op, &PyBytes_Type, size);
84+
}
85+
_PyObject_InitVar((PyVarObject*)op, &PyBytes_Type, size);
8586
op->ob_shash = -1;
8687
if (!use_calloc)
8788
op->ob_sval[size] = '\0';
@@ -148,9 +149,10 @@ PyBytes_FromString(const char *str)
148149

149150
/* Inline PyObject_NewVar */
150151
op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size);
151-
if (op == NULL)
152+
if (op == NULL) {
152153
return PyErr_NoMemory();
153-
(void)PyObject_INIT_VAR(op, &PyBytes_Type, size);
154+
}
155+
_PyObject_InitVar((PyVarObject*)op, &PyBytes_Type, size);
154156
op->ob_shash = -1;
155157
memcpy(op->ob_sval, str, size+1);
156158
/* share short strings */
@@ -1435,9 +1437,10 @@ bytes_repeat(PyBytesObject *a, Py_ssize_t n)
14351437
return NULL;
14361438
}
14371439
op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + nbytes);
1438-
if (op == NULL)
1440+
if (op == NULL) {
14391441
return PyErr_NoMemory();
1440-
(void)PyObject_INIT_VAR(op, &PyBytes_Type, size);
1442+
}
1443+
_PyObject_InitVar((PyVarObject*)op, &PyBytes_Type, size);
14411444
op->ob_shash = -1;
14421445
op->ob_sval[size] = '\0';
14431446
if (Py_SIZE(a) == 1 && n > 0) {

Objects/complexobject.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
/* Submitted by Jim Hugunin */
77

88
#include "Python.h"
9+
#include "pycore_object.h" // _PyObject_Init()
910
#include "structmember.h" // PyMemberDef
1011

12+
1113
/*[clinic input]
1214
class complex "PyComplexObject *" "&PyComplex_Type"
1315
[clinic start generated code]*/
@@ -229,13 +231,12 @@ complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval)
229231
PyObject *
230232
PyComplex_FromCComplex(Py_complex cval)
231233
{
232-
PyComplexObject *op;
233-
234234
/* Inline PyObject_New */
235-
op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
236-
if (op == NULL)
235+
PyComplexObject *op = PyObject_MALLOC(sizeof(PyComplexObject));
236+
if (op == NULL) {
237237
return PyErr_NoMemory();
238-
(void)PyObject_INIT(op, &PyComplex_Type);
238+
}
239+
_PyObject_Init((PyObject*)op, &PyComplex_Type);
239240
op->cval = cval;
240241
return (PyObject *) op;
241242
}

Objects/floatobject.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
for any kind of float exception without losing portability. */
55

66
#include "Python.h"
7-
#include "pycore_dtoa.h"
7+
#include "pycore_dtoa.h" // _Py_dg_dtoa()
88
#include "pycore_interp.h" // _PyInterpreterState.float_state
9+
#include "pycore_object.h" // _PyObject_Init()
910
#include "pycore_pystate.h" // _PyInterpreterState_GET()
1011

1112
#include <ctype.h>
@@ -129,7 +130,7 @@ PyFloat_FromDouble(double fval)
129130
return PyErr_NoMemory();
130131
}
131132
}
132-
(void)PyObject_INIT(op, &PyFloat_Type);
133+
_PyObject_Init((PyObject*)op, &PyFloat_Type);
133134
op->ob_fval = fval;
134135
return (PyObject *) op;
135136
}

0 commit comments

Comments
 (0)