[3.12] GH-108390: Prevent non-local events being set with sys.monitoring.set_local_events() (GH-108420) (#108899)

* GH-108390: Prevent non-local events being set with `sys.monitoring.set_local_events()` (GH-108420)

* Restore generated objects

* Restore size of monitoring arrays in code object for 3.12 ABI compatibility.

* Update ABI file
This commit is contained in:
Mark Shannon 2023-09-05 12:29:38 +01:00 committed by GitHub
parent 5121faabd1
commit 7ee021f999
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 3101 additions and 3001 deletions

View file

@ -1218,9 +1218,11 @@ class TestInstallIncrementallly(MonitoringTestBase, unittest.TestCase):
self.check_events(self.func2,
recorders = recorders, must_include = self.MUST_INCLUDE_CI)
LOCAL_RECORDERS = CallRecorder, LineRecorder, CReturnRecorder, CRaiseRecorder
class TestLocalEvents(MonitoringTestBase, unittest.TestCase):
def check_events(self, func, expected, tool=TEST_TOOL, recorders=(ExceptionRecorder,)):
def check_events(self, func, expected, tool=TEST_TOOL, recorders=()):
try:
self.assertEqual(sys.monitoring._all_events(), {})
event_list = []
@ -1248,7 +1250,7 @@ class TestLocalEvents(MonitoringTestBase, unittest.TestCase):
line2 = 2
line3 = 3
self.check_events(func1, recorders = MANY_RECORDERS, expected = [
self.check_events(func1, recorders = LOCAL_RECORDERS, expected = [
('line', 'func1', 1),
('line', 'func1', 2),
('line', 'func1', 3)])
@ -1260,7 +1262,7 @@ class TestLocalEvents(MonitoringTestBase, unittest.TestCase):
[].append(2)
line3 = 3
self.check_events(func2, recorders = MANY_RECORDERS, expected = [
self.check_events(func2, recorders = LOCAL_RECORDERS, expected = [
('line', 'func2', 1),
('line', 'func2', 2),
('call', 'append', [2]),
@ -1277,15 +1279,17 @@ class TestLocalEvents(MonitoringTestBase, unittest.TestCase):
line = 5
line = 6
self.check_events(func3, recorders = MANY_RECORDERS, expected = [
self.check_events(func3, recorders = LOCAL_RECORDERS, expected = [
('line', 'func3', 1),
('line', 'func3', 2),
('line', 'func3', 3),
('raise', KeyError),
('line', 'func3', 4),
('line', 'func3', 5),
('line', 'func3', 6)])
def test_set_non_local_event(self):
with self.assertRaises(ValueError):
sys.monitoring.set_local_events(TEST_TOOL, just_call.__code__, E.RAISE)
def line_from_offset(code, offset):
for start, end, line in code.co_lines():
@ -1698,3 +1702,19 @@ class TestRegressions(MonitoringTestBase, unittest.TestCase):
self.assertEqual(caught, "inner")
finally:
sys.monitoring.set_events(TEST_TOOL, 0)
def test_108390(self):
class Foo:
def __init__(self, set_event):
if set_event:
sys.monitoring.set_events(TEST_TOOL, E.PY_RESUME)
def make_foo_optimized_then_set_event():
for i in range(100):
Foo(i == 99)
try:
make_foo_optimized_then_set_event()
finally:
sys.monitoring.set_events(TEST_TOOL, 0)