[Python-checkins] bpo-35059: PyObject_INIT() casts to PyObject* (GH-10674)

Victor Stinner webhook-mailer at python.org
Fri Nov 23 08:27:42 EST 2018


https://github.com/python/cpython/commit/b509d52083e156f97d6bd36f2f894a052e960f03
commit: b509d52083e156f97d6bd36f2f894a052e960f03
branch: master
author: Victor Stinner <vstinner at redhat.com>
committer: GitHub <noreply at github.com>
date: 2018-11-23T14:27:38+01:00
summary:

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 b4435e20a92af474f117b78b98ddc6f515363af5.

files:
M Include/objimpl.h
M Objects/bytesobject.c
M Objects/classobject.c
M Objects/complexobject.c
M Objects/floatobject.c
M Objects/longobject.c
M Objects/methodobject.c
M PC/winreg.c

diff --git a/Include/objimpl.h b/Include/objimpl.h
index 1c50d8bd6c04..aee3fdc3747a 100644
--- a/Include/objimpl.h
+++ b/Include/objimpl.h
@@ -145,7 +145,7 @@ PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
 
    These inline functions expect non-NULL object pointers. */
 static inline PyObject*
-PyObject_INIT(PyObject *op, PyTypeObject *typeobj)
+_PyObject_INIT(PyObject *op, PyTypeObject *typeobj)
 {
     assert(op != NULL);
     Py_TYPE(op) = typeobj;
@@ -153,8 +153,11 @@ PyObject_INIT(PyObject *op, PyTypeObject *typeobj)
     return op;
 }
 
+#define PyObject_INIT(op, typeobj) \
+    _PyObject_INIT(_PyObject_CAST(op), (typeobj))
+
 static inline PyVarObject*
-PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
+_PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
 {
     assert(op != NULL);
     Py_SIZE(op) = size;
@@ -162,6 +165,9 @@ PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
     return op;
 }
 
+#define PyObject_INIT_VAR(op, typeobj, size) \
+    _PyObject_INIT_VAR(_PyVarObject_CAST(op), (typeobj), (size))
+
 #define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
 
 /* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index bed75ee49e27..e4a49731aba6 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -86,7 +86,7 @@ _PyBytes_FromSize(Py_ssize_t size, int use_calloc)
         op = (PyBytesObject *)PyObject_Malloc(PyBytesObject_SIZE + size);
     if (op == NULL)
         return PyErr_NoMemory();
-    (void)PyObject_INIT_VAR((PyVarObject *)op, &PyBytes_Type, size);
+    (void)PyObject_INIT_VAR(op, &PyBytes_Type, size);
     op->ob_shash = -1;
     if (!use_calloc)
         op->ob_sval[size] = '\0';
@@ -164,7 +164,7 @@ PyBytes_FromString(const char *str)
     op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size);
     if (op == NULL)
         return PyErr_NoMemory();
-    (void)PyObject_INIT_VAR((PyVarObject *)op, &PyBytes_Type, size);
+    (void)PyObject_INIT_VAR(op, &PyBytes_Type, size);
     op->ob_shash = -1;
     memcpy(op->ob_sval, str, size+1);
     /* share short strings */
@@ -1509,7 +1509,7 @@ bytes_repeat(PyBytesObject *a, Py_ssize_t n)
     op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + nbytes);
     if (op == NULL)
         return PyErr_NoMemory();
-    (void)PyObject_INIT_VAR((PyVarObject *)op, &PyBytes_Type, size);
+    (void)PyObject_INIT_VAR(op, &PyBytes_Type, size);
     op->ob_shash = -1;
     op->ob_sval[size] = '\0';
     if (Py_SIZE(a) == 1 && n > 0) {
diff --git a/Objects/classobject.c b/Objects/classobject.c
index 6d1f05ccd3a9..1eed5d36ab03 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -56,7 +56,7 @@ PyMethod_New(PyObject *func, PyObject *self)
     im = free_list;
     if (im != NULL) {
         free_list = (PyMethodObject *)(im->im_self);
-        (void)PyObject_INIT((PyObject *)im, &PyMethod_Type);
+        (void)PyObject_INIT(im, &PyMethod_Type);
         numfree--;
     }
     else {
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index eecdb5250653..6e3d47b62d19 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -228,7 +228,7 @@ PyComplex_FromCComplex(Py_complex cval)
     op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
     if (op == NULL)
         return PyErr_NoMemory();
-    (void)PyObject_INIT((PyObject *)op, &PyComplex_Type);
+    (void)PyObject_INIT(op, &PyComplex_Type);
     op->cval = cval;
     return (PyObject *) op;
 }
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 8d83f00452e5..67f9e5d5b4ef 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -124,7 +124,7 @@ PyFloat_FromDouble(double fval)
             return PyErr_NoMemory();
     }
     /* Inline PyObject_New */
-    (void)PyObject_INIT((PyObject *)op, &PyFloat_Type);
+    (void)PyObject_INIT(op, &PyFloat_Type);
     op->ob_fval = fval;
     return (PyObject *) op;
 }
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 26d6c53fa9ff..f42683e9d024 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -211,7 +211,7 @@ _PyLong_New(Py_ssize_t size)
         PyErr_NoMemory();
         return NULL;
     }
-    return (PyLongObject*)PyObject_INIT_VAR((PyVarObject *)result, &PyLong_Type, size);
+    return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size);
 }
 
 PyObject *
@@ -5620,7 +5620,7 @@ _PyLong_Init(void)
             assert(v->ob_digit[0] == (digit)abs(ival));
         }
         else {
-            (void)PyObject_INIT((PyObject *)v, &PyLong_Type);
+            (void)PyObject_INIT(v, &PyLong_Type);
         }
         Py_SIZE(v) = size;
         v->ob_digit[0] = (digit)abs(ival);
diff --git a/Objects/methodobject.c b/Objects/methodobject.c
index 23325e2a1b3e..6dec64276de4 100644
--- a/Objects/methodobject.c
+++ b/Objects/methodobject.c
@@ -32,7 +32,7 @@ PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
     op = free_list;
     if (op != NULL) {
         free_list = (PyCFunctionObject *)(op->m_self);
-        (void)PyObject_INIT((PyObject *)op, &PyCFunction_Type);
+        (void)PyObject_INIT(op, &PyCFunction_Type);
         numfree--;
     }
     else {
diff --git a/PC/winreg.c b/PC/winreg.c
index c6ad7ab64e16..0198097a1c92 100644
--- a/PC/winreg.c
+++ b/PC/winreg.c
@@ -459,7 +459,7 @@ PyHKEY_FromHKEY(HKEY h)
     op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
     if (op == NULL)
         return PyErr_NoMemory();
-    PyObject_INIT((PyObject *)op, &PyHKEY_Type);
+    PyObject_INIT(op, &PyHKEY_Type);
     op->hkey = h;
     return (PyObject *)op;
 }



More information about the Python-checkins mailing list