@@ -5138,6 +5138,91 @@ test_set_type_size(PyObject *self, PyObject *Py_UNUSED(ignored))
5138
5138
}
5139
5139
5140
5140
5141
+ // Test Py_CLEAR() macro
5142
+ static PyObject *
5143
+ test_py_clear (PyObject * self , PyObject * Py_UNUSED (ignored ))
5144
+ {
5145
+ // simple case with a variable
5146
+ PyObject * obj = PyList_New (0 );
5147
+ if (obj == NULL ) {
5148
+ return NULL ;
5149
+ }
5150
+ Py_CLEAR (obj );
5151
+ assert (obj == NULL );
5152
+
5153
+ // gh-98724: complex case, Py_CLEAR() argument has a side effect
5154
+ PyObject * array [1 ];
5155
+ array [0 ] = PyList_New (0 );
5156
+ if (array [0 ] == NULL ) {
5157
+ return NULL ;
5158
+ }
5159
+
5160
+ PyObject * * p = array ;
5161
+ Py_CLEAR (* p ++ );
5162
+ assert (array [0 ] == NULL );
5163
+ assert (p == array + 1 );
5164
+
5165
+ Py_RETURN_NONE ;
5166
+ }
5167
+
5168
+
5169
+ // Test Py_SETREF() and Py_XSETREF() macros, similar to test_py_clear()
5170
+ static PyObject *
5171
+ test_py_setref (PyObject * self , PyObject * Py_UNUSED (ignored ))
5172
+ {
5173
+ // Py_SETREF() simple case with a variable
5174
+ PyObject * obj = PyList_New (0 );
5175
+ if (obj == NULL ) {
5176
+ return NULL ;
5177
+ }
5178
+ Py_SETREF (obj , NULL );
5179
+ assert (obj == NULL );
5180
+
5181
+ // Py_XSETREF() simple case with a variable
5182
+ PyObject * obj2 = PyList_New (0 );
5183
+ if (obj2 == NULL ) {
5184
+ return NULL ;
5185
+ }
5186
+ Py_XSETREF (obj2 , NULL );
5187
+ assert (obj2 == NULL );
5188
+ // test Py_XSETREF() when the argument is NULL
5189
+ Py_XSETREF (obj2 , NULL );
5190
+ assert (obj2 == NULL );
5191
+
5192
+ // gh-98724: complex case, Py_SETREF() argument has a side effect
5193
+ PyObject * array [1 ];
5194
+ array [0 ] = PyList_New (0 );
5195
+ if (array [0 ] == NULL ) {
5196
+ return NULL ;
5197
+ }
5198
+
5199
+ PyObject * * p = array ;
5200
+ Py_SETREF (* p ++ , NULL );
5201
+ assert (array [0 ] == NULL );
5202
+ assert (p == array + 1 );
5203
+
5204
+ // gh-98724: complex case, Py_XSETREF() argument has a side effect
5205
+ PyObject * array2 [1 ];
5206
+ array2 [0 ] = PyList_New (0 );
5207
+ if (array2 [0 ] == NULL ) {
5208
+ return NULL ;
5209
+ }
5210
+
5211
+ PyObject * * p2 = array2 ;
5212
+ Py_XSETREF (* p2 ++ , NULL );
5213
+ assert (array2 [0 ] == NULL );
5214
+ assert (p2 == array2 + 1 );
5215
+
5216
+ // test Py_XSETREF() when the argument is NULL
5217
+ p2 = array2 ;
5218
+ Py_XSETREF (* p2 ++ , NULL );
5219
+ assert (array2 [0 ] == NULL );
5220
+ assert (p2 == array2 + 1 );
5221
+
5222
+ Py_RETURN_NONE ;
5223
+ }
5224
+
5225
+
5141
5226
#define TEST_REFCOUNT () \
5142
5227
do { \
5143
5228
PyObject *obj = PyList_New(0); \
@@ -6311,6 +6396,8 @@ static PyMethodDef TestMethods[] = {
6311
6396
{"pynumber_tobase" , pynumber_tobase , METH_VARARGS },
6312
6397
{"without_gc" , without_gc , METH_O },
6313
6398
{"test_set_type_size" , test_set_type_size , METH_NOARGS },
6399
+ {"test_py_clear" , test_py_clear , METH_NOARGS },
6400
+ {"test_py_setref" , test_py_setref , METH_NOARGS },
6314
6401
{"test_refcount_macros" , test_refcount_macros , METH_NOARGS },
6315
6402
{"test_refcount_funcs" , test_refcount_funcs , METH_NOARGS },
6316
6403
{"test_py_is_macros" , test_py_is_macros , METH_NOARGS },
0 commit comments