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*.
diff --git a/Include/objimpl.h b/Include/objimpl.h
index 4eeb8df..6ce6455 100644
--- a/Include/objimpl.h
+++ b/Include/objimpl.h
@@ -138,12 +138,29 @@
 #define PyObject_NewVar(type, typeobj, n) \
                 ( (type *) _PyObject_NewVar((typeobj), (n)) )
 
-/* Macros trading binary compatibility for speed. See also pymem.h.
-   Note that these macros expect non-NULL object pointers.*/
-#define PyObject_INIT(op, typeobj) \
-    ( Py_TYPE(op) = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
-#define PyObject_INIT_VAR(op, typeobj, size) \
-    ( Py_SIZE(op) = (size), PyObject_INIT((op), (typeobj)) )
+/* Inline functions trading binary compatibility for speed:
+   PyObject_INIT() is the fast version of PyObject_Init(), and
+   PyObject_INIT_VAR() is the fast version of PyObject_InitVar.
+   See also pymem.h.
+
+   These inline functions expect non-NULL object pointers. */
+Py_STATIC_INLINE(PyObject*)
+PyObject_INIT(PyObject *op, PyTypeObject *typeobj)
+{
+    assert(op != NULL);
+    Py_TYPE(op) = typeobj;
+    _Py_NewReference(op);
+    return op;
+}
+
+Py_STATIC_INLINE(PyVarObject*)
+PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
+{
+    assert(op != NULL);
+    Py_SIZE(op) = size;
+    PyObject_INIT((PyObject *)op, typeobj);
+    return op;
+}
 
 #define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
 
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index c412393..9a08814 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -85,7 +85,7 @@
         op = (PyBytesObject *)PyObject_Malloc(PyBytesObject_SIZE + size);
     if (op == NULL)
         return PyErr_NoMemory();
-    (void)PyObject_INIT_VAR(op, &PyBytes_Type, size);
+    (void)PyObject_INIT_VAR((PyVarObject *)op, &PyBytes_Type, size);
     op->ob_shash = -1;
     if (!use_calloc)
         op->ob_sval[size] = '\0';
@@ -163,7 +163,7 @@
     op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size);
     if (op == NULL)
         return PyErr_NoMemory();
-    (void)PyObject_INIT_VAR(op, &PyBytes_Type, size);
+    (void)PyObject_INIT_VAR((PyVarObject *)op, &PyBytes_Type, size);
     op->ob_shash = -1;
     memcpy(op->ob_sval, str, size+1);
     /* share short strings */
@@ -1508,7 +1508,7 @@
     op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + nbytes);
     if (op == NULL)
         return PyErr_NoMemory();
-    (void)PyObject_INIT_VAR(op, &PyBytes_Type, size);
+    (void)PyObject_INIT_VAR((PyVarObject *)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 a193ada..c4efaf2 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -55,7 +55,7 @@
     im = free_list;
     if (im != NULL) {
         free_list = (PyMethodObject *)(im->im_self);
-        (void)PyObject_INIT(im, &PyMethod_Type);
+        (void)PyObject_INIT((PyObject *)im, &PyMethod_Type);
         numfree--;
     }
     else {
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 6e3d47b..eecdb52 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -228,7 +228,7 @@
     op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
     if (op == NULL)
         return PyErr_NoMemory();
-    (void)PyObject_INIT(op, &PyComplex_Type);
+    (void)PyObject_INIT((PyObject *)op, &PyComplex_Type);
     op->cval = cval;
     return (PyObject *) op;
 }
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 67f9e5d..8d83f00 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -124,7 +124,7 @@
             return PyErr_NoMemory();
     }
     /* Inline PyObject_New */
-    (void)PyObject_INIT(op, &PyFloat_Type);
+    (void)PyObject_INIT((PyObject *)op, &PyFloat_Type);
     op->ob_fval = fval;
     return (PyObject *) op;
 }
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 6f7fe33..ab5ac34 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -211,7 +211,7 @@
         PyErr_NoMemory();
         return NULL;
     }
-    return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size);
+    return (PyLongObject*)PyObject_INIT_VAR((PyVarObject *)result, &PyLong_Type, size);
 }
 
 PyObject *
@@ -5620,7 +5620,7 @@
             assert(v->ob_digit[0] == (digit)abs(ival));
         }
         else {
-            (void)PyObject_INIT(v, &PyLong_Type);
+            (void)PyObject_INIT((PyObject *)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 5ad2831..9176e39 100644
--- a/Objects/methodobject.c
+++ b/Objects/methodobject.c
@@ -31,7 +31,7 @@
     op = free_list;
     if (op != NULL) {
         free_list = (PyCFunctionObject *)(op->m_self);
-        (void)PyObject_INIT(op, &PyCFunction_Type);
+        (void)PyObject_INIT((PyObject *)op, &PyCFunction_Type);
         numfree--;
     }
     else {
diff --git a/PC/winreg.c b/PC/winreg.c
index 78864b1..4f5a676 100644
--- a/PC/winreg.c
+++ b/PC/winreg.c
@@ -459,7 +459,7 @@
     op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
     if (op == NULL)
         return PyErr_NoMemory();
-    PyObject_INIT(op, &PyHKEY_Type);
+    PyObject_INIT((PyObject *)op, &PyHKEY_Type);
     op->hkey = h;
     return (PyObject *)op;
 }