Skip to content

Commit

Permalink
fix C raise event
Browse files Browse the repository at this point in the history
  • Loading branch information
carljm committed Apr 29, 2023
1 parent 82fa183 commit 9f1db4a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 15 deletions.
66 changes: 59 additions & 7 deletions Lib/test/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,8 @@ def func():


class TestLoadSuperAttr(CheckEvents):
RECORDERS = CallRecorder, LineRecorder, CRaiseRecorder, CReturnRecorder

def _super_method_call(self, optimized=False):
assignment = "x = 1" if optimized else "super = super"
codestr = textwrap.dedent(f"""
Expand Down Expand Up @@ -1128,10 +1130,62 @@ def test_method_call(self):
nonopt_func, nonopt_expected = self._super_method_call(optimized=False)
opt_func, opt_expected = self._super_method_call(optimized=True)

recorders = CallRecorder, LineRecorder, CRaiseRecorder, CReturnRecorder
self.check_events(nonopt_func, recorders=self.RECORDERS, expected=nonopt_expected)
self.check_events(opt_func, recorders=self.RECORDERS, expected=opt_expected)

self.check_events(nonopt_func, recorders=recorders, expected=nonopt_expected)
self.check_events(opt_func, recorders=recorders, expected=opt_expected)
def _super_method_call_error(self, optimized=False):
assignment = "x = 1" if optimized else "super = super"
codestr = textwrap.dedent(f"""
{assignment}
class A:
def method(self, x):
return x
class B(A):
def method(self, x):
return super(
x,
self,
).method(
x
)
b = B()
def f():
try:
return b.method(1)
except TypeError:
pass
else:
assert False, "should have raised TypeError"
""")
d = {}
exec(codestr, d, d)
expected = [
('line', 'check_events', 10),
('call', 'f', sys.monitoring.MISSING),
('line', 'f', 1),
('line', 'f', 2),
('call', 'method', d["b"]),
('line', 'method', 1),
('line', 'method', 2),
('line', 'method', 3),
('line', 'method', 1),
('call', 'super', 1),
('C raise', 'super', 1),
('line', 'f', 3),
('line', 'f', 4),
('line', 'check_events', 11),
('call', 'set_events', 2),
]
return d["f"], expected

def test_method_call_error(self):
nonopt_func, nonopt_expected = self._super_method_call_error(optimized=False)
opt_func, opt_expected = self._super_method_call_error(optimized=True)

self.check_events(nonopt_func, recorders=self.RECORDERS, expected=nonopt_expected)
self.check_events(opt_func, recorders=self.RECORDERS, expected=opt_expected)

def _super_attr(self, optimized=False):
assignment = "x = 1" if optimized else "super = super"
Expand Down Expand Up @@ -1170,10 +1224,8 @@ def test_attr(self):
nonopt_func, nonopt_expected = self._super_attr(optimized=False)
opt_func, opt_expected = self._super_attr(optimized=True)

recorders = CallRecorder, LineRecorder, CRaiseRecorder, CReturnRecorder

self.check_events(nonopt_func, recorders=recorders, expected=nonopt_expected)
self.check_events(opt_func, recorders=recorders, expected=opt_expected)
self.check_events(nonopt_func, recorders=self.RECORDERS, expected=nonopt_expected)
self.check_events(opt_func, recorders=self.RECORDERS, expected=opt_expected)


class TestSetGetEvents(MonitoringTestBase, unittest.TestCase):
Expand Down
8 changes: 4 additions & 4 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1594,12 +1594,9 @@ dummy_func(
PyObject *stack[] = {class, self};
PyObject *super = PyObject_Vectorcall(global_super, stack, oparg & 2, NULL);
DECREF_INPUTS();
ERROR_IF(super == NULL, error);
res = PyObject_GetAttr(super, name);
Py_DECREF(super);
if (opcode == INSTRUMENTED_LOAD_SUPER_ATTR) {
PyObject *arg = oparg & 2 ? class : &_PyInstrumentation_MISSING;
if (res == NULL) {
if (super == NULL) {
_Py_call_instrumentation_exc2(
tstate, PY_MONITORING_EVENT_C_RAISE,
frame, next_instr-1, global_super, arg);
Expand All @@ -1613,6 +1610,9 @@ dummy_func(
}
}
}
ERROR_IF(super == NULL, error);
res = PyObject_GetAttr(super, name);
Py_DECREF(super);
ERROR_IF(res == NULL, error);
}

Expand Down
8 changes: 4 additions & 4 deletions Python/generated_cases.c.h

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

0 comments on commit 9f1db4a

Please sign in to comment.