Skip to content

Commit b509d52

Browse files
authored
bpo-35059: PyObject_INIT() casts to PyObject* (GH-10674)
PyObject_INIT() and PyObject_INIT_VAR() now cast their first argument to PyObject*, as done in Python 3.7. Revert partially commit b4435e2.
1 parent 353933e commit b509d52

File tree

8 files changed

+18
-12
lines changed

8 files changed

+18
-12
lines changed

Include/objimpl.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,23 +145,29 @@ PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
145145
146146
These inline functions expect non-NULL object pointers. */
147147
static inline PyObject*
148-
PyObject_INIT(PyObject *op, PyTypeObject *typeobj)
148+
_PyObject_INIT(PyObject *op, PyTypeObject *typeobj)
149149
{
150150
assert(op != NULL);
151151
Py_TYPE(op) = typeobj;
152152
_Py_NewReference(op);
153153
return op;
154154
}
155155

156+
#define PyObject_INIT(op, typeobj) \
157+
_PyObject_INIT(_PyObject_CAST(op), (typeobj))
158+
156159
static inline PyVarObject*
157-
PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
160+
_PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
158161
{
159162
assert(op != NULL);
160163
Py_SIZE(op) = size;
161164
PyObject_INIT((PyObject *)op, typeobj);
162165
return op;
163166
}
164167

168+
#define PyObject_INIT_VAR(op, typeobj, size) \
169+
_PyObject_INIT_VAR(_PyVarObject_CAST(op), (typeobj), (size))
170+
165171
#define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
166172

167173
/* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a

Objects/bytesobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ _PyBytes_FromSize(Py_ssize_t size, int use_calloc)
8686
op = (PyBytesObject *)PyObject_Malloc(PyBytesObject_SIZE + size);
8787
if (op == NULL)
8888
return PyErr_NoMemory();
89-
(void)PyObject_INIT_VAR((PyVarObject *)op, &PyBytes_Type, size);
89+
(void)PyObject_INIT_VAR(op, &PyBytes_Type, size);
9090
op->ob_shash = -1;
9191
if (!use_calloc)
9292
op->ob_sval[size] = '\0';
@@ -164,7 +164,7 @@ PyBytes_FromString(const char *str)
164164
op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size);
165165
if (op == NULL)
166166
return PyErr_NoMemory();
167-
(void)PyObject_INIT_VAR((PyVarObject *)op, &PyBytes_Type, size);
167+
(void)PyObject_INIT_VAR(op, &PyBytes_Type, size);
168168
op->ob_shash = -1;
169169
memcpy(op->ob_sval, str, size+1);
170170
/* share short strings */
@@ -1509,7 +1509,7 @@ bytes_repeat(PyBytesObject *a, Py_ssize_t n)
15091509
op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + nbytes);
15101510
if (op == NULL)
15111511
return PyErr_NoMemory();
1512-
(void)PyObject_INIT_VAR((PyVarObject *)op, &PyBytes_Type, size);
1512+
(void)PyObject_INIT_VAR(op, &PyBytes_Type, size);
15131513
op->ob_shash = -1;
15141514
op->ob_sval[size] = '\0';
15151515
if (Py_SIZE(a) == 1 && n > 0) {

Objects/classobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ PyMethod_New(PyObject *func, PyObject *self)
5656
im = free_list;
5757
if (im != NULL) {
5858
free_list = (PyMethodObject *)(im->im_self);
59-
(void)PyObject_INIT((PyObject *)im, &PyMethod_Type);
59+
(void)PyObject_INIT(im, &PyMethod_Type);
6060
numfree--;
6161
}
6262
else {

Objects/complexobject.c

Lines changed: 1 addition & 1 deletion
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((PyObject *)op, &PyComplex_Type);
231+
(void)PyObject_INIT(op, &PyComplex_Type);
232232
op->cval = cval;
233233
return (PyObject *) op;
234234
}

Objects/floatobject.c

Lines changed: 1 addition & 1 deletion
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((PyObject *)op, &PyFloat_Type);
127+
(void)PyObject_INIT(op, &PyFloat_Type);
128128
op->ob_fval = fval;
129129
return (PyObject *) op;
130130
}

Objects/longobject.c

Lines changed: 2 additions & 2 deletions
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((PyVarObject *)result, &PyLong_Type, size);
214+
return (PyLongObject*)PyObject_INIT_VAR(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((PyObject *)v, &PyLong_Type);
5623+
(void)PyObject_INIT(v, &PyLong_Type);
56245624
}
56255625
Py_SIZE(v) = size;
56265626
v->ob_digit[0] = (digit)abs(ival);

Objects/methodobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
3232
op = free_list;
3333
if (op != NULL) {
3434
free_list = (PyCFunctionObject *)(op->m_self);
35-
(void)PyObject_INIT((PyObject *)op, &PyCFunction_Type);
35+
(void)PyObject_INIT(op, &PyCFunction_Type);
3636
numfree--;
3737
}
3838
else {

PC/winreg.c

Lines changed: 1 addition & 1 deletion
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((PyObject *)op, &PyHKEY_Type);
462+
PyObject_INIT(op, &PyHKEY_Type);
463463
op->hkey = h;
464464
return (PyObject *)op;
465465
}

0 commit comments

Comments
 (0)