1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
|
#include "parts.h"
#define Py_BUILD_CORE
#include "pycore_function.h" // FUNC_MAX_WATCHERS
#include "pycore_code.h" // CODE_MAX_WATCHERS
// Test dict watching
static PyObject *g_dict_watch_events;
static int g_dict_watchers_installed;
static int
dict_watch_callback(PyDict_WatchEvent event,
PyObject *dict,
PyObject *key,
PyObject *new_value)
{
PyObject *msg;
switch (event) {
case PyDict_EVENT_CLEARED:
msg = PyUnicode_FromString("clear");
break;
case PyDict_EVENT_DEALLOCATED:
msg = PyUnicode_FromString("dealloc");
break;
case PyDict_EVENT_CLONED:
msg = PyUnicode_FromString("clone");
break;
case PyDict_EVENT_ADDED:
msg = PyUnicode_FromFormat("new:%S:%S", key, new_value);
break;
case PyDict_EVENT_MODIFIED:
msg = PyUnicode_FromFormat("mod:%S:%S", key, new_value);
break;
case PyDict_EVENT_DELETED:
msg = PyUnicode_FromFormat("del:%S", key);
break;
default:
msg = PyUnicode_FromString("unknown");
}
if (msg == NULL) {
return -1;
}
assert(PyList_Check(g_dict_watch_events));
if (PyList_Append(g_dict_watch_events, msg) < 0) {
Py_DECREF(msg);
return -1;
}
Py_DECREF(msg);
return 0;
}
static int
dict_watch_callback_second(PyDict_WatchEvent event,
PyObject *dict,
PyObject *key,
PyObject *new_value)
{
PyObject *msg = PyUnicode_FromString("second");
if (msg == NULL) {
return -1;
}
int rc = PyList_Append(g_dict_watch_events, msg);
Py_DECREF(msg);
if (rc < 0) {
return -1;
}
return 0;
}
static int
dict_watch_callback_error(PyDict_WatchEvent event,
PyObject *dict,
PyObject *key,
PyObject *new_value)
{
PyErr_SetString(PyExc_RuntimeError, "boom!");
return -1;
}
static PyObject *
add_dict_watcher(PyObject *self, PyObject *kind)
{
int watcher_id;
assert(PyLong_Check(kind));
long kind_l = PyLong_AsLong(kind);
if (kind_l == 2) {
watcher_id = PyDict_AddWatcher(dict_watch_callback_second);
}
else if (kind_l == 1) {
watcher_id = PyDict_AddWatcher(dict_watch_callback_error);
}
else {
watcher_id = PyDict_AddWatcher(dict_watch_callback);
}
if (watcher_id < 0) {
return NULL;
}
if (!g_dict_watchers_installed) {
assert(!g_dict_watch_events);
if (!(g_dict_watch_events = PyList_New(0))) {
return NULL;
}
}
g_dict_watchers_installed++;
return PyLong_FromLong(watcher_id);
}
static PyObject *
clear_dict_watcher(PyObject *self, PyObject *watcher_id)
{
if (PyDict_ClearWatcher(PyLong_AsLong(watcher_id))) {
return NULL;
}
g_dict_watchers_installed--;
if (!g_dict_watchers_installed) {
assert(g_dict_watch_events);
Py_CLEAR(g_dict_watch_events);
}
Py_RETURN_NONE;
}
static PyObject *
watch_dict(PyObject *self, PyObject *args)
{
PyObject *dict;
int watcher_id;
if (!PyArg_ParseTuple(args, "iO", &watcher_id, &dict)) {
return NULL;
}
if (PyDict_Watch(watcher_id, dict)) {
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
unwatch_dict(PyObject *self, PyObject *args)
{
PyObject *dict;
int watcher_id;
if (!PyArg_ParseTuple(args, "iO", &watcher_id, &dict)) {
return NULL;
}
if (PyDict_Unwatch(watcher_id, dict)) {
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
get_dict_watcher_events(PyObject *self, PyObject *Py_UNUSED(args))
{
if (!g_dict_watch_events) {
PyErr_SetString(PyExc_RuntimeError, "no watchers active");
return NULL;
}
return Py_NewRef(g_dict_watch_events);
}
// Test type watchers
static PyObject *g_type_modified_events;
static int g_type_watchers_installed;
static int
type_modified_callback(PyTypeObject *type)
{
assert(PyList_Check(g_type_modified_events));
if(PyList_Append(g_type_modified_events, (PyObject *)type) < 0) {
return -1;
}
return 0;
}
static int
type_modified_callback_wrap(PyTypeObject *type)
{
assert(PyList_Check(g_type_modified_events));
PyObject *list = PyList_New(0);
if (list == NULL) {
return -1;
}
if (PyList_Append(list, (PyObject *)type) < 0) {
Py_DECREF(list);
return -1;
}
if (PyList_Append(g_type_modified_events, list) < 0) {
Py_DECREF(list);
return -1;
}
Py_DECREF(list);
return 0;
}
static int
type_modified_callback_error(PyTypeObject *type)
{
PyErr_SetString(PyExc_RuntimeError, "boom!");
return -1;
}
static PyObject *
add_type_watcher(PyObject *self, PyObject *kind)
{
int watcher_id;
assert(PyLong_Check(kind));
long kind_l = PyLong_AsLong(kind);
if (kind_l == 2) {
watcher_id = PyType_AddWatcher(type_modified_callback_wrap);
}
else if (kind_l == 1) {
watcher_id = PyType_AddWatcher(type_modified_callback_error);
}
else {
watcher_id = PyType_AddWatcher(type_modified_callback);
}
if (watcher_id < 0) {
return NULL;
}
if (!g_type_watchers_installed) {
assert(!g_type_modified_events);
if (!(g_type_modified_events = PyList_New(0))) {
return NULL;
}
}
g_type_watchers_installed++;
return PyLong_FromLong(watcher_id);
}
static PyObject *
clear_type_watcher(PyObject *self, PyObject *watcher_id)
{
if (PyType_ClearWatcher(PyLong_AsLong(watcher_id))) {
return NULL;
}
g_type_watchers_installed--;
if (!g_type_watchers_installed) {
assert(g_type_modified_events);
Py_CLEAR(g_type_modified_events);
}
Py_RETURN_NONE;
}
static PyObject *
get_type_modified_events(PyObject *self, PyObject *Py_UNUSED(args))
{
if (!g_type_modified_events) {
PyErr_SetString(PyExc_RuntimeError, "no watchers active");
return NULL;
}
return Py_NewRef(g_type_modified_events);
}
static PyObject *
watch_type(PyObject *self, PyObject *args)
{
PyObject *type;
int watcher_id;
if (!PyArg_ParseTuple(args, "iO", &watcher_id, &type)) {
return NULL;
}
if (PyType_Watch(watcher_id, type)) {
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
unwatch_type(PyObject *self, PyObject *args)
{
PyObject *type;
int watcher_id;
if (!PyArg_ParseTuple(args, "iO", &watcher_id, &type)) {
return NULL;
}
if (PyType_Unwatch(watcher_id, type)) {
return NULL;
}
Py_RETURN_NONE;
}
// Test code object watching
#define NUM_CODE_WATCHERS 2
static int num_code_object_created_events[NUM_CODE_WATCHERS] = {0, 0};
static int num_code_object_destroyed_events[NUM_CODE_WATCHERS] = {0, 0};
static int
handle_code_object_event(int which_watcher, PyCodeEvent event, PyCodeObject *co) {
if (event == PY_CODE_EVENT_CREATE) {
num_code_object_created_events[which_watcher]++;
}
else if (event == PY_CODE_EVENT_DESTROY) {
num_code_object_destroyed_events[which_watcher]++;
}
else {
return -1;
}
return 0;
}
static int
first_code_object_callback(PyCodeEvent event, PyCodeObject *co)
{
return handle_code_object_event(0, event, co);
}
static int
second_code_object_callback(PyCodeEvent event, PyCodeObject *co)
{
return handle_code_object_event(1, event, co);
}
static int
noop_code_event_handler(PyCodeEvent event, PyCodeObject *co)
{
return 0;
}
static int
error_code_event_handler(PyCodeEvent event, PyCodeObject *co)
{
PyErr_SetString(PyExc_RuntimeError, "boom!");
return -1;
}
static PyObject *
add_code_watcher(PyObject *self, PyObject *which_watcher)
{
int watcher_id;
assert(PyLong_Check(which_watcher));
long which_l = PyLong_AsLong(which_watcher);
if (which_l == 0) {
watcher_id = PyCode_AddWatcher(first_code_object_callback);
num_code_object_created_events[0] = 0;
num_code_object_destroyed_events[0] = 0;
}
else if (which_l == 1) {
watcher_id = PyCode_AddWatcher(second_code_object_callback);
num_code_object_created_events[1] = 0;
num_code_object_destroyed_events[1] = 0;
}
else if (which_l == 2) {
watcher_id = PyCode_AddWatcher(error_code_event_handler);
}
else {
PyErr_Format(PyExc_ValueError, "invalid watcher %d", which_l);
return NULL;
}
if (watcher_id < 0) {
return NULL;
}
return PyLong_FromLong(watcher_id);
}
static PyObject *
clear_code_watcher(PyObject *self, PyObject *watcher_id)
{
assert(PyLong_Check(watcher_id));
long watcher_id_l = PyLong_AsLong(watcher_id);
if (PyCode_ClearWatcher(watcher_id_l) < 0) {
return NULL;
}
// reset static events counters
if (watcher_id_l >= 0 && watcher_id_l < NUM_CODE_WATCHERS) {
num_code_object_created_events[watcher_id_l] = 0;
num_code_object_destroyed_events[watcher_id_l] = 0;
}
Py_RETURN_NONE;
}
static PyObject *
get_code_watcher_num_created_events(PyObject *self, PyObject *watcher_id)
{
assert(PyLong_Check(watcher_id));
long watcher_id_l = PyLong_AsLong(watcher_id);
assert(watcher_id_l >= 0 && watcher_id_l < NUM_CODE_WATCHERS);
return PyLong_FromLong(num_code_object_created_events[watcher_id_l]);
}
static PyObject *
get_code_watcher_num_destroyed_events(PyObject *self, PyObject *watcher_id)
{
assert(PyLong_Check(watcher_id));
long watcher_id_l = PyLong_AsLong(watcher_id);
assert(watcher_id_l >= 0 && watcher_id_l < NUM_CODE_WATCHERS);
return PyLong_FromLong(num_code_object_destroyed_events[watcher_id_l]);
}
static PyObject *
allocate_too_many_code_watchers(PyObject *self, PyObject *args)
{
int watcher_ids[CODE_MAX_WATCHERS + 1];
int num_watchers = 0;
for (unsigned long i = 0; i < sizeof(watcher_ids) / sizeof(int); i++) {
int watcher_id = PyCode_AddWatcher(noop_code_event_handler);
if (watcher_id == -1) {
break;
}
watcher_ids[i] = watcher_id;
num_watchers++;
}
PyObject *exc = PyErr_GetRaisedException();
for (int i = 0; i < num_watchers; i++) {
if (PyCode_ClearWatcher(watcher_ids[i]) < 0) {
PyErr_WriteUnraisable(Py_None);
break;
}
}
if (exc) {
PyErr_SetRaisedException(exc);
return NULL;
}
else if (PyErr_Occurred()) {
return NULL;
}
Py_RETURN_NONE;
}
// Test function watchers
#define NUM_FUNC_WATCHERS 2
static PyObject *pyfunc_watchers[NUM_FUNC_WATCHERS];
static int func_watcher_ids[NUM_FUNC_WATCHERS] = {-1, -1};
static PyObject *
get_id(PyObject *obj)
{
PyObject *builtins = PyEval_GetBuiltins(); // borrowed ref.
if (builtins == NULL) {
return NULL;
}
PyObject *id_str = PyUnicode_FromString("id");
if (id_str == NULL) {
return NULL;
}
PyObject *id_func = PyObject_GetItem(builtins, id_str);
Py_DECREF(id_str);
if (id_func == NULL) {
return NULL;
}
PyObject *stack[] = {obj};
PyObject *id = PyObject_Vectorcall(id_func, stack, 1, NULL);
Py_DECREF(id_func);
return id;
}
static int
call_pyfunc_watcher(PyObject *watcher, PyFunction_WatchEvent event,
PyFunctionObject *func, PyObject *new_value)
{
PyObject *event_obj = PyLong_FromLong(event);
if (event_obj == NULL) {
return -1;
}
if (new_value == NULL) {
new_value = Py_None;
}
Py_INCREF(new_value);
PyObject *func_or_id = NULL;
if (event == PyFunction_EVENT_DESTROY) {
/* Don't expose a function that's about to be destroyed to managed code */
func_or_id = get_id((PyObject *) func);
if (func_or_id == NULL) {
Py_DECREF(event_obj);
Py_DECREF(new_value);
return -1;
}
}
else {
Py_INCREF(func);
func_or_id = (PyObject *) func;
}
PyObject *stack[] = {event_obj, func_or_id, new_value};
PyObject *res = PyObject_Vectorcall(watcher, stack, 3, NULL);
int st = (res == NULL) ? -1 : 0;
Py_XDECREF(res);
Py_DECREF(new_value);
Py_DECREF(event_obj);
Py_DECREF(func_or_id);
return st;
}
static int
first_func_watcher_callback(PyFunction_WatchEvent event, PyFunctionObject *func,
PyObject *new_value)
{
return call_pyfunc_watcher(pyfunc_watchers[0], event, func, new_value);
}
static int
second_func_watcher_callback(PyFunction_WatchEvent event,
PyFunctionObject *func, PyObject *new_value)
{
return call_pyfunc_watcher(pyfunc_watchers[1], event, func, new_value);
}
static PyFunction_WatchCallback func_watcher_callbacks[NUM_FUNC_WATCHERS] = {
first_func_watcher_callback,
second_func_watcher_callback
};
static int
add_func_event(PyObject *module, const char *name, PyFunction_WatchEvent event)
{
PyObject *value = PyLong_FromLong(event);
if (value == NULL) {
return -1;
}
int ok = PyModule_AddObjectRef(module, name, value);
Py_DECREF(value);
return ok;
}
static PyObject *
add_func_watcher(PyObject *self, PyObject *func)
{
if (!PyFunction_Check(func)) {
PyErr_SetString(PyExc_TypeError, "'func' must be a function");
return NULL;
}
int idx = -1;
for (int i = 0; i < NUM_FUNC_WATCHERS; i++) {
if (func_watcher_ids[i] == -1) {
idx = i;
break;
}
}
if (idx == -1) {
PyErr_SetString(PyExc_RuntimeError, "no free watchers");
return NULL;
}
PyObject *result = PyLong_FromLong(idx);
if (result == NULL) {
return NULL;
}
func_watcher_ids[idx] = PyFunction_AddWatcher(func_watcher_callbacks[idx]);
if (func_watcher_ids[idx] < 0) {
Py_DECREF(result);
return NULL;
}
pyfunc_watchers[idx] = Py_NewRef(func);
return result;
}
static PyObject *
clear_func_watcher(PyObject *self, PyObject *watcher_id_obj)
{
long watcher_id = PyLong_AsLong(watcher_id_obj);
if ((watcher_id < INT_MIN) || (watcher_id > INT_MAX)) {
PyErr_SetString(PyExc_ValueError, "invalid watcher ID");
return NULL;
}
int wid = (int) watcher_id;
if (PyFunction_ClearWatcher(wid) < 0) {
return NULL;
}
int idx = -1;
for (int i = 0; i < NUM_FUNC_WATCHERS; i++) {
if (func_watcher_ids[i] == wid) {
idx = i;
break;
}
}
assert(idx != -1);
Py_CLEAR(pyfunc_watchers[idx]);
func_watcher_ids[idx] = -1;
Py_RETURN_NONE;
}
static int
noop_func_event_handler(PyFunction_WatchEvent event, PyFunctionObject *func,
PyObject *new_value)
{
return 0;
}
static PyObject *
allocate_too_many_func_watchers(PyObject *self, PyObject *args)
{
int watcher_ids[FUNC_MAX_WATCHERS + 1];
int num_watchers = 0;
for (unsigned long i = 0; i < sizeof(watcher_ids) / sizeof(int); i++) {
int watcher_id = PyFunction_AddWatcher(noop_func_event_handler);
if (watcher_id == -1) {
break;
}
watcher_ids[i] = watcher_id;
num_watchers++;
}
PyObject *exc = PyErr_GetRaisedException();
for (int i = 0; i < num_watchers; i++) {
if (PyFunction_ClearWatcher(watcher_ids[i]) < 0) {
PyErr_WriteUnraisable(Py_None);
break;
}
}
if (exc) {
PyErr_SetRaisedException(exc);
return NULL;
}
else if (PyErr_Occurred()) {
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
set_func_defaults(PyObject *self, PyObject *args)
{
PyObject *func = NULL;
PyObject *defaults = NULL;
if (!PyArg_ParseTuple(args, "OO", &func, &defaults)) {
return NULL;
}
if (PyFunction_SetDefaults(func, defaults) < 0) {
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
set_func_kwdefaults(PyObject *self, PyObject *args)
{
PyObject *func = NULL;
PyObject *kwdefaults = NULL;
if (!PyArg_ParseTuple(args, "OO", &func, &kwdefaults)) {
return NULL;
}
if (PyFunction_SetKwDefaults(func, kwdefaults) < 0) {
return NULL;
}
Py_RETURN_NONE;
}
static PyMethodDef test_methods[] = {
// Dict watchers.
{"add_dict_watcher", add_dict_watcher, METH_O, NULL},
{"clear_dict_watcher", clear_dict_watcher, METH_O, NULL},
{"watch_dict", watch_dict, METH_VARARGS, NULL},
{"unwatch_dict", unwatch_dict, METH_VARARGS, NULL},
{"get_dict_watcher_events",
(PyCFunction) get_dict_watcher_events, METH_NOARGS, NULL},
// Type watchers.
{"add_type_watcher", add_type_watcher, METH_O, NULL},
{"clear_type_watcher", clear_type_watcher, METH_O, NULL},
{"watch_type", watch_type, METH_VARARGS, NULL},
{"unwatch_type", unwatch_type, METH_VARARGS, NULL},
{"get_type_modified_events",
(PyCFunction) get_type_modified_events, METH_NOARGS, NULL},
// Code object watchers.
{"add_code_watcher", add_code_watcher, METH_O, NULL},
{"clear_code_watcher", clear_code_watcher, METH_O, NULL},
{"get_code_watcher_num_created_events",
get_code_watcher_num_created_events, METH_O, NULL},
{"get_code_watcher_num_destroyed_events",
get_code_watcher_num_destroyed_events, METH_O, NULL},
{"allocate_too_many_code_watchers",
(PyCFunction) allocate_too_many_code_watchers, METH_NOARGS, NULL},
// Function watchers.
{"add_func_watcher", add_func_watcher, METH_O, NULL},
{"clear_func_watcher", clear_func_watcher, METH_O, NULL},
{"set_func_defaults_via_capi", set_func_defaults, METH_VARARGS, NULL},
{"set_func_kwdefaults_via_capi", set_func_kwdefaults, METH_VARARGS, NULL},
{"allocate_too_many_func_watchers", allocate_too_many_func_watchers,
METH_NOARGS, NULL},
{NULL},
};
int
_PyTestCapi_Init_Watchers(PyObject *mod)
{
if (PyModule_AddFunctions(mod, test_methods) < 0) {
return -1;
}
/* Expose each event as an attribute on the module */
#define ADD_EVENT(event) \
if (add_func_event(mod, "PYFUNC_EVENT_" #event, \
PyFunction_EVENT_##event)) { \
return -1; \
}
PY_FOREACH_FUNC_EVENT(ADD_EVENT);
#undef ADD_EVENT
return 0;
}
|