@@ -5614,7 +5614,7 @@ static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
56145614
56155615
56165616static PyObject *
5617- test_set_type_size (PyObject * self , PyObject * ignored )
5617+ test_set_type_size (PyObject * self , PyObject * Py_UNUSED ( ignored ) )
56185618{
56195619 PyObject * obj = PyList_New (0 );
56205620 if (obj == NULL ) {
@@ -5636,6 +5636,35 @@ test_set_type_size(PyObject* self, PyObject* ignored)
56365636}
56375637
56385638
5639+ // Test Py_NewRef() and Py_XNewRef() functions
5640+ static PyObject *
5641+ test_refcount (PyObject * self , PyObject * Py_UNUSED (ignored ))
5642+ {
5643+ PyObject * obj = PyList_New (0 );
5644+ if (obj == NULL ) {
5645+ return NULL ;
5646+ }
5647+ assert (Py_REFCNT (obj ) == 1 );
5648+
5649+ // Test Py_NewRef()
5650+ PyObject * ref = Py_NewRef (obj );
5651+ assert (ref == obj );
5652+ assert (Py_REFCNT (obj ) == 2 );
5653+ Py_DECREF (ref );
5654+
5655+ // Test Py_XNewRef()
5656+ PyObject * xref = Py_XNewRef (obj );
5657+ assert (xref == obj );
5658+ assert (Py_REFCNT (obj ) == 2 );
5659+ Py_DECREF (xref );
5660+
5661+ assert (Py_XNewRef (NULL ) == NULL );
5662+
5663+ Py_DECREF (obj );
5664+ Py_RETURN_NONE ;
5665+ }
5666+
5667+
56395668static PyMethodDef TestMethods [] = {
56405669 {"raise_exception" , raise_exception , METH_VARARGS },
56415670 {"raise_memoryerror" , raise_memoryerror , METH_NOARGS },
@@ -5908,6 +5937,7 @@ static PyMethodDef TestMethods[] = {
59085937 {"pynumber_tobase" , pynumber_tobase , METH_VARARGS },
59095938 {"without_gc" , without_gc , METH_O },
59105939 {"test_set_type_size" , test_set_type_size , METH_NOARGS },
5940+ {"test_refcount" , test_refcount , METH_NOARGS },
59115941 {NULL , NULL } /* sentinel */
59125942};
59135943
0 commit comments