@@ -5401,32 +5401,98 @@ test_set_type_size(PyObject *self, PyObject *Py_UNUSED(ignored))
54015401}
54025402
54035403
5404- // Test Py_NewRef() and Py_XNewRef() functions
5404+ #define TEST_REFCOUNT () \
5405+ do { \
5406+ PyObject *obj = PyList_New(0); \
5407+ if (obj == NULL) { \
5408+ return NULL; \
5409+ } \
5410+ assert(Py_REFCNT(obj) == 1); \
5411+ \
5412+ /* test Py_NewRef() */ \
5413+ PyObject * ref = Py_NewRef (obj ) ; \
5414+ assert (ref == obj ); \
5415+ assert (Py_REFCNT (obj ) == 2 ); \
5416+ Py_DECREF (ref ); \
5417+ \
5418+ /* test Py_XNewRef() */ \
5419+ PyObject * xref = Py_XNewRef (obj ); \
5420+ assert (xref == obj ); \
5421+ assert (Py_REFCNT (obj ) == 2 ); \
5422+ Py_DECREF (xref ); \
5423+ \
5424+ assert (Py_XNewRef (NULL ) == NULL ); \
5425+ \
5426+ Py_DECREF (obj ); \
5427+ Py_RETURN_NONE ; \
5428+ } while (0 ) \
5429+
5430+
5431+ // Test Py_NewRef() and Py_XNewRef() macros
54055432static PyObject *
5406- test_refcount (PyObject * self , PyObject * Py_UNUSED (ignored ))
5433+ test_refcount_macros (PyObject * self , PyObject * Py_UNUSED (ignored ))
54075434{
5408- PyObject * obj = PyList_New (0 );
5409- if (obj == NULL ) {
5410- return NULL ;
5411- }
5412- assert (Py_REFCNT (obj ) == 1 );
5435+ TEST_REFCOUNT ();
5436+ }
5437+
5438+ #undef Py_NewRef
5439+ #undef Py_XNewRef
5440+
5441+ // Test Py_NewRef() and Py_XNewRef() functions, after undefining macros.
5442+ static PyObject *
5443+ test_refcount_funcs (PyObject * self , PyObject * Py_UNUSED (ignored ))
5444+ {
5445+ TEST_REFCOUNT ();
5446+ }
54135447
5414- // Test Py_NewRef()
5415- PyObject * ref = Py_NewRef (obj );
5416- assert (ref == obj );
5417- assert (Py_REFCNT (obj ) == 2 );
5418- Py_DECREF (ref );
54195448
5420- // Test Py_XNewRef()
5421- PyObject * xref = Py_XNewRef (obj );
5422- assert (xref == obj );
5423- assert (Py_REFCNT (obj ) == 2 );
5424- Py_DECREF (xref );
5449+ // Test Py_Is() function
5450+ #define TEST_PY_IS () \
5451+ do { \
5452+ PyObject *o_none = Py_None; \
5453+ PyObject *o_true = Py_True; \
5454+ PyObject *o_false = Py_False; \
5455+ PyObject *obj = PyList_New(0); \
5456+ if (obj == NULL) { \
5457+ return NULL; \
5458+ } \
5459+ \
5460+ /* test Py_Is() */ \
5461+ assert (Py_Is (obj , obj )); \
5462+ assert (!Py_Is (obj , o_none )); \
5463+ \
5464+ /* test Py_None */ \
5465+ assert (Py_Is (o_none , o_none )); \
5466+ assert (!Py_Is (obj , o_none )); \
5467+ \
5468+ /* test Py_True */ \
5469+ assert (Py_Is (o_true , o_true )); \
5470+ assert (!Py_Is (o_false , o_true )); \
5471+ assert (!Py_Is (obj , o_true )); \
5472+ \
5473+ /* test Py_False */ \
5474+ assert (Py_Is (o_false , o_false )); \
5475+ assert (!Py_Is (o_true , o_false )); \
5476+ assert (!Py_Is (obj , o_false )); \
5477+ \
5478+ Py_DECREF (obj ); \
5479+ Py_RETURN_NONE ; \
5480+ } while (0 )
5481+
5482+ // Test Py_Is() macro
5483+ static PyObject *
5484+ test_py_is_macros (PyObject * self , PyObject * Py_UNUSED (ignored ))
5485+ {
5486+ TEST_PY_IS ();
5487+ }
54255488
5426- assert ( Py_XNewRef ( NULL ) == NULL );
5489+ #undef Py_Is
54275490
5428- Py_DECREF (obj );
5429- Py_RETURN_NONE ;
5491+ // Test Py_Is() function, after undefining its macro.
5492+ static PyObject *
5493+ test_py_is_funcs (PyObject * self , PyObject * Py_UNUSED (ignored ))
5494+ {
5495+ TEST_PY_IS ();
54305496}
54315497
54325498
@@ -5716,7 +5782,10 @@ static PyMethodDef TestMethods[] = {
57165782 {"pynumber_tobase" , pynumber_tobase , METH_VARARGS },
57175783 {"without_gc" , without_gc , METH_O },
57185784 {"test_set_type_size" , test_set_type_size , METH_NOARGS },
5719- {"test_refcount" , test_refcount , METH_NOARGS },
5785+ {"test_refcount_macros" , test_refcount_macros , METH_NOARGS },
5786+ {"test_refcount_funcs" , test_refcount_funcs , METH_NOARGS },
5787+ {"test_py_is_macros" , test_py_is_macros , METH_NOARGS },
5788+ {"test_py_is_funcs" , test_py_is_funcs , METH_NOARGS },
57205789 {"fatal_error" , test_fatal_error , METH_VARARGS ,
57215790 PyDoc_STR ("fatal_error(message, release_gil=False): call Py_FatalError(message)" )},
57225791 {NULL , NULL } /* sentinel */
0 commit comments