summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-11-18 17:48:06 (GMT)
committerGitHub <noreply@github.com>2020-11-18 17:48:06 (GMT)
commit0e2ac21dd4960574e89561243763eabba685296a (patch)
tree62d5b28d9f66f40020d3fa403ac639a781d70d80 /Modules
parent2156d964a12285280c533af1c96eb273c58451e6 (diff)
downloadcpython-0e2ac21dd4960574e89561243763eabba685296a.zip
cpython-0e2ac21dd4960574e89561243763eabba685296a.tar.gz
cpython-0e2ac21dd4960574e89561243763eabba685296a.tar.bz2
bpo-39573: Convert Py_TYPE() and Py_SIZE() back to macros (GH-23366)
This change partically reverts commit ad3252bad905d41635bcbb4b76db30d570cf0087 and the commit fe2978b3b940fe2478335e3a2ca5ad22338cdf9c. Many third party C extension modules rely on the ability of using Py_TYPE() to set an object type: "Py_TYPE(obj) = type;" or to set an object type using: "Py_SIZE(obj) = size;".
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_testcapimodule.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 7b6da1e..a1d4c92 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -5612,6 +5612,30 @@ pynumber_tobase(PyObject *module, PyObject *args)
static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
+
+static PyObject*
+test_set_type_size(PyObject* self, PyObject* ignored)
+{
+ PyObject *obj = PyList_New(0);
+ if (obj == NULL) {
+ return NULL;
+ }
+
+ // Ensure that following tests don't modify the object,
+ // to ensure that Py_DECREF() will not crash.
+ assert(Py_TYPE(obj) == &PyList_Type);
+ assert(Py_SIZE(obj) == 0);
+
+ // bpo-39573: Check that Py_TYPE() and Py_SIZE() can be used
+ // as l-values to set an object type and size.
+ Py_TYPE(obj) = &PyList_Type;
+ Py_SIZE(obj) = 0;
+
+ Py_DECREF(obj);
+ Py_RETURN_NONE;
+}
+
+
static PyMethodDef TestMethods[] = {
{"raise_exception", raise_exception, METH_VARARGS},
{"raise_memoryerror", raise_memoryerror, METH_NOARGS},
@@ -5883,6 +5907,7 @@ static PyMethodDef TestMethods[] = {
{"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS},
{"pynumber_tobase", pynumber_tobase, METH_VARARGS},
{"without_gc", without_gc, METH_O},
+ {"test_set_type_size", test_set_type_size, METH_NOARGS},
{NULL, NULL} /* sentinel */
};