diff options
author | Victor Stinner <vstinner@python.org> | 2020-12-03 13:01:10 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-03 13:01:10 (GMT) |
commit | 8b6c4a921af6d5d0a9640211ac93d7886a55a8f3 (patch) | |
tree | 0c2af672c699e9d07e0da8780b4d87309b1dc3fc /Modules/_testcapimodule.c | |
parent | 7e5e13d113798117d5ef25c5ffdbd0eb39420f98 (diff) | |
download | cpython-8b6c4a921af6d5d0a9640211ac93d7886a55a8f3.zip cpython-8b6c4a921af6d5d0a9640211ac93d7886a55a8f3.tar.gz cpython-8b6c4a921af6d5d0a9640211ac93d7886a55a8f3.tar.bz2 |
bpo-42262: Py_NewRef() casts its argument to PyObject* (GH-23626)
Write also unit tests on Py_NewRef() and Py_XNewRef().
Diffstat (limited to 'Modules/_testcapimodule.c')
-rw-r--r-- | Modules/_testcapimodule.c | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index d210442..4f97927 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -5614,7 +5614,7 @@ static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *); static PyObject* -test_set_type_size(PyObject* self, PyObject* ignored) +test_set_type_size(PyObject *self, PyObject *Py_UNUSED(ignored)) { PyObject *obj = PyList_New(0); if (obj == NULL) { @@ -5636,6 +5636,35 @@ test_set_type_size(PyObject* self, PyObject* ignored) } +// Test Py_NewRef() and Py_XNewRef() functions +static PyObject* +test_refcount(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *obj = PyList_New(0); + if (obj == NULL) { + return NULL; + } + assert(Py_REFCNT(obj) == 1); + + // Test Py_NewRef() + PyObject *ref = Py_NewRef(obj); + assert(ref == obj); + assert(Py_REFCNT(obj) == 2); + Py_DECREF(ref); + + // Test Py_XNewRef() + PyObject *xref = Py_XNewRef(obj); + assert(xref == obj); + assert(Py_REFCNT(obj) == 2); + Py_DECREF(xref); + + assert(Py_XNewRef(NULL) == NULL); + + Py_DECREF(obj); + Py_RETURN_NONE; +} + + static PyMethodDef TestMethods[] = { {"raise_exception", raise_exception, METH_VARARGS}, {"raise_memoryerror", raise_memoryerror, METH_NOARGS}, @@ -5908,6 +5937,7 @@ static PyMethodDef TestMethods[] = { {"pynumber_tobase", pynumber_tobase, METH_VARARGS}, {"without_gc", without_gc, METH_O}, {"test_set_type_size", test_set_type_size, METH_NOARGS}, + {"test_refcount", test_refcount, METH_NOARGS}, {NULL, NULL} /* sentinel */ }; |