Skip to content

Commit 44c8e68

Browse files
authored
gh-87347: Fix PyObject_NEW() regression (#94234)
Don't add parenthesis around the type parameter. Add unit tests on PyObject_NEW() and similar functions.
1 parent 17ed560 commit 44c8e68

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

Include/objimpl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
135135

136136
// Alias to PyObject_New(). In Python 3.8, PyObject_NEW() called directly
137137
// PyObject_MALLOC() with _PyObject_SIZE().
138-
#define PyObject_NEW(type, typeobj) PyObject_New((type), (typeobj))
138+
#define PyObject_NEW(type, typeobj) PyObject_New(type, (typeobj))
139139

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

143143
// Alias to PyObject_NewVar(). In Python 3.8, PyObject_NEW_VAR() called
144144
// directly PyObject_MALLOC() with _PyObject_VAR_SIZE().
145-
#define PyObject_NEW_VAR(type, typeobj, n) PyObject_NewVar((type), (typeobj), (n))
145+
#define PyObject_NEW_VAR(type, typeobj, n) PyObject_NewVar(type, (typeobj), (n))
146146

147147

148148
/*

Modules/_testcapimodule.c

+43
Original file line numberDiff line numberDiff line change
@@ -4188,6 +4188,48 @@ test_pymem_alloc0(PyObject *self, PyObject *Py_UNUSED(ignored))
41884188
Py_RETURN_NONE;
41894189
}
41904190

4191+
static PyObject *
4192+
test_pymem_new(PyObject *self, PyObject *Py_UNUSED(ignored))
4193+
{
4194+
char *ptr;
4195+
PyTypeObject *type = &PyBaseObject_Type;
4196+
PyTypeObject *var_type = &PyLong_Type;
4197+
4198+
// PyObject_New()
4199+
ptr = PyObject_New(char, type);
4200+
if (ptr == NULL) {
4201+
goto alloc_failed;
4202+
}
4203+
PyObject_Free(ptr);
4204+
4205+
// PyObject_NEW()
4206+
ptr = PyObject_NEW(char, type);
4207+
if (ptr == NULL) {
4208+
goto alloc_failed;
4209+
}
4210+
PyObject_Free(ptr);
4211+
4212+
// PyObject_NewVar()
4213+
ptr = PyObject_NewVar(char, var_type, 3);
4214+
if (ptr == NULL) {
4215+
goto alloc_failed;
4216+
}
4217+
PyObject_Free(ptr);
4218+
4219+
// PyObject_NEW_VAR()
4220+
ptr = PyObject_NEW_VAR(char, var_type, 3);
4221+
if (ptr == NULL) {
4222+
goto alloc_failed;
4223+
}
4224+
PyObject_Free(ptr);
4225+
4226+
Py_RETURN_NONE;
4227+
4228+
alloc_failed:
4229+
PyErr_NoMemory();
4230+
return NULL;
4231+
}
4232+
41914233
typedef struct {
41924234
PyMemAllocatorEx alloc;
41934235

@@ -6284,6 +6326,7 @@ static PyMethodDef TestMethods[] = {
62846326
{"with_tp_del", with_tp_del, METH_VARARGS},
62856327
{"create_cfunction", create_cfunction, METH_NOARGS},
62866328
{"test_pymem_alloc0", test_pymem_alloc0, METH_NOARGS},
6329+
{"test_pymem_new", test_pymem_new, METH_NOARGS},
62876330
{"test_pymem_setrawallocators",test_pymem_setrawallocators, METH_NOARGS},
62886331
{"test_pymem_setallocators",test_pymem_setallocators, METH_NOARGS},
62896332
{"test_pyobject_setallocators",test_pyobject_setallocators, METH_NOARGS},

0 commit comments

Comments
 (0)