Skip to content

Commit b4435e2

Browse files
authored
bpo-35059: Convert PyObject_INIT() to function (GH-10077)
* Convert PyObject_INIT() and PyObject_INIT_VAR() macros to static inline functions. * Fix usage of these functions: cast to PyObject* or PyVarObject*.
1 parent 7cd2543 commit b4435e2

File tree

8 files changed

+33
-16
lines changed

8 files changed

+33
-16
lines changed

Include/objimpl.h

+23-6
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,29 @@ PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
138138
#define PyObject_NewVar(type, typeobj, n) \
139139
( (type *) _PyObject_NewVar((typeobj), (n)) )
140140

141-
/* Macros trading binary compatibility for speed. See also pymem.h.
142-
Note that these macros expect non-NULL object pointers.*/
143-
#define PyObject_INIT(op, typeobj) \
144-
( Py_TYPE(op) = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
145-
#define PyObject_INIT_VAR(op, typeobj, size) \
146-
( Py_SIZE(op) = (size), PyObject_INIT((op), (typeobj)) )
141+
/* Inline functions trading binary compatibility for speed:
142+
PyObject_INIT() is the fast version of PyObject_Init(), and
143+
PyObject_INIT_VAR() is the fast version of PyObject_InitVar.
144+
See also pymem.h.
145+
146+
These inline functions expect non-NULL object pointers. */
147+
Py_STATIC_INLINE(PyObject*)
148+
PyObject_INIT(PyObject *op, PyTypeObject *typeobj)
149+
{
150+
assert(op != NULL);
151+
Py_TYPE(op) = typeobj;
152+
_Py_NewReference(op);
153+
return op;
154+
}
155+
156+
Py_STATIC_INLINE(PyVarObject*)
157+
PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
158+
{
159+
assert(op != NULL);
160+
Py_SIZE(op) = size;
161+
PyObject_INIT((PyObject *)op, typeobj);
162+
return op;
163+
}
147164

148165
#define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
149166

Objects/bytesobject.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ _PyBytes_FromSize(Py_ssize_t size, int use_calloc)
8585
op = (PyBytesObject *)PyObject_Malloc(PyBytesObject_SIZE + size);
8686
if (op == NULL)
8787
return PyErr_NoMemory();
88-
(void)PyObject_INIT_VAR(op, &PyBytes_Type, size);
88+
(void)PyObject_INIT_VAR((PyVarObject *)op, &PyBytes_Type, size);
8989
op->ob_shash = -1;
9090
if (!use_calloc)
9191
op->ob_sval[size] = '\0';
@@ -163,7 +163,7 @@ PyBytes_FromString(const char *str)
163163
op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size);
164164
if (op == NULL)
165165
return PyErr_NoMemory();
166-
(void)PyObject_INIT_VAR(op, &PyBytes_Type, size);
166+
(void)PyObject_INIT_VAR((PyVarObject *)op, &PyBytes_Type, size);
167167
op->ob_shash = -1;
168168
memcpy(op->ob_sval, str, size+1);
169169
/* share short strings */
@@ -1508,7 +1508,7 @@ bytes_repeat(PyBytesObject *a, Py_ssize_t n)
15081508
op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + nbytes);
15091509
if (op == NULL)
15101510
return PyErr_NoMemory();
1511-
(void)PyObject_INIT_VAR(op, &PyBytes_Type, size);
1511+
(void)PyObject_INIT_VAR((PyVarObject *)op, &PyBytes_Type, size);
15121512
op->ob_shash = -1;
15131513
op->ob_sval[size] = '\0';
15141514
if (Py_SIZE(a) == 1 && n > 0) {

Objects/classobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ PyMethod_New(PyObject *func, PyObject *self)
5555
im = free_list;
5656
if (im != NULL) {
5757
free_list = (PyMethodObject *)(im->im_self);
58-
(void)PyObject_INIT(im, &PyMethod_Type);
58+
(void)PyObject_INIT((PyObject *)im, &PyMethod_Type);
5959
numfree--;
6060
}
6161
else {

Objects/complexobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ PyComplex_FromCComplex(Py_complex cval)
228228
op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
229229
if (op == NULL)
230230
return PyErr_NoMemory();
231-
(void)PyObject_INIT(op, &PyComplex_Type);
231+
(void)PyObject_INIT((PyObject *)op, &PyComplex_Type);
232232
op->cval = cval;
233233
return (PyObject *) op;
234234
}

Objects/floatobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ PyFloat_FromDouble(double fval)
124124
return PyErr_NoMemory();
125125
}
126126
/* Inline PyObject_New */
127-
(void)PyObject_INIT(op, &PyFloat_Type);
127+
(void)PyObject_INIT((PyObject *)op, &PyFloat_Type);
128128
op->ob_fval = fval;
129129
return (PyObject *) op;
130130
}

Objects/longobject.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ _PyLong_New(Py_ssize_t size)
211211
PyErr_NoMemory();
212212
return NULL;
213213
}
214-
return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size);
214+
return (PyLongObject*)PyObject_INIT_VAR((PyVarObject *)result, &PyLong_Type, size);
215215
}
216216

217217
PyObject *
@@ -5620,7 +5620,7 @@ _PyLong_Init(void)
56205620
assert(v->ob_digit[0] == (digit)abs(ival));
56215621
}
56225622
else {
5623-
(void)PyObject_INIT(v, &PyLong_Type);
5623+
(void)PyObject_INIT((PyObject *)v, &PyLong_Type);
56245624
}
56255625
Py_SIZE(v) = size;
56265626
v->ob_digit[0] = (digit)abs(ival);

Objects/methodobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
3131
op = free_list;
3232
if (op != NULL) {
3333
free_list = (PyCFunctionObject *)(op->m_self);
34-
(void)PyObject_INIT(op, &PyCFunction_Type);
34+
(void)PyObject_INIT((PyObject *)op, &PyCFunction_Type);
3535
numfree--;
3636
}
3737
else {

PC/winreg.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ PyHKEY_FromHKEY(HKEY h)
459459
op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
460460
if (op == NULL)
461461
return PyErr_NoMemory();
462-
PyObject_INIT(op, &PyHKEY_Type);
462+
PyObject_INIT((PyObject *)op, &PyHKEY_Type);
463463
op->hkey = h;
464464
return (PyObject *)op;
465465
}

0 commit comments

Comments
 (0)