bpo-40170: PyObject_NEW() becomes an alias to PyObject_New() (GH-19379) · python/cpython@9205520
@@ -122,12 +122,18 @@ PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *,
122122PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *);
123123PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
124124125-#define PyObject_New(type, typeobj) \
126- ( (type *) _PyObject_New(typeobj) )
125+#define PyObject_New(type, typeobj) ((type *)_PyObject_New(typeobj))
126+127+// Alias to PyObject_New(). In Python 3.8, PyObject_NEW() called directly
128+// PyObject_MALLOC() with _PyObject_SIZE().
129+#define PyObject_NEW(type, typeobj) PyObject_New(type, typeobj)
130+127131#define PyObject_NewVar(type, typeobj, n) \
128132 ( (type *) _PyObject_NewVar((typeobj), (n)) )
129133130-#define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
134+// Alias to PyObject_New(). In Python 3.8, PyObject_NEW() called directly
135+// PyObject_MALLOC() with _PyObject_VAR_SIZE().
136+#define PyObject_NEW_VAR(type, typeobj, n) PyObject_NewVar(type, typeobj, n)
131137132138133139#ifdef Py_LIMITED_API
@@ -143,64 +149,6 @@ PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
143149#endif
144150145151146-/* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
147- vrbl-size object with nitems items, exclusive of gc overhead (if any). The
148- value is rounded up to the closest multiple of sizeof(void *), in order to
149- ensure that pointer fields at the end of the object are correctly aligned
150- for the platform (this is of special importance for subclasses of, e.g.,
151- str or int, so that pointers can be stored after the embedded data).
152-153- Note that there's no memory wastage in doing this, as malloc has to
154- return (at worst) pointer-aligned memory anyway.
155-*/
156-#if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0
157-# error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
158-#endif
159-160-#define _PyObject_VAR_SIZE(typeobj, nitems) \
161- _Py_SIZE_ROUND_UP((typeobj)->tp_basicsize + \
162- (nitems)*(typeobj)->tp_itemsize, \
163- SIZEOF_VOID_P)
164-165-#define PyObject_NEW(type, typeobj) \
166-( (type *) PyObject_Init( \
167- (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
168-169-#define PyObject_NEW_VAR(type, typeobj, n) \
170-( (type *) PyObject_InitVar( \
171- (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj),(n)) ),\
172- (typeobj), (n)) )
173-174-/* This example code implements an object constructor with a custom
175- allocator, where PyObject_New is inlined, and shows the important
176- distinction between two steps (at least):
177- 1) the actual allocation of the object storage;
178- 2) the initialization of the Python specific fields
179- in this storage with PyObject_{Init, InitVar}.
180-181- PyObject *
182- YourObject_New(...)
183- {
184- PyObject *op;
185-186- op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
187- if (op == NULL)
188- return PyErr_NoMemory();
189-190- PyObject_Init(op, &YourTypeStruct);
191-192- op->ob_field = value;
193- ...
194- return op;
195- }
196-197- Note that in C++, the use of the new operator usually implies that
198- the 1st step is performed automatically for you, so in a C++ class
199- constructor you would start directly with PyObject_Init/InitVar
200-*/
201-202-203-204152/*
205153 * Garbage Collection Support
206154 * ==========================