Thanks to visit codestin.com
Credit goes to github.com

Skip to content

GH-117087: Initial implementation of support for 'code like' objects in sys.monitoring #131414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Include/internal/pycore_global_objects_fini_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(__fspath__)
STRUCT_FOR_ID(__ge__)
STRUCT_FOR_ID(__get__)
STRUCT_FOR_ID(__get_local_events__)
STRUCT_FOR_ID(__getattr__)
STRUCT_FOR_ID(__getattribute__)
STRUCT_FOR_ID(__getinitargs__)
Expand Down Expand Up @@ -203,6 +204,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(__rtruediv__)
STRUCT_FOR_ID(__rxor__)
STRUCT_FOR_ID(__set__)
STRUCT_FOR_ID(__set_local_events__)
STRUCT_FOR_ID(__set_name__)
STRUCT_FOR_ID(__setattr__)
STRUCT_FOR_ID(__setitem__)
Expand Down Expand Up @@ -409,6 +411,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(errors)
STRUCT_FOR_ID(event)
STRUCT_FOR_ID(eventmask)
STRUCT_FOR_ID(events)
STRUCT_FOR_ID(exc_type)
STRUCT_FOR_ID(exc_value)
STRUCT_FOR_ID(excepthook)
Expand Down Expand Up @@ -746,6 +749,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(times)
STRUCT_FOR_ID(timetuple)
STRUCT_FOR_ID(timeunit)
STRUCT_FOR_ID(tool)
STRUCT_FOR_ID(top)
STRUCT_FOR_ID(trace_callback)
STRUCT_FOR_ID(traceback)
Expand Down
4 changes: 2 additions & 2 deletions Include/internal/pycore_instruments.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ typedef uint32_t _PyMonitoringEventSet;
PyObject *_PyMonitoring_RegisterCallback(int tool_id, int event_id, PyObject *obj);

int _PyMonitoring_SetEvents(int tool_id, _PyMonitoringEventSet events);
int _PyMonitoring_SetLocalEvents(PyCodeObject *code, int tool_id, _PyMonitoringEventSet events);
int _PyMonitoring_GetLocalEvents(PyCodeObject *code, int tool_id, _PyMonitoringEventSet *events);
int _PyMonitoring_SetLocalEvents(PyObject *codelike, int tool_id, _PyMonitoringEventSet events);
int _PyMonitoring_GetLocalEvents(PyObject *codelike, int tool_id, _PyMonitoringEventSet *events);

extern int
_Py_call_instrumentation(PyThreadState *tstate, int event,
Expand Down
4 changes: 4 additions & 0 deletions Include/internal/pycore_runtime_init_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Include/internal/pycore_unicodeobject_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 28 additions & 2 deletions Lib/test/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1412,7 +1412,6 @@ def check_events(self, func, expected, tool=TEST_TOOL, recorders=()):
for recorder in recorders:
sys.monitoring.register_callback(tool, recorder.event_type, None)


def test_simple(self):

def func1():
Expand Down Expand Up @@ -1461,6 +1460,27 @@ def test_set_non_local_event(self):
with self.assertRaises(ValueError):
sys.monitoring.set_local_events(TEST_TOOL, just_call.__code__, E.RAISE)


def test_code_like(self):
class CodeLike:

def __init__(self):
self.events = [ 0 ] * 8

def __get_local_events__(self, tool):
return self.events[tool]

def __set_local_events__(self, tool, events):
self.events[tool] = events

codelike = CodeLike()
tool = TEST_TOOL
for events in ((E.LINE | E.PY_START), (E.BRANCH_LEFT | E.BRANCH_RIGHT), 0):
sys.monitoring.set_local_events(tool, codelike, events)
self.assertEqual(codelike.__get_local_events__(tool), events)
self.assertEqual(sys.monitoring.get_local_events(tool, codelike), events)


def line_from_offset(code, offset):
for start, end, line in code.co_lines():
if start <= offset < end:
Expand Down Expand Up @@ -2062,7 +2082,13 @@ def f():
pass

def test_get_local_events_uninitialized(self):
self.assertEqual(sys.monitoring.get_local_events(TEST_TOOL, self.f.__code__), 0)
with self.assertRaises(ValueError):
sys.monitoring.get_local_events(TEST_TOOL, self.f.__code__)
sys.monitoring.use_tool_id(TEST_TOOL, "test unitialized")
try:
self.assertEqual(sys.monitoring.get_local_events(TEST_TOOL, self.f.__code__), 0)
finally:
sys.monitoring.free_tool_id(TEST_TOOL)

class TestRegressions(MonitoringTestBase, unittest.TestCase):

Expand Down
128 changes: 127 additions & 1 deletion Objects/clinic/codeobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading