Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-103865: add monitoring support to LOAD_SUPER_ATTR #103866

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

Merged
merged 17 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix C raise event
  • Loading branch information
carljm committed Apr 29, 2023
commit 9f1db4a4aa1fa520ded7cdaa5cd618cdf8ce89a8
66 changes: 59 additions & 7 deletions 66 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 8 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 8 Python/generated_cases.c.h

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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.