@@ -5243,6 +5243,21 @@ try_watch(int watcher_id, PyObject *obj) {
52435243 return 0 ;
52445244}
52455245
5246+ static int
5247+ dict_watch_assert_error (int watcher_id , PyObject * obj , const char * fail_msg )
5248+ {
5249+ if (!PyDict_Watch (watcher_id , obj )) {
5250+ raiseTestError ("test_watch_dict" , fail_msg );
5251+ return -1 ;
5252+ } else if (!PyErr_Occurred ()) {
5253+ raiseTestError ("test_watch_dict" , "PyDict_Watch() returned error code without exception set" );
5254+ return -1 ;
5255+ } else {
5256+ PyErr_Clear ();
5257+ }
5258+ return 0 ;
5259+ }
5260+
52465261static PyObject *
52475262test_watch_dict (PyObject * self , PyObject * Py_UNUSED (args ))
52485263{
@@ -5321,23 +5336,30 @@ test_watch_dict(PyObject *self, PyObject *Py_UNUSED(args))
53215336 Py_CLEAR (copy );
53225337
53235338 Py_CLEAR (watched );
5324- Py_CLEAR (unwatched );
53255339
53265340 if (dict_watch_assert (9 , "dealloc" )) {
53275341 return NULL ;
53285342 }
53295343
53305344 // it is an error to try to watch a non-dict
5331- if (!PyDict_Watch (wid , one )) {
5332- raiseTestError ("test_watch_dict" , "PyDict_Watch() succeeded on non-dict" );
5345+ if (dict_watch_assert_error (wid , one , "PyDict_Watch() succeeded on non-dict" )) {
53335346 return NULL ;
5334- } else if (!PyErr_Occurred ()) {
5335- raiseTestError ("test_watch_dict" , "PyDict_Watch() returned error code without exception set" );
5347+ }
5348+
5349+ // It is an error to pass an out-of-range watcher ID
5350+ if (dict_watch_assert_error (-1 , unwatched , "PyDict_Watch() succeeded on negative watcher ID" )) {
5351+ return NULL ;
5352+ }
5353+ if (dict_watch_assert_error (8 , unwatched , "PyDict_Watch() succeeded on too-large watcher ID" )) {
5354+ return NULL ;
5355+ }
5356+
5357+ // It is an error to pass a never-registered watcher ID
5358+ if (dict_watch_assert_error (7 , unwatched , "PyDict_Watch() succeeded on unused watcher ID" )) {
53365359 return NULL ;
5337- } else {
5338- PyErr_Clear ();
53395360 }
53405361
5362+ Py_CLEAR (unwatched );
53415363 Py_CLEAR (g_dict_watch_events );
53425364 Py_DECREF (one );
53435365 Py_DECREF (two );
0 commit comments