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

Skip to content
Merged
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
extract _exec and add comment
  • Loading branch information
carljm committed May 10, 2023
commit c203916ad192ef7287983e2bef19f5d7004ec9f8
40 changes: 22 additions & 18 deletions Lib/test/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1122,10 +1122,21 @@ def func():
class TestLoadSuperAttr(CheckEvents):
RECORDERS = CallRecorder, LineRecorder, CRaiseRecorder, CReturnRecorder

def _super_method_call(self, optimized=False):
def _exec(self, codestr, optimized=False):
# The compiler checks for statically visible shadowing of the name
# `super`, and declines to emit `LOAD_SUPER_ATTR` if shadowing is found.
# So inserting `super = super` prevents the compiler from emitting
# `LOAD_SUPER_ATTR`, and allows us to test that monitoring events for
# `LOAD_SUPER_ATTR` are equivalent to those we'd get from the
# un-optimized `LOAD_GLOBAL super; CALL; LOAD_ATTR` form.
assignment = "x = 1" if optimized else "super = super"
codestr = textwrap.dedent(f"""
{assignment}
codestr = f"{assignment}\n{textwrap.dedent(codestr)}"
d = {}
exec(codestr, d, d)
return d

def _super_method_call(self, optimized=False):
codestr = """
class A:
def method(self, x):
return x
Expand All @@ -1140,9 +1151,8 @@ def method(self, x):
b = B()
def f():
return b.method(1)
""")
d = {}
exec(codestr, d, d)
"""
d = self._exec(codestr, optimized)
expected = [
('line', 'check_events', 10),
('call', 'f', sys.monitoring.MISSING),
Expand Down Expand Up @@ -1170,9 +1180,7 @@ def test_method_call(self):
self.check_events(opt_func, recorders=self.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}
codestr = """
class A:
def method(self, x):
return x
Expand All @@ -1194,9 +1202,8 @@ def f():
pass
else:
assert False, "should have raised TypeError"
""")
d = {}
exec(codestr, d, d)
"""
d = self._exec(codestr, optimized)
expected = [
('line', 'check_events', 10),
('call', 'f', sys.monitoring.MISSING),
Expand Down Expand Up @@ -1224,9 +1231,7 @@ def test_method_call_error(self):
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"
codestr = textwrap.dedent(f"""
{assignment}
codestr = """
class A:
x = 1

Expand All @@ -1238,9 +1243,8 @@ def method(self):
b = B()
def f():
return b.method()
""")
d = {}
exec(codestr, d, d)
"""
d = self._exec(codestr, optimized)
expected = [
('line', 'check_events', 10),
('call', 'f', sys.monitoring.MISSING),
Expand Down