| 1 |
// clinic/watchers.c.h uses internal pycore_modsupport.h API |
| 2 |
#define PYTESTCAPI_NEED_INTERNAL_API |
| 3 |
|
| 4 |
#include "parts.h" |
| 5 |
|
| 6 |
#include "clinic/watchers.c.h" |
| 7 |
|
| 8 |
#define Py_BUILD_CORE |
| 9 |
#include "pycore_function.h" // FUNC_MAX_WATCHERS |
| 10 |
#include "pycore_interp_structs.h" // CODE_MAX_WATCHERS |
| 11 |
#include "pycore_context.h" // CONTEXT_MAX_WATCHERS |
| 12 |
|
| 13 |
/*[clinic input] |
| 14 |
module _testcapi |
| 15 |
[clinic start generated code]*/ |
| 16 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6361033e795369fc]*/ |
| 17 |
|
| 18 |
// Test dict watching |
| 19 |
static PyObject *g_dict_watch_events = NULL; |
| 20 |
static int g_dict_watchers_installed = 0; |
| 21 |
|
| 22 |
static int |
| 23 |
dict_watch_callback(PyDict_WatchEvent event, |
| 24 |
PyObject *dict, |
| 25 |
PyObject *key, |
| 26 |
PyObject *new_value) |
| 27 |
{ |
| 28 |
PyObject *msg; |
| 29 |
switch (event) { |
| 30 |
case PyDict_EVENT_CLEARED: |
| 31 |
msg = PyUnicode_FromString("clear"); |
| 32 |
break; |
| 33 |
case PyDict_EVENT_DEALLOCATED: |
| 34 |
msg = PyUnicode_FromString("dealloc"); |
| 35 |
break; |
| 36 |
case PyDict_EVENT_CLONED: |
| 37 |
msg = PyUnicode_FromString("clone"); |
| 38 |
break; |
| 39 |
case PyDict_EVENT_ADDED: |
| 40 |
msg = PyUnicode_FromFormat("new:%S:%S", key, new_value); |
| 41 |
break; |
| 42 |
case PyDict_EVENT_MODIFIED: |
| 43 |
msg = PyUnicode_FromFormat("mod:%S:%S", key, new_value); |
| 44 |
break; |
| 45 |
case PyDict_EVENT_DELETED: |
| 46 |
msg = PyUnicode_FromFormat("del:%S", key); |
| 47 |
break; |
| 48 |
default: |
| 49 |
msg = PyUnicode_FromString("unknown"); |
| 50 |
} |
| 51 |
if (msg == NULL) { |
| 52 |
return -1; |
| 53 |
} |
| 54 |
assert(PyList_Check(g_dict_watch_events)); |
| 55 |
if (PyList_Append(g_dict_watch_events, msg) < 0) { |
| 56 |
Py_DECREF(msg); |
| 57 |
return -1; |
| 58 |
} |
| 59 |
Py_DECREF(msg); |
| 60 |
return 0; |
| 61 |
} |
| 62 |
|
| 63 |
static int |
| 64 |
dict_watch_callback_second(PyDict_WatchEvent event, |
| 65 |
PyObject *dict, |
| 66 |
PyObject *key, |
| 67 |
PyObject *new_value) |
| 68 |
{ |
| 69 |
PyObject *msg = PyUnicode_FromString("second"); |
| 70 |
if (msg == NULL) { |
| 71 |
return -1; |
| 72 |
} |
| 73 |
int rc = PyList_Append(g_dict_watch_events, msg); |
| 74 |
Py_DECREF(msg); |
| 75 |
if (rc < 0) { |
| 76 |
return -1; |
| 77 |
} |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
static int |
| 82 |
dict_watch_callback_error(PyDict_WatchEvent event, |
| 83 |
PyObject *dict, |
| 84 |
PyObject *key, |
| 85 |
PyObject *new_value) |
| 86 |
{ |
| 87 |
PyErr_SetString(PyExc_RuntimeError, "boom!"); |
| 88 |
return -1; |
| 89 |
} |
| 90 |
|
| 91 |
static PyObject * |
| 92 |
add_dict_watcher(PyObject *self, PyObject *kind) |
| 93 |
{ |
| 94 |
int watcher_id; |
| 95 |
assert(PyLong_Check(kind)); |
| 96 |
long kind_l = PyLong_AsLong(kind); |
| 97 |
if (kind_l == 2) { |
| 98 |
watcher_id = PyDict_AddWatcher(dict_watch_callback_second); |
| 99 |
} |
| 100 |
else if (kind_l == 1) { |
| 101 |
watcher_id = PyDict_AddWatcher(dict_watch_callback_error); |
| 102 |
} |
| 103 |
else { |
| 104 |
watcher_id = PyDict_AddWatcher(dict_watch_callback); |
| 105 |
} |
| 106 |
if (watcher_id < 0) { |
| 107 |
return NULL; |
| 108 |
} |
| 109 |
if (!g_dict_watchers_installed) { |
| 110 |
assert(!g_dict_watch_events); |
| 111 |
if (!(g_dict_watch_events = PyList_New(0))) { |
| 112 |
return NULL; |
| 113 |
} |
| 114 |
} |
| 115 |
g_dict_watchers_installed++; |
| 116 |
return PyLong_FromLong(watcher_id); |
| 117 |
} |
| 118 |
|
| 119 |
static PyObject * |
| 120 |
clear_dict_watcher(PyObject *self, PyObject *watcher_id) |
| 121 |
{ |
| 122 |
if (PyDict_ClearWatcher(PyLong_AsLong(watcher_id))) { |
| 123 |
return NULL; |
| 124 |
} |
| 125 |
g_dict_watchers_installed--; |
| 126 |
if (!g_dict_watchers_installed) { |
| 127 |
assert(g_dict_watch_events); |
| 128 |
Py_CLEAR(g_dict_watch_events); |
| 129 |
} |
| 130 |
Py_RETURN_NONE; |
| 131 |
} |
| 132 |
|
| 133 |
/*[clinic input] |
| 134 |
_testcapi.watch_dict |
| 135 |
watcher_id: int |
| 136 |
dict: object |
| 137 |
/ |
| 138 |
[clinic start generated code]*/ |
| 139 |
|
| 140 |
static PyObject * |
| 141 |
_testcapi_watch_dict_impl(PyObject *module, int watcher_id, PyObject *dict) |
| 142 |
/*[clinic end generated code: output=1426e0273cebe2d8 input=269b006d60c358bd]*/ |
| 143 |
{ |
| 144 |
if (PyDict_Watch(watcher_id, dict)) { |
| 145 |
return NULL; |
| 146 |
} |
| 147 |
Py_RETURN_NONE; |
| 148 |
} |
| 149 |
|
| 150 |
/*[clinic input] |
| 151 |
_testcapi.unwatch_dict = _testcapi.watch_dict |
| 152 |
[clinic start generated code]*/ |
| 153 |
|
| 154 |
static PyObject * |
| 155 |
_testcapi_unwatch_dict_impl(PyObject *module, int watcher_id, PyObject *dict) |
| 156 |
/*[clinic end generated code: output=512b1a71ae33c351 input=cae7dc1b6f7713b8]*/ |
| 157 |
{ |
| 158 |
if (PyDict_Unwatch(watcher_id, dict)) { |
| 159 |
return NULL; |
| 160 |
} |
| 161 |
Py_RETURN_NONE; |
| 162 |
} |
| 163 |
|
| 164 |
static PyObject * |
| 165 |
get_dict_watcher_events(PyObject *self, PyObject *Py_UNUSED(args)) |
| 166 |
{ |
| 167 |
if (!g_dict_watch_events) { |
| 168 |
PyErr_SetString(PyExc_RuntimeError, "no watchers active"); |
| 169 |
return NULL; |
| 170 |
} |
| 171 |
return Py_NewRef(g_dict_watch_events); |
| 172 |
} |
| 173 |
|
| 174 |
// Test type watchers |
| 175 |
static PyObject *g_type_modified_events; |
| 176 |
static int g_type_watchers_installed; |
| 177 |
|
| 178 |
static int |
| 179 |
type_modified_callback(PyTypeObject *type) |
| 180 |
{ |
| 181 |
assert(PyList_Check(g_type_modified_events)); |
| 182 |
if(PyList_Append(g_type_modified_events, (PyObject *)type) < 0) { |
| 183 |
return -1; |
| 184 |
} |
| 185 |
return 0; |
| 186 |
} |
| 187 |
|
| 188 |
static int |
| 189 |
type_modified_callback_wrap(PyTypeObject *type) |
| 190 |
{ |
| 191 |
assert(PyList_Check(g_type_modified_events)); |
| 192 |
PyObject *list = PyList_New(0); |
| 193 |
if (list == NULL) { |
| 194 |
return -1; |
| 195 |
} |
| 196 |
if (PyList_Append(list, (PyObject *)type) < 0) { |
| 197 |
Py_DECREF(list); |
| 198 |
return -1; |
| 199 |
} |
| 200 |
if (PyList_Append(g_type_modified_events, list) < 0) { |
| 201 |
Py_DECREF(list); |
| 202 |
return -1; |
| 203 |
} |
| 204 |
Py_DECREF(list); |
| 205 |
return 0; |
| 206 |
} |
| 207 |
|
| 208 |
static int |
| 209 |
type_modified_callback_error(PyTypeObject *type) |
| 210 |
{ |
| 211 |
PyErr_SetString(PyExc_RuntimeError, "boom!"); |
| 212 |
return -1; |
| 213 |
} |
| 214 |
|
| 215 |
static PyObject * |
| 216 |
add_type_watcher(PyObject *self, PyObject *kind) |
| 217 |
{ |
| 218 |
int watcher_id; |
| 219 |
assert(PyLong_Check(kind)); |
| 220 |
long kind_l = PyLong_AsLong(kind); |
| 221 |
if (kind_l == 2) { |
| 222 |
watcher_id = PyType_AddWatcher(type_modified_callback_wrap); |
| 223 |
} |
| 224 |
else if (kind_l == 1) { |
| 225 |
watcher_id = PyType_AddWatcher(type_modified_callback_error); |
| 226 |
} |
| 227 |
else { |
| 228 |
watcher_id = PyType_AddWatcher(type_modified_callback); |
| 229 |
} |
| 230 |
if (watcher_id < 0) { |
| 231 |
return NULL; |
| 232 |
} |
| 233 |
if (!g_type_watchers_installed) { |
| 234 |
assert(!g_type_modified_events); |
| 235 |
if (!(g_type_modified_events = PyList_New(0))) { |
| 236 |
return NULL; |
| 237 |
} |
| 238 |
} |
| 239 |
g_type_watchers_installed++; |
| 240 |
return PyLong_FromLong(watcher_id); |
| 241 |
} |
| 242 |
|
| 243 |
static PyObject * |
| 244 |
clear_type_watcher(PyObject *self, PyObject *watcher_id) |
| 245 |
{ |
| 246 |
if (PyType_ClearWatcher(PyLong_AsLong(watcher_id))) { |
| 247 |
return NULL; |
| 248 |
} |
| 249 |
g_type_watchers_installed--; |
| 250 |
if (!g_type_watchers_installed) { |
| 251 |
assert(g_type_modified_events); |
| 252 |
Py_CLEAR(g_type_modified_events); |
| 253 |
} |
| 254 |
Py_RETURN_NONE; |
| 255 |
} |
| 256 |
|
| 257 |
static PyObject * |
| 258 |
get_type_modified_events(PyObject *self, PyObject *Py_UNUSED(args)) |
| 259 |
{ |
| 260 |
if (!g_type_modified_events) { |
| 261 |
PyErr_SetString(PyExc_RuntimeError, "no watchers active"); |
| 262 |
return NULL; |
| 263 |
} |
| 264 |
return Py_NewRef(g_type_modified_events); |
| 265 |
} |
| 266 |
|
| 267 |
/*[clinic input] |
| 268 |
_testcapi.watch_type |
| 269 |
watcher_id: int |
| 270 |
type: object |
| 271 |
/ |
| 272 |
[clinic start generated code]*/ |
| 273 |
|
| 274 |
static PyObject * |
| 275 |
_testcapi_watch_type_impl(PyObject *module, int watcher_id, PyObject *type) |
| 276 |
/*[clinic end generated code: output=fdf4777126724fc4 input=5a808bf12be7e3ed]*/ |
| 277 |
{ |
| 278 |
if (PyType_Watch(watcher_id, type)) { |
| 279 |
return NULL; |
| 280 |
} |
| 281 |
Py_RETURN_NONE; |
| 282 |
} |
| 283 |
|
| 284 |
/*[clinic input] |
| 285 |
_testcapi.unwatch_type = _testcapi.watch_type |
| 286 |
[clinic start generated code]*/ |
| 287 |
|
| 288 |
static PyObject * |
| 289 |
_testcapi_unwatch_type_impl(PyObject *module, int watcher_id, PyObject *type) |
| 290 |
/*[clinic end generated code: output=0389672d4ad5f68b input=6701911fb45edc9e]*/ |
| 291 |
{ |
| 292 |
if (PyType_Unwatch(watcher_id, type)) { |
| 293 |
return NULL; |
| 294 |
} |
| 295 |
Py_RETURN_NONE; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
// Test code object watching |
| 300 |
|
| 301 |
#define NUM_CODE_WATCHERS 2 |
| 302 |
static int code_watcher_ids[NUM_CODE_WATCHERS] = {-1, -1}; |
| 303 |
static int num_code_object_created_events[NUM_CODE_WATCHERS] = {0, 0}; |
| 304 |
static int num_code_object_destroyed_events[NUM_CODE_WATCHERS] = {0, 0}; |
| 305 |
|
| 306 |
static int |
| 307 |
handle_code_object_event(int which_watcher, PyCodeEvent event, PyCodeObject *co) { |
| 308 |
if (event == PY_CODE_EVENT_CREATE) { |
| 309 |
num_code_object_created_events[which_watcher]++; |
| 310 |
} |
| 311 |
else if (event == PY_CODE_EVENT_DESTROY) { |
| 312 |
num_code_object_destroyed_events[which_watcher]++; |
| 313 |
} |
| 314 |
else { |
| 315 |
return -1; |
| 316 |
} |
| 317 |
return 0; |
| 318 |
} |
| 319 |
|
| 320 |
static int |
| 321 |
first_code_object_callback(PyCodeEvent event, PyCodeObject *co) |
| 322 |
{ |
| 323 |
return handle_code_object_event(0, event, co); |
| 324 |
} |
| 325 |
|
| 326 |
static int |
| 327 |
second_code_object_callback(PyCodeEvent event, PyCodeObject *co) |
| 328 |
{ |
| 329 |
return handle_code_object_event(1, event, co); |
| 330 |
} |
| 331 |
|
| 332 |
static int |
| 333 |
noop_code_event_handler(PyCodeEvent event, PyCodeObject *co) |
| 334 |
{ |
| 335 |
return 0; |
| 336 |
} |
| 337 |
|
| 338 |
static int |
| 339 |
error_code_event_handler(PyCodeEvent event, PyCodeObject *co) |
| 340 |
{ |
| 341 |
PyErr_SetString(PyExc_RuntimeError, "boom!"); |
| 342 |
return -1; |
| 343 |
} |
| 344 |
|
| 345 |
static PyObject * |
| 346 |
add_code_watcher(PyObject *self, PyObject *which_watcher) |
| 347 |
{ |
| 348 |
int watcher_id; |
| 349 |
assert(PyLong_Check(which_watcher)); |
| 350 |
long which_l = PyLong_AsLong(which_watcher); |
| 351 |
if (which_l == 0) { |
| 352 |
watcher_id = PyCode_AddWatcher(first_code_object_callback); |
| 353 |
code_watcher_ids[0] = watcher_id; |
| 354 |
num_code_object_created_events[0] = 0; |
| 355 |
num_code_object_destroyed_events[0] = 0; |
| 356 |
} |
| 357 |
else if (which_l == 1) { |
| 358 |
watcher_id = PyCode_AddWatcher(second_code_object_callback); |
| 359 |
code_watcher_ids[1] = watcher_id; |
| 360 |
num_code_object_created_events[1] = 0; |
| 361 |
num_code_object_destroyed_events[1] = 0; |
| 362 |
} |
| 363 |
else if (which_l == 2) { |
| 364 |
watcher_id = PyCode_AddWatcher(error_code_event_handler); |
| 365 |
} |
| 366 |
else { |
| 367 |
PyErr_Format(PyExc_ValueError, "invalid watcher %ld", which_l); |
| 368 |
return NULL; |
| 369 |
} |
| 370 |
if (watcher_id < 0) { |
| 371 |
return NULL; |
| 372 |
} |
| 373 |
return PyLong_FromLong(watcher_id); |
| 374 |
} |
| 375 |
|
| 376 |
static PyObject * |
| 377 |
clear_code_watcher(PyObject *self, PyObject *watcher_id) |
| 378 |
{ |
| 379 |
assert(PyLong_Check(watcher_id)); |
| 380 |
long watcher_id_l = PyLong_AsLong(watcher_id); |
| 381 |
if (PyCode_ClearWatcher(watcher_id_l) < 0) { |
| 382 |
return NULL; |
| 383 |
} |
| 384 |
// reset static events counters |
| 385 |
if (watcher_id_l >= 0) { |
| 386 |
for (int i = 0; i < NUM_CODE_WATCHERS; i++) { |
| 387 |
if (watcher_id_l == code_watcher_ids[i]) { |
| 388 |
code_watcher_ids[i] = -1; |
| 389 |
num_code_object_created_events[i] = 0; |
| 390 |
num_code_object_destroyed_events[i] = 0; |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
Py_RETURN_NONE; |
| 395 |
} |
| 396 |
|
| 397 |
static PyObject * |
| 398 |
get_code_watcher_num_created_events(PyObject *self, PyObject *watcher_id) |
| 399 |
{ |
| 400 |
assert(PyLong_Check(watcher_id)); |
| 401 |
long watcher_id_l = PyLong_AsLong(watcher_id); |
| 402 |
assert(watcher_id_l >= 0 && watcher_id_l < NUM_CODE_WATCHERS); |
| 403 |
return PyLong_FromLong(num_code_object_created_events[watcher_id_l]); |
| 404 |
} |
| 405 |
|
| 406 |
static PyObject * |
| 407 |
get_code_watcher_num_destroyed_events(PyObject *self, PyObject *watcher_id) |
| 408 |
{ |
| 409 |
assert(PyLong_Check(watcher_id)); |
| 410 |
long watcher_id_l = PyLong_AsLong(watcher_id); |
| 411 |
assert(watcher_id_l >= 0 && watcher_id_l < NUM_CODE_WATCHERS); |
| 412 |
return PyLong_FromLong(num_code_object_destroyed_events[watcher_id_l]); |
| 413 |
} |
| 414 |
|
| 415 |
static PyObject * |
| 416 |
allocate_too_many_code_watchers(PyObject *self, PyObject *Py_UNUSED(args)) |
| 417 |
{ |
| 418 |
int watcher_ids[CODE_MAX_WATCHERS + 1]; |
| 419 |
int num_watchers = 0; |
| 420 |
for (unsigned long i = 0; i < sizeof(watcher_ids) / sizeof(int); i++) { |
| 421 |
int watcher_id = PyCode_AddWatcher(noop_code_event_handler); |
| 422 |
if (watcher_id == -1) { |
| 423 |
break; |
| 424 |
} |
| 425 |
watcher_ids[i] = watcher_id; |
| 426 |
num_watchers++; |
| 427 |
} |
| 428 |
PyObject *exc = PyErr_GetRaisedException(); |
| 429 |
for (int i = 0; i < num_watchers; i++) { |
| 430 |
if (PyCode_ClearWatcher(watcher_ids[i]) < 0) { |
| 431 |
PyErr_FormatUnraisable("Exception ignored while " |
| 432 |
"clearing code watcher"); |
| 433 |
break; |
| 434 |
} |
| 435 |
} |
| 436 |
if (exc) { |
| 437 |
PyErr_SetRaisedException(exc); |
| 438 |
return NULL; |
| 439 |
} |
| 440 |
else if (PyErr_Occurred()) { |
| 441 |
return NULL; |
| 442 |
} |
| 443 |
Py_RETURN_NONE; |
| 444 |
} |
| 445 |
|
| 446 |
// Test function watchers |
| 447 |
|
| 448 |
#define NUM_TEST_FUNC_WATCHERS 2 |
| 449 |
static PyObject *pyfunc_watchers[NUM_TEST_FUNC_WATCHERS]; |
| 450 |
static int func_watcher_ids[NUM_TEST_FUNC_WATCHERS] = {-1, -1}; |
| 451 |
|
| 452 |
static PyObject * |
| 453 |
get_id(PyObject *obj) |
| 454 |
{ |
| 455 |
PyObject *builtins = PyEval_GetBuiltins(); // borrowed ref. |
| 456 |
if (builtins == NULL) { |
| 457 |
return NULL; |
| 458 |
} |
| 459 |
PyObject *id_str = PyUnicode_FromString("id"); |
| 460 |
if (id_str == NULL) { |
| 461 |
return NULL; |
| 462 |
} |
| 463 |
PyObject *id_func = PyObject_GetItem(builtins, id_str); |
| 464 |
Py_DECREF(id_str); |
| 465 |
if (id_func == NULL) { |
| 466 |
return NULL; |
| 467 |
} |
| 468 |
PyObject *stack[] = {obj}; |
| 469 |
PyObject *id = PyObject_Vectorcall(id_func, stack, 1, NULL); |
| 470 |
Py_DECREF(id_func); |
| 471 |
return id; |
| 472 |
} |
| 473 |
|
| 474 |
static int |
| 475 |
call_pyfunc_watcher(PyObject *watcher, PyFunction_WatchEvent event, |
| 476 |
PyFunctionObject *func, PyObject *new_value) |
| 477 |
{ |
| 478 |
PyObject *event_obj = PyLong_FromLong(event); |
| 479 |
if (event_obj == NULL) { |
| 480 |
return -1; |
| 481 |
} |
| 482 |
if (new_value == NULL) { |
| 483 |
new_value = Py_None; |
| 484 |
} |
| 485 |
Py_INCREF(new_value); |
| 486 |
PyObject *func_or_id = NULL; |
| 487 |
if (event == PyFunction_EVENT_DESTROY) { |
| 488 |
/* Don't expose a function that's about to be destroyed to managed code */ |
| 489 |
func_or_id = get_id((PyObject *) func); |
| 490 |
if (func_or_id == NULL) { |
| 491 |
Py_DECREF(event_obj); |
| 492 |
Py_DECREF(new_value); |
| 493 |
return -1; |
| 494 |
} |
| 495 |
} |
| 496 |
else { |
| 497 |
Py_INCREF(func); |
| 498 |
func_or_id = (PyObject *) func; |
| 499 |
} |
| 500 |
PyObject *stack[] = {event_obj, func_or_id, new_value}; |
| 501 |
PyObject *res = PyObject_Vectorcall(watcher, stack, 3, NULL); |
| 502 |
int st = (res == NULL) ? -1 : 0; |
| 503 |
Py_XDECREF(res); |
| 504 |
Py_DECREF(new_value); |
| 505 |
Py_DECREF(event_obj); |
| 506 |
Py_DECREF(func_or_id); |
| 507 |
return st; |
| 508 |
} |
| 509 |
|
| 510 |
static int |
| 511 |
first_func_watcher_callback(PyFunction_WatchEvent event, PyFunctionObject *func, |
| 512 |
PyObject *new_value) |
| 513 |
{ |
| 514 |
return call_pyfunc_watcher(pyfunc_watchers[0], event, func, new_value); |
| 515 |
} |
| 516 |
|
| 517 |
static int |
| 518 |
second_func_watcher_callback(PyFunction_WatchEvent event, |
| 519 |
PyFunctionObject *func, PyObject *new_value) |
| 520 |
{ |
| 521 |
return call_pyfunc_watcher(pyfunc_watchers[1], event, func, new_value); |
| 522 |
} |
| 523 |
|
| 524 |
static PyFunction_WatchCallback func_watcher_callbacks[NUM_TEST_FUNC_WATCHERS] = { |
| 525 |
first_func_watcher_callback, |
| 526 |
second_func_watcher_callback |
| 527 |
}; |
| 528 |
|
| 529 |
static int |
| 530 |
add_func_event(PyObject *module, const char *name, PyFunction_WatchEvent event) |
| 531 |
{ |
| 532 |
return PyModule_Add(module, name, PyLong_FromLong(event)); |
| 533 |
} |
| 534 |
|
| 535 |
static PyObject * |
| 536 |
add_func_watcher(PyObject *self, PyObject *func) |
| 537 |
{ |
| 538 |
if (!PyFunction_Check(func)) { |
| 539 |
PyErr_SetString(PyExc_TypeError, "'func' must be a function"); |
| 540 |
return NULL; |
| 541 |
} |
| 542 |
int idx = -1; |
| 543 |
for (int i = 0; i < NUM_TEST_FUNC_WATCHERS; i++) { |
| 544 |
if (func_watcher_ids[i] == -1) { |
| 545 |
idx = i; |
| 546 |
break; |
| 547 |
} |
| 548 |
} |
| 549 |
if (idx == -1) { |
| 550 |
PyErr_SetString(PyExc_RuntimeError, "no free test watchers"); |
| 551 |
return NULL; |
| 552 |
} |
| 553 |
func_watcher_ids[idx] = PyFunction_AddWatcher(func_watcher_callbacks[idx]); |
| 554 |
if (func_watcher_ids[idx] < 0) { |
| 555 |
return NULL; |
| 556 |
} |
| 557 |
pyfunc_watchers[idx] = Py_NewRef(func); |
| 558 |
PyObject *result = PyLong_FromLong(func_watcher_ids[idx]); |
| 559 |
if (result == NULL) { |
| 560 |
return NULL; |
| 561 |
} |
| 562 |
return result; |
| 563 |
} |
| 564 |
|
| 565 |
static PyObject * |
| 566 |
clear_func_watcher(PyObject *self, PyObject *watcher_id_obj) |
| 567 |
{ |
| 568 |
long watcher_id = PyLong_AsLong(watcher_id_obj); |
| 569 |
if ((watcher_id < INT_MIN) || (watcher_id > INT_MAX)) { |
| 570 |
PyErr_SetString(PyExc_ValueError, "invalid watcher ID"); |
| 571 |
return NULL; |
| 572 |
} |
| 573 |
int wid = (int) watcher_id; |
| 574 |
if (PyFunction_ClearWatcher(wid) < 0) { |
| 575 |
return NULL; |
| 576 |
} |
| 577 |
int idx = -1; |
| 578 |
for (int i = 0; i < NUM_TEST_FUNC_WATCHERS; i++) { |
| 579 |
if (func_watcher_ids[i] == wid) { |
| 580 |
idx = i; |
| 581 |
break; |
| 582 |
} |
| 583 |
} |
| 584 |
assert(idx != -1); |
| 585 |
Py_CLEAR(pyfunc_watchers[idx]); |
| 586 |
func_watcher_ids[idx] = -1; |
| 587 |
Py_RETURN_NONE; |
| 588 |
} |
| 589 |
|
| 590 |
static int |
| 591 |
noop_func_event_handler(PyFunction_WatchEvent event, PyFunctionObject *func, |
| 592 |
PyObject *new_value) |
| 593 |
{ |
| 594 |
return 0; |
| 595 |
} |
| 596 |
|
| 597 |
static PyObject * |
| 598 |
allocate_too_many_func_watchers(PyObject *self, PyObject *args) |
| 599 |
{ |
| 600 |
int watcher_ids[FUNC_MAX_WATCHERS + 1]; |
| 601 |
int num_watchers = 0; |
| 602 |
for (unsigned long i = 0; i < sizeof(watcher_ids) / sizeof(int); i++) { |
| 603 |
int watcher_id = PyFunction_AddWatcher(noop_func_event_handler); |
| 604 |
if (watcher_id == -1) { |
| 605 |
break; |
| 606 |
} |
| 607 |
watcher_ids[i] = watcher_id; |
| 608 |
num_watchers++; |
| 609 |
} |
| 610 |
PyObject *exc = PyErr_GetRaisedException(); |
| 611 |
for (int i = 0; i < num_watchers; i++) { |
| 612 |
if (PyFunction_ClearWatcher(watcher_ids[i]) < 0) { |
| 613 |
PyErr_FormatUnraisable("Exception ignored while " |
| 614 |
"clearing function watcher"); |
| 615 |
break; |
| 616 |
} |
| 617 |
} |
| 618 |
if (exc) { |
| 619 |
PyErr_SetRaisedException(exc); |
| 620 |
return NULL; |
| 621 |
} |
| 622 |
else if (PyErr_Occurred()) { |
| 623 |
return NULL; |
| 624 |
} |
| 625 |
Py_RETURN_NONE; |
| 626 |
} |
| 627 |
|
| 628 |
// Test contexct object watchers |
| 629 |
#define NUM_CONTEXT_WATCHERS 2 |
| 630 |
static int context_watcher_ids[NUM_CONTEXT_WATCHERS] = {-1, -1}; |
| 631 |
static PyObject *context_switches[NUM_CONTEXT_WATCHERS]; |
| 632 |
|
| 633 |
static int |
| 634 |
handle_context_watcher_event(int which_watcher, PyContextEvent event, PyObject *ctx) { |
| 635 |
if (event == Py_CONTEXT_SWITCHED) { |
| 636 |
PyList_Append(context_switches[which_watcher], ctx); |
| 637 |
} |
| 638 |
else { |
| 639 |
return -1; |
| 640 |
} |
| 641 |
return 0; |
| 642 |
} |
| 643 |
|
| 644 |
static int |
| 645 |
first_context_watcher_callback(PyContextEvent event, PyObject *ctx) { |
| 646 |
return handle_context_watcher_event(0, event, ctx); |
| 647 |
} |
| 648 |
|
| 649 |
static int |
| 650 |
second_context_watcher_callback(PyContextEvent event, PyObject *ctx) { |
| 651 |
return handle_context_watcher_event(1, event, ctx); |
| 652 |
} |
| 653 |
|
| 654 |
static int |
| 655 |
noop_context_event_handler(PyContextEvent event, PyObject *ctx) { |
| 656 |
return 0; |
| 657 |
} |
| 658 |
|
| 659 |
static int |
| 660 |
error_context_event_handler(PyContextEvent event, PyObject *ctx) { |
| 661 |
PyErr_SetString(PyExc_RuntimeError, "boom!"); |
| 662 |
return -1; |
| 663 |
} |
| 664 |
|
| 665 |
static PyObject * |
| 666 |
add_context_watcher(PyObject *self, PyObject *which_watcher) |
| 667 |
{ |
| 668 |
static const PyContext_WatchCallback callbacks[] = { |
| 669 |
&first_context_watcher_callback, |
| 670 |
&second_context_watcher_callback, |
| 671 |
&error_context_event_handler, |
| 672 |
}; |
| 673 |
assert(PyLong_Check(which_watcher)); |
| 674 |
long which_l = PyLong_AsLong(which_watcher); |
| 675 |
if (which_l < 0 || which_l >= (long)Py_ARRAY_LENGTH(callbacks)) { |
| 676 |
PyErr_Format(PyExc_ValueError, "invalid watcher %ld", which_l); |
| 677 |
return NULL; |
| 678 |
} |
| 679 |
int watcher_id = PyContext_AddWatcher(callbacks[which_l]); |
| 680 |
if (watcher_id < 0) { |
| 681 |
return NULL; |
| 682 |
} |
| 683 |
if (which_l >= 0 && which_l < NUM_CONTEXT_WATCHERS) { |
| 684 |
context_watcher_ids[which_l] = watcher_id; |
| 685 |
Py_XSETREF(context_switches[which_l], PyList_New(0)); |
| 686 |
if (context_switches[which_l] == NULL) { |
| 687 |
return NULL; |
| 688 |
} |
| 689 |
} |
| 690 |
return PyLong_FromLong(watcher_id); |
| 691 |
} |
| 692 |
|
| 693 |
static PyObject * |
| 694 |
clear_context_watcher(PyObject *self, PyObject *watcher_id) |
| 695 |
{ |
| 696 |
assert(PyLong_Check(watcher_id)); |
| 697 |
long watcher_id_l = PyLong_AsLong(watcher_id); |
| 698 |
if (PyContext_ClearWatcher(watcher_id_l) < 0) { |
| 699 |
return NULL; |
| 700 |
} |
| 701 |
// reset static events counters |
| 702 |
if (watcher_id_l >= 0) { |
| 703 |
for (int i = 0; i < NUM_CONTEXT_WATCHERS; i++) { |
| 704 |
if (watcher_id_l == context_watcher_ids[i]) { |
| 705 |
context_watcher_ids[i] = -1; |
| 706 |
Py_CLEAR(context_switches[i]); |
| 707 |
} |
| 708 |
} |
| 709 |
} |
| 710 |
Py_RETURN_NONE; |
| 711 |
} |
| 712 |
|
| 713 |
static PyObject * |
| 714 |
clear_context_stack(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) |
| 715 |
{ |
| 716 |
PyThreadState *tstate = PyThreadState_Get(); |
| 717 |
if (tstate->context == NULL) { |
| 718 |
Py_RETURN_NONE; |
| 719 |
} |
| 720 |
if (((PyContext *)tstate->context)->ctx_prev != NULL) { |
| 721 |
PyErr_SetString(PyExc_RuntimeError, |
| 722 |
"must first exit all non-base contexts"); |
| 723 |
return NULL; |
| 724 |
} |
| 725 |
Py_CLEAR(tstate->context); |
| 726 |
Py_RETURN_NONE; |
| 727 |
} |
| 728 |
|
| 729 |
static PyObject * |
| 730 |
get_context_switches(PyObject *Py_UNUSED(self), PyObject *watcher_id) |
| 731 |
{ |
| 732 |
assert(PyLong_Check(watcher_id)); |
| 733 |
long watcher_id_l = PyLong_AsLong(watcher_id); |
| 734 |
if (watcher_id_l < 0 || watcher_id_l >= NUM_CONTEXT_WATCHERS) { |
| 735 |
PyErr_Format(PyExc_ValueError, "invalid watcher %ld", watcher_id_l); |
| 736 |
return NULL; |
| 737 |
} |
| 738 |
if (context_switches[watcher_id_l] == NULL) { |
| 739 |
return PyList_New(0); |
| 740 |
} |
| 741 |
return Py_NewRef(context_switches[watcher_id_l]); |
| 742 |
} |
| 743 |
|
| 744 |
static PyObject * |
| 745 |
allocate_too_many_context_watchers(PyObject *self, PyObject *Py_UNUSED(args)) |
| 746 |
{ |
| 747 |
int watcher_ids[CONTEXT_MAX_WATCHERS + 1]; |
| 748 |
int num_watchers = 0; |
| 749 |
for (unsigned long i = 0; i < sizeof(watcher_ids) / sizeof(int); i++) { |
| 750 |
int watcher_id = PyContext_AddWatcher(noop_context_event_handler); |
| 751 |
if (watcher_id == -1) { |
| 752 |
break; |
| 753 |
} |
| 754 |
watcher_ids[i] = watcher_id; |
| 755 |
num_watchers++; |
| 756 |
} |
| 757 |
PyObject *exc = PyErr_GetRaisedException(); |
| 758 |
for (int i = 0; i < num_watchers; i++) { |
| 759 |
if (PyContext_ClearWatcher(watcher_ids[i]) < 0) { |
| 760 |
PyErr_FormatUnraisable("Exception ignored while " |
| 761 |
"clearing context watcher"); |
| 762 |
break; |
| 763 |
} |
| 764 |
} |
| 765 |
if (exc) { |
| 766 |
PyErr_SetRaisedException(exc); |
| 767 |
return NULL; |
| 768 |
} |
| 769 |
else if (PyErr_Occurred()) { |
| 770 |
return NULL; |
| 771 |
} |
| 772 |
Py_RETURN_NONE; |
| 773 |
} |
| 774 |
|
| 775 |
/*[clinic input] |
| 776 |
_testcapi.set_func_defaults_via_capi |
| 777 |
func: object |
| 778 |
defaults: object |
| 779 |
/ |
| 780 |
[clinic start generated code]*/ |
| 781 |
|
| 782 |
static PyObject * |
| 783 |
_testcapi_set_func_defaults_via_capi_impl(PyObject *module, PyObject *func, |
| 784 |
PyObject *defaults) |
| 785 |
/*[clinic end generated code: output=caf0cb39db31ac24 input=e04a8508ca9d42fc]*/ |
| 786 |
{ |
| 787 |
if (PyFunction_SetDefaults(func, defaults) < 0) { |
| 788 |
return NULL; |
| 789 |
} |
| 790 |
Py_RETURN_NONE; |
| 791 |
} |
| 792 |
|
| 793 |
/*[clinic input] |
| 794 |
_testcapi.set_func_kwdefaults_via_capi = _testcapi.set_func_defaults_via_capi |
| 795 |
[clinic start generated code]*/ |
| 796 |
|
| 797 |
static PyObject * |
| 798 |
_testcapi_set_func_kwdefaults_via_capi_impl(PyObject *module, PyObject *func, |
| 799 |
PyObject *defaults) |
| 800 |
/*[clinic end generated code: output=9ed3b08177025070 input=f3cd1ca3c18de8ce]*/ |
| 801 |
{ |
| 802 |
if (PyFunction_SetKwDefaults(func, defaults) < 0) { |
| 803 |
return NULL; |
| 804 |
} |
| 805 |
Py_RETURN_NONE; |
| 806 |
} |
| 807 |
|
| 808 |
static PyMethodDef test_methods[] = { |
| 809 |
// Dict watchers. |
| 810 |
{"add_dict_watcher", add_dict_watcher, METH_O, NULL}, |
| 811 |
{"clear_dict_watcher", clear_dict_watcher, METH_O, NULL}, |
| 812 |
_TESTCAPI_WATCH_DICT_METHODDEF |
| 813 |
_TESTCAPI_UNWATCH_DICT_METHODDEF |
| 814 |
{"get_dict_watcher_events", get_dict_watcher_events, METH_NOARGS, NULL}, |
| 815 |
|
| 816 |
// Type watchers. |
| 817 |
{"add_type_watcher", add_type_watcher, METH_O, NULL}, |
| 818 |
{"clear_type_watcher", clear_type_watcher, METH_O, NULL}, |
| 819 |
_TESTCAPI_WATCH_TYPE_METHODDEF |
| 820 |
_TESTCAPI_UNWATCH_TYPE_METHODDEF |
| 821 |
{"get_type_modified_events", |
| 822 |
get_type_modified_events, METH_NOARGS, NULL}, |
| 823 |
|
| 824 |
// Code object watchers. |
| 825 |
{"add_code_watcher", add_code_watcher, METH_O, NULL}, |
| 826 |
{"clear_code_watcher", clear_code_watcher, METH_O, NULL}, |
| 827 |
{"get_code_watcher_num_created_events", |
| 828 |
get_code_watcher_num_created_events, METH_O, NULL}, |
| 829 |
{"get_code_watcher_num_destroyed_events", |
| 830 |
get_code_watcher_num_destroyed_events, METH_O, NULL}, |
| 831 |
{"allocate_too_many_code_watchers", |
| 832 |
allocate_too_many_code_watchers, METH_NOARGS, NULL}, |
| 833 |
|
| 834 |
// Function watchers. |
| 835 |
{"add_func_watcher", add_func_watcher, METH_O, NULL}, |
| 836 |
{"clear_func_watcher", clear_func_watcher, METH_O, NULL}, |
| 837 |
_TESTCAPI_SET_FUNC_DEFAULTS_VIA_CAPI_METHODDEF |
| 838 |
_TESTCAPI_SET_FUNC_KWDEFAULTS_VIA_CAPI_METHODDEF |
| 839 |
{"allocate_too_many_func_watchers", allocate_too_many_func_watchers, |
| 840 |
METH_NOARGS, NULL}, |
| 841 |
|
| 842 |
// Code object watchers. |
| 843 |
{"add_context_watcher", add_context_watcher, METH_O, NULL}, |
| 844 |
{"clear_context_watcher", clear_context_watcher, METH_O, NULL}, |
| 845 |
{"clear_context_stack", clear_context_stack, METH_NOARGS, NULL}, |
| 846 |
{"get_context_switches", get_context_switches, METH_O, NULL}, |
| 847 |
{"allocate_too_many_context_watchers", |
| 848 |
allocate_too_many_context_watchers, METH_NOARGS, NULL}, |
| 849 |
{NULL}, |
| 850 |
}; |
| 851 |
|
| 852 |
int |
| 853 |
_PyTestCapi_Init_Watchers(PyObject *mod) |
| 854 |
{ |
| 855 |
if (PyModule_AddFunctions(mod, test_methods) < 0) { |
| 856 |
return -1; |
| 857 |
} |
| 858 |
|
| 859 |
/* Expose each event as an attribute on the module */ |
| 860 |
#define ADD_EVENT(event) \ |
| 861 |
if (add_func_event(mod, "PYFUNC_EVENT_" #event, \ |
| 862 |
PyFunction_EVENT_##event)) { \ |
| 863 |
return -1; \ |
| 864 |
} |
| 865 |
PY_FOREACH_FUNC_EVENT(ADD_EVENT); |
| 866 |
#undef ADD_EVENT |
| 867 |
|
| 868 |
return 0; |
| 869 |
} |