@@ -3395,57 +3395,73 @@ test_weakref_capi(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
33953395 Py_DECREF (obj );
33963396 return NULL ;
33973397 }
3398+
3399+ // test PyWeakref_Check(), valid weakref object
33983400 assert (PyWeakref_Check (weakref ));
33993401 assert (PyWeakref_CheckRefExact (weakref ));
34003402 assert (PyWeakref_CheckRefExact (weakref ));
34013403 assert (Py_REFCNT (obj ) == refcnt );
34023404
34033405 // test PyWeakref_GetRef(), reference is alive
3404- PyObject * ref1 ;
3405- assert (PyWeakref_GetRef (weakref , & ref1 ) == 0 );
3406- assert (ref1 == obj );
3406+ PyObject * ref = Py_True ; // marker to check that value was set
3407+ assert (PyWeakref_GetRef (weakref , & ref ) == 0 );
3408+ assert (ref == obj );
34073409 assert (Py_REFCNT (obj ) == (refcnt + 1 ));
3408- Py_DECREF (ref1 );
3410+ Py_DECREF (ref );
34093411
34103412 // test PyWeakref_GetObject(), reference is alive
3411- PyObject * ref2 = PyWeakref_GetObject (weakref );
3412- assert (ref2 == obj );
3413+ ref = PyWeakref_GetObject (weakref ); // borrowed ref
3414+ assert (ref == obj );
34133415
34143416 // test PyWeakref_GET_OBJECT(), reference is alive
3415- PyObject * ref3 = PyWeakref_GET_OBJECT (weakref );
3416- assert (ref3 == obj );
3417+ ref = PyWeakref_GET_OBJECT (weakref ); // borrowed ref
3418+ assert (ref == obj );
34173419
3418- // delete the referenced object
3420+ // delete the referenced object: clear the weakref
34193421 assert (Py_REFCNT (obj ) == 1 );
34203422 Py_DECREF (obj );
34213423
34223424 // test PyWeakref_GET_OBJECT(), reference is dead
34233425 assert (PyWeakref_GET_OBJECT (weakref ) == Py_None );
34243426
34253427 // test PyWeakref_GetRef(), reference is dead
3426- PyObject * ref4 = Py_True ; // marker to check that value was set
3427- assert (PyWeakref_GetRef (weakref , & ref4 ) == 0 );
3428- assert (ref4 == NULL );
3428+ ref = Py_True ;
3429+ assert (PyWeakref_GetRef (weakref , & ref ) == 0 );
3430+ assert (ref == NULL );
34293431
3430- // None is not a weak reference object
3432+ // test PyWeakref_Check(), not a weakref object
34313433 PyObject * invalid_weakref = Py_None ;
34323434 assert (!PyWeakref_Check (invalid_weakref ));
34333435 assert (!PyWeakref_CheckRefExact (invalid_weakref ));
34343436 assert (!PyWeakref_CheckRefExact (invalid_weakref ));
34353437
34363438 // test PyWeakref_GetRef(), invalid type
34373439 assert (!PyErr_Occurred ());
3438- PyObject * ref5 = Py_True ; // marker to check that value was set
3439- assert (PyWeakref_GetRef (invalid_weakref , & ref5 ) == -1 );
3440+ ref = Py_True ;
3441+ assert (PyWeakref_GetRef (invalid_weakref , & ref ) == -1 );
34403442 assert (PyErr_ExceptionMatches (PyExc_TypeError ));
34413443 PyErr_Clear ();
3442- assert (ref5 == NULL );
3444+ assert (ref == NULL );
34433445
34443446 // test PyWeakref_GetObject(), invalid type
34453447 assert (PyWeakref_GetObject (invalid_weakref ) == NULL );
34463448 assert (PyErr_ExceptionMatches (PyExc_SystemError ));
34473449 PyErr_Clear ();
34483450
3451+ // test PyWeakref_GetRef(NULL)
3452+ ref = Py_True ; // marker to check that value was set
3453+ assert (PyWeakref_GetRef (NULL , & ref ) == -1 );
3454+ assert (PyErr_ExceptionMatches (PyExc_SystemError ));
3455+ assert (ref == NULL );
3456+ PyErr_Clear ();
3457+
3458+ // test PyWeakref_GetObject(NULL)
3459+ assert (PyWeakref_GetObject (NULL ) == NULL );
3460+ assert (PyErr_ExceptionMatches (PyExc_SystemError ));
3461+ PyErr_Clear ();
3462+
3463+ Py_DECREF (weakref );
3464+
34493465 Py_RETURN_NONE ;
34503466}
34513467
0 commit comments