Skip to content

bpo-35059: Convert PyObject_INIT() to function #10077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions Include/objimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,29 @@ PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
#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 )

Expand Down
6 changes: 3 additions & 3 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,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(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';
Expand Down Expand Up @@ -163,7 +163,7 @@ PyBytes_FromString(const char *str)
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 */
Expand Down Expand Up @@ -1508,7 +1508,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(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) {
Expand Down
2 changes: 1 addition & 1 deletion Objects/classobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ PyMethod_New(PyObject *func, PyObject *self)
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 {
Expand Down
2 changes: 1 addition & 1 deletion Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ PyComplex_FromCComplex(Py_complex cval)
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;
}
Expand Down
2 changes: 1 addition & 1 deletion Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ PyFloat_FromDouble(double fval)
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;
}
Expand Down
4 changes: 2 additions & 2 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ _PyLong_New(Py_ssize_t size)
PyErr_NoMemory();
return NULL;
}
return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size);
return (PyLongObject*)PyObject_INIT_VAR((PyVarObject *)result, &PyLong_Type, size);
}

PyObject *
Expand Down Expand Up @@ -5620,7 +5620,7 @@ _PyLong_Init(void)
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);
Expand Down
2 changes: 1 addition & 1 deletion Objects/methodobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
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 {
Expand Down
2 changes: 1 addition & 1 deletion PC/winreg.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ PyHKEY_FromHKEY(HKEY h)
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;
}
Expand Down