Skip to content

Commit da4179e

Browse files
Move API to public header and tests to _testcapi
1 parent 426a915 commit da4179e

File tree

5 files changed

+59
-62
lines changed

5 files changed

+59
-62
lines changed

Include/cpython/monitoring.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,20 @@ PyMonitoring_FireStopIterationEvent(PyMonitoringState *state, PyObject *codelike
274274

275275
#undef _PYMONITORING_IF_ACTIVE
276276

277+
278+
/* Callback API for notifications when sys.settrace/sys.setprofile are called. */
279+
typedef enum {
280+
PyUnstable_EVAL_TRACE_SET = 0,
281+
PyUnstable_EVAL_TRACE_CLEAR = 1,
282+
PyUnstable_EVAL_PROFILE_SET = 2,
283+
PyUnstable_EVAL_PROFILE_CLEAR = 3,
284+
} PyUnstable_EvalEvent;
285+
286+
typedef int (*PyUnstable_EvalCallback)(PyUnstable_EvalEvent event, void *data);
287+
288+
PyAPI_FUNC(int) PyUnstable_SetEvalCallback(PyUnstable_EvalCallback callback, void *data);
289+
PyAPI_FUNC(PyUnstable_EvalCallback) PyUnstable_GetEvalCallback(void **data);
290+
277291
#ifdef __cplusplus
278292
}
279293
#endif

Include/internal/pycore_instruments.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,6 @@ typedef struct _PyCoMonitoringData {
123123
} _PyCoMonitoringData;
124124

125125

126-
/* Callback API for notifications when sys.settrace/sys.setprofile are called. */
127-
typedef enum {
128-
PyUnstable_EVAL_TRACE_SET = 0,
129-
PyUnstable_EVAL_TRACE_CLEAR = 1,
130-
PyUnstable_EVAL_PROFILE_SET = 2,
131-
PyUnstable_EVAL_PROFILE_CLEAR = 3,
132-
} PyUnstable_EvalEvent;
133-
134-
typedef int (*PyUnstable_EvalCallback)(PyUnstable_EvalEvent event, void *data);
135-
136-
PyAPI_FUNC(int) PyUnstable_SetEvalCallback(PyUnstable_EvalCallback callback, void *data);
137-
PyAPI_FUNC(PyUnstable_EvalCallback) PyUnstable_GetEvalCallback(void **data);
138-
139126
#ifdef __cplusplus
140127
}
141128
#endif

Lib/test/test_capi/test_misc.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2869,10 +2869,10 @@ class TestEvalCallback(unittest.TestCase):
28692869

28702870
def setUp(self):
28712871
self.events = []
2872-
_testinternalcapi.set_eval_callback_record(self.events)
2872+
_testcapi.set_eval_callback_record(self.events)
28732873

28742874
def tearDown(self):
2875-
_testinternalcapi.clear_eval_callback()
2875+
_testcapi.clear_eval_callback()
28762876
sys.settrace(None)
28772877
sys.setprofile(None)
28782878

@@ -2923,10 +2923,10 @@ def dummy_profile(frame, event, arg):
29232923
])
29242924

29252925
def test_clear_callback_stops_events(self):
2926-
_testinternalcapi.clear_eval_callback()
2926+
_testcapi.clear_eval_callback()
29272927
events_after_clear = []
2928-
_testinternalcapi.set_eval_callback_record(events_after_clear)
2929-
_testinternalcapi.clear_eval_callback()
2928+
_testcapi.set_eval_callback_record(events_after_clear)
2929+
_testcapi.clear_eval_callback()
29302930

29312931
def dummy_trace(frame, event, arg):
29322932
return dummy_trace

Modules/_testcapi/monitoring.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#include "parts.h"
22
#include "util.h"
33

4-
#define Py_BUILD_CORE
5-
#include "internal/pycore_instruments.h"
4+
#include "cpython/monitoring.h"
65

76
typedef struct {
87
PyObject_HEAD
@@ -488,6 +487,43 @@ exit_scope(PyObject *self, PyObject *args)
488487
Py_RETURN_NONE;
489488
}
490489

490+
static int
491+
test_eval_callback(PyUnstable_EvalEvent event, void *data)
492+
{
493+
if (data == NULL) {
494+
return 0;
495+
}
496+
PyObject *event_int = PyLong_FromLong((long)event);
497+
if (event_int == NULL) {
498+
return -1;
499+
}
500+
int res = PyList_Append((PyObject *)data, event_int);
501+
Py_DECREF(event_int);
502+
return res;
503+
}
504+
505+
static PyObject *
506+
set_eval_callback_record(PyObject *self, PyObject *list)
507+
{
508+
if (!PyList_Check(list)) {
509+
PyErr_SetString(PyExc_TypeError, "argument must be a list");
510+
return NULL;
511+
}
512+
if (PyUnstable_SetEvalCallback(test_eval_callback, list) < 0) {
513+
return NULL;
514+
}
515+
Py_RETURN_NONE;
516+
}
517+
518+
static PyObject *
519+
clear_eval_callback(PyObject *self, PyObject *Py_UNUSED(args))
520+
{
521+
if (PyUnstable_SetEvalCallback(NULL, NULL) < 0) {
522+
return NULL;
523+
}
524+
Py_RETURN_NONE;
525+
}
526+
491527
static PyMethodDef TestMethods[] = {
492528
{"fire_event_py_start", fire_event_py_start, METH_VARARGS},
493529
{"fire_event_py_resume", fire_event_py_resume, METH_VARARGS},
@@ -508,6 +544,8 @@ static PyMethodDef TestMethods[] = {
508544
{"fire_event_stop_iteration", fire_event_stop_iteration, METH_VARARGS},
509545
{"monitoring_enter_scope", enter_scope, METH_VARARGS},
510546
{"monitoring_exit_scope", exit_scope, METH_VARARGS},
547+
{"set_eval_callback_record", set_eval_callback_record, METH_O},
548+
{"clear_eval_callback", clear_eval_callback, METH_NOARGS},
511549
{NULL},
512550
};
513551

Modules/_testinternalcapi.c

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
#include "pycore_pystate.h" // _PyThreadState_GET()
3939
#include "pycore_runtime_structs.h" // _PY_NSMALLPOSINTS
4040
#include "pycore_unicodeobject.h" // _PyUnicode_TransformDecimalAndSpaceToASCII()
41-
#include "pycore_instruments.h" // PyUnstable_SetEvalCallback
4241

4342
#include "clinic/_testinternalcapi.c.h"
4443

@@ -2837,50 +2836,9 @@ test_threadstate_set_stack_protection(PyObject *self, PyObject *Py_UNUSED(args))
28372836
}
28382837

28392838

2840-
// Helper for testing PyUnstable_SetEvalCallback / PyUnstable_GetEvalCallback
2841-
static int
2842-
test_eval_callback(PyUnstable_EvalEvent event, void *data)
2843-
{
2844-
if (data == NULL) {
2845-
return 0;
2846-
}
2847-
PyObject *event_int = PyLong_FromLong((long)event);
2848-
if (event_int == NULL) {
2849-
return -1;
2850-
}
2851-
int res = PyList_Append((PyObject *)data, event_int);
2852-
Py_DECREF(event_int);
2853-
return res;
2854-
}
2855-
2856-
static PyObject *
2857-
set_eval_callback_record(PyObject *self, PyObject *list)
2858-
{
2859-
if (!PyList_Check(list)) {
2860-
PyErr_SetString(PyExc_TypeError, "argument must be a list");
2861-
return NULL;
2862-
}
2863-
if (PyUnstable_SetEvalCallback(test_eval_callback, list) < 0) {
2864-
return NULL;
2865-
}
2866-
Py_RETURN_NONE;
2867-
}
2868-
2869-
static PyObject *
2870-
clear_eval_callback(PyObject *self, PyObject *Py_UNUSED(args))
2871-
{
2872-
if (PyUnstable_SetEvalCallback(NULL, NULL) < 0) {
2873-
return NULL;
2874-
}
2875-
Py_RETURN_NONE;
2876-
}
2877-
2878-
28792839
static PyMethodDef module_functions[] = {
28802840
{"get_configs", get_configs, METH_NOARGS},
28812841
{"get_eval_frame_stats", get_eval_frame_stats, METH_NOARGS, NULL},
2882-
{"set_eval_callback_record", set_eval_callback_record, METH_O, NULL},
2883-
{"clear_eval_callback", clear_eval_callback, METH_NOARGS, NULL},
28842842
{"get_recursion_depth", get_recursion_depth, METH_NOARGS},
28852843
{"get_c_recursion_remaining", get_c_recursion_remaining, METH_NOARGS},
28862844
{"get_stack_pointer", get_stack_pointer, METH_NOARGS},

0 commit comments

Comments
 (0)