@@ -5243,6 +5243,21 @@ try_watch(int watcher_id, PyObject *obj) {
5243
5243
return 0 ;
5244
5244
}
5245
5245
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
+
5246
5261
static PyObject *
5247
5262
test_watch_dict (PyObject * self , PyObject * Py_UNUSED (args ))
5248
5263
{
@@ -5321,23 +5336,30 @@ test_watch_dict(PyObject *self, PyObject *Py_UNUSED(args))
5321
5336
Py_CLEAR (copy );
5322
5337
5323
5338
Py_CLEAR (watched );
5324
- Py_CLEAR (unwatched );
5325
5339
5326
5340
if (dict_watch_assert (9 , "dealloc" )) {
5327
5341
return NULL ;
5328
5342
}
5329
5343
5330
5344
// 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" )) {
5333
5346
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" )) {
5336
5359
return NULL ;
5337
- } else {
5338
- PyErr_Clear ();
5339
5360
}
5340
5361
5362
+ Py_CLEAR (unwatched );
5341
5363
Py_CLEAR (g_dict_watch_events );
5342
5364
Py_DECREF (one );
5343
5365
Py_DECREF (two );
0 commit comments