2019-08-14 10:11:25 -04:00
|
|
|
# test sys.settrace with generators
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
try:
|
|
|
|
sys.settrace
|
|
|
|
except AttributeError:
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-08-14 10:11:25 -04:00
|
|
|
def print_stacktrace(frame, level=0):
|
2020-03-22 22:26:08 -04:00
|
|
|
print(
|
|
|
|
"%2d: %s@%s:%s => %s:%d"
|
|
|
|
% (
|
|
|
|
level,
|
|
|
|
" ",
|
|
|
|
frame.f_globals["__name__"],
|
|
|
|
frame.f_code.co_name,
|
2020-12-15 05:54:34 -05:00
|
|
|
# Keep just the filename.
|
|
|
|
"sys_settrace_" + frame.f_code.co_filename.split("sys_settrace_")[-1],
|
2020-03-22 22:26:08 -04:00
|
|
|
frame.f_lineno,
|
|
|
|
)
|
|
|
|
)
|
2019-08-14 10:11:25 -04:00
|
|
|
|
|
|
|
if frame.f_back:
|
|
|
|
print_stacktrace(frame.f_back, level + 1)
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-08-14 10:11:25 -04:00
|
|
|
trace_count = 0
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-08-14 10:11:25 -04:00
|
|
|
def trace_tick_handler(frame, event, arg):
|
|
|
|
global trace_count
|
|
|
|
print("### trace_handler::main event:", event)
|
|
|
|
trace_count += 1
|
|
|
|
print_stacktrace(frame)
|
|
|
|
return trace_tick_handler
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-08-14 10:11:25 -04:00
|
|
|
def test_generator():
|
|
|
|
def make_gen():
|
2020-03-22 22:26:08 -04:00
|
|
|
yield 1 << 0
|
|
|
|
yield 1 << 1
|
|
|
|
yield 1 << 2
|
|
|
|
return 1 << 3
|
2019-08-14 10:11:25 -04:00
|
|
|
|
|
|
|
gen = make_gen()
|
|
|
|
r = 0
|
|
|
|
try:
|
|
|
|
r += gen.send(None)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
r += gen.send(None)
|
|
|
|
|
|
|
|
except StopIteration as e:
|
|
|
|
print("test_generator", r, e)
|
|
|
|
|
|
|
|
gen = make_gen()
|
|
|
|
r = 0
|
|
|
|
for i in gen:
|
|
|
|
r += i
|
|
|
|
print(r)
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-08-14 10:11:25 -04:00
|
|
|
sys.settrace(trace_tick_handler)
|
|
|
|
test_generator()
|
|
|
|
sys.settrace(None)
|
|
|
|
print("Total traces executed: ", trace_count)
|