Skip to content
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

gh-87347: Fix PyObject_NEW() regression #94234

Merged
merged 1 commit into from Jun 26, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Include/objimpl.h
Expand Up @@ -135,14 +135,14 @@ PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);

// Alias to PyObject_New(). In Python 3.8, PyObject_NEW() called directly
// PyObject_MALLOC() with _PyObject_SIZE().
#define PyObject_NEW(type, typeobj) PyObject_New((type), (typeobj))
#define PyObject_NEW(type, typeobj) PyObject_New(type, (typeobj))

#define PyObject_NewVar(type, typeobj, n) \
( (type *) _PyObject_NewVar((typeobj), (n)) )

// Alias to PyObject_NewVar(). In Python 3.8, PyObject_NEW_VAR() called
// directly PyObject_MALLOC() with _PyObject_VAR_SIZE().
#define PyObject_NEW_VAR(type, typeobj, n) PyObject_NewVar((type), (typeobj), (n))
#define PyObject_NEW_VAR(type, typeobj, n) PyObject_NewVar(type, (typeobj), (n))


/*
Expand Down
43 changes: 43 additions & 0 deletions Modules/_testcapimodule.c
Expand Up @@ -4188,6 +4188,48 @@ test_pymem_alloc0(PyObject *self, PyObject *Py_UNUSED(ignored))
Py_RETURN_NONE;
}

static PyObject *
test_pymem_new(PyObject *self, PyObject *Py_UNUSED(ignored))
{
char *ptr;
PyTypeObject *type = &PyBaseObject_Type;
PyTypeObject *var_type = &PyLong_Type;

// PyObject_New()
ptr = PyObject_New(char, type);
if (ptr == NULL) {
goto alloc_failed;
}
PyObject_Free(ptr);

// PyObject_NEW()
ptr = PyObject_NEW(char, type);
if (ptr == NULL) {
goto alloc_failed;
}
PyObject_Free(ptr);

// PyObject_NewVar()
ptr = PyObject_NewVar(char, var_type, 3);
if (ptr == NULL) {
goto alloc_failed;
}
PyObject_Free(ptr);

// PyObject_NEW_VAR()
ptr = PyObject_NEW_VAR(char, var_type, 3);
if (ptr == NULL) {
goto alloc_failed;
}
PyObject_Free(ptr);

Py_RETURN_NONE;

alloc_failed:
PyErr_NoMemory();
return NULL;
}

typedef struct {
PyMemAllocatorEx alloc;

Expand Down Expand Up @@ -6284,6 +6326,7 @@ static PyMethodDef TestMethods[] = {
{"with_tp_del", with_tp_del, METH_VARARGS},
{"create_cfunction", create_cfunction, METH_NOARGS},
{"test_pymem_alloc0", test_pymem_alloc0, METH_NOARGS},
{"test_pymem_new", test_pymem_new, METH_NOARGS},
{"test_pymem_setrawallocators",test_pymem_setrawallocators, METH_NOARGS},
{"test_pymem_setallocators",test_pymem_setallocators, METH_NOARGS},
{"test_pyobject_setallocators",test_pyobject_setallocators, METH_NOARGS},
Expand Down