[Python-checkins] gh-87347: Fix PyObject_NEW() regression (#94234)

vstinner webhook-mailer at python.org
Sun Jun 26 06:40:22 EDT 2022


https://github.com/python/cpython/commit/44c8e68b8cae2627ffe54a2ef407af0271981ed3
commit: 44c8e68b8cae2627ffe54a2ef407af0271981ed3
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2022-06-26T12:40:17+02:00
summary:

gh-87347: Fix PyObject_NEW() regression (#94234)

Don't add parenthesis around the type parameter.

Add unit tests on PyObject_NEW() and similar functions.

files:
M Include/objimpl.h
M Modules/_testcapimodule.c

diff --git a/Include/objimpl.h b/Include/objimpl.h
index 140918198dae0..dde8df3483532 100644
--- a/Include/objimpl.h
+++ b/Include/objimpl.h
@@ -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))
 
 
 /*
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index f427a49047176..f8fb6eeec66f5 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -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;
 
@@ -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},



More information about the Python-checkins mailing list