summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2022-06-26 10:40:17 (GMT)
committerGitHub <noreply@github.com>2022-06-26 10:40:17 (GMT)
commit44c8e68b8cae2627ffe54a2ef407af0271981ed3 (patch)
tree977aed7c3ef385efc4da76899f09678b3f62057f /Modules
parent17ed560fcd0a1442485f9bd48884bbe412f35abc (diff)
downloadcpython-44c8e68b8cae2627ffe54a2ef407af0271981ed3.zip
cpython-44c8e68b8cae2627ffe54a2ef407af0271981ed3.tar.gz
cpython-44c8e68b8cae2627ffe54a2ef407af0271981ed3.tar.bz2
gh-87347: Fix PyObject_NEW() regression (#94234)
Don't add parenthesis around the type parameter. Add unit tests on PyObject_NEW() and similar functions.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_testcapimodule.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index f427a49..f8fb6ee 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},