gh-110892: Return NULL for PyTrace_RETURN events caused by an exception (GH-110909)

This commit is contained in:
Tian Gao 2023-11-02 09:38:08 -07:00 committed by GitHub
parent 0887b9ce8b
commit f4b5588bde
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 23 deletions

View file

@ -30,9 +30,9 @@ class HookWatcher:
if (event == "call"
or event == "return"
or event == "exception"):
self.add_event(event, frame)
self.add_event(event, frame, arg)
def add_event(self, event, frame=None):
def add_event(self, event, frame=None, arg=None):
"""Add an event to the log."""
if frame is None:
frame = sys._getframe(1)
@ -43,7 +43,7 @@ class HookWatcher:
frameno = len(self.frames)
self.frames.append(frame)
self.events.append((frameno, event, ident(frame)))
self.events.append((frameno, event, ident(frame), arg))
def get_events(self):
"""Remove calls to add_event()."""
@ -89,11 +89,16 @@ class ProfileSimulator(HookWatcher):
class TestCaseBase(unittest.TestCase):
def check_events(self, callable, expected):
def check_events(self, callable, expected, check_args=False):
events = capture_events(callable, self.new_watcher())
if events != expected:
self.fail("Expected events:\n%s\nReceived events:\n%s"
% (pprint.pformat(expected), pprint.pformat(events)))
if check_args:
if events != expected:
self.fail("Expected events:\n%s\nReceived events:\n%s"
% (pprint.pformat(expected), pprint.pformat(events)))
else:
if [(frameno, event, ident) for frameno, event, ident, arg in events] != expected:
self.fail("Expected events:\n%s\nReceived events:\n%s"
% (pprint.pformat(expected), pprint.pformat(events)))
class ProfileHookTestCase(TestCaseBase):
@ -264,11 +269,11 @@ class ProfileHookTestCase(TestCaseBase):
f_ident = ident(f)
g_ident = ident(g)
self.check_events(g, [(1, 'call', g_ident),
(2, 'call', f_ident),
(2, 'return', f_ident),
(1, 'return', g_ident),
])
self.check_events(g, [(1, 'call', g_ident, None),
(2, 'call', f_ident, None),
(2, 'return', f_ident, 0),
(1, 'return', g_ident, None),
], check_args=True)
def test_stop_iteration(self):
def f():