tests: Add tests for sys.settrace feature.

This commit is contained in:
Milan Rossa 2019-08-14 16:11:25 +02:00 committed by Damien George
parent 310b3d1b81
commit 498e35219e
9 changed files with 584 additions and 0 deletions

View File

@ -417,6 +417,7 @@ arg names:
(N_STATE 1)
(N_EXC_STACK 0)
bc=-1 line=1
########
bc=13 line=149
00 LOAD_NAME __name__ (cache=0)
04 STORE_NAME __module__
@ -450,6 +451,7 @@ arg names: * * *
(N_EXC_STACK 0)
bc=-\\d\+ line=1
bc=0 line=59
########
00 LOAD_NULL
01 LOAD_FAST 2
02 LOAD_NULL
@ -473,6 +475,7 @@ arg names: * * *
(N_EXC_STACK 0)
bc=-\\d\+ line=1
bc=0 line=60
########
00 BUILD_LIST 0
02 LOAD_FAST 2
03 GET_ITER_STACK

View File

@ -0,0 +1,91 @@
import sys
try:
sys.settrace
except AttributeError:
print("SKIP")
raise SystemExit
def print_stacktrace(frame, level=0):
# Ignore CPython specific helpers.
if frame.f_globals['__name__'].find('importlib') != -1:
print_stacktrace(frame.f_back, level)
return
print("%2d: %s@%s:%s => %s:%d" % (
level, " ",
frame.f_globals['__name__'],
frame.f_code.co_name,
# reduce full path to some pseudo-relative
'misc' + ''.join(frame.f_code.co_filename.split('tests/misc')[-1:]),
frame.f_lineno,
))
if frame.f_back:
print_stacktrace(frame.f_back, level + 1)
class _Prof:
trace_count = 0;
def trace_tick(self, frame, event, arg):
self.trace_count += 1
print_stacktrace(frame)
__prof__ = _Prof()
alice_handler_set = False
def trace_tick_handler_alice(frame, event, arg):
print("### trace_handler::Alice event:", event)
__prof__.trace_tick(frame, event, arg)
return trace_tick_handler_alice
bob_handler_set = False
def trace_tick_handler_bob(frame, event, arg):
print("### trace_handler::Bob event:", event)
__prof__.trace_tick(frame, event, arg)
return trace_tick_handler_bob
def trace_tick_handler(frame, event, arg):
# Ignore CPython specific helpers.
if frame.f_globals['__name__'].find('importlib') != -1:
return
print("### trace_handler::main event:", event)
__prof__.trace_tick(frame, event, arg)
if frame.f_code.co_name != 'factorial':
return trace_tick_handler
global alice_handler_set
if event == 'call' and not alice_handler_set:
alice_handler_set = True
return trace_tick_handler_alice
global bob_handler_set
if event == 'call' and not bob_handler_set:
bob_handler_set = True
return trace_tick_handler_bob
return trace_tick_handler
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
def do_tests():
# These commands are here to demonstrate some execution being traced.
print("Who loves the sun?")
print("Not every-", factorial(3))
from sys_settrace_subdir import trace_generic
trace_generic.run_tests()
return
sys.settrace(trace_tick_handler)
do_tests()
sys.settrace(None)
print("\n------------------ script exited ------------------")
print("Total traces executed: ", __prof__.trace_count)

View File

@ -0,0 +1,62 @@
# test sys.settrace with generators
import sys
try:
sys.settrace
except AttributeError:
print("SKIP")
raise SystemExit
def print_stacktrace(frame, level=0):
print("%2d: %s@%s:%s => %s:%d" % (
level, " ",
frame.f_globals['__name__'],
frame.f_code.co_name,
# reduce full path to some pseudo-relative
'misc' + ''.join(frame.f_code.co_filename.split('tests/misc')[-1:]),
frame.f_lineno,
))
if frame.f_back:
print_stacktrace(frame.f_back, level + 1)
trace_count = 0
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
def test_generator():
def make_gen():
yield 1<<0
yield 1<<1
yield 1<<2
return 1<<3
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)
sys.settrace(trace_tick_handler)
test_generator()
sys.settrace(None)
print("Total traces executed: ", trace_count)

View File

@ -0,0 +1,195 @@
### trace_handler::main event: call
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:33
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:34
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:40
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:41
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:42
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:44
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: call
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:34
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:44
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:35
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:44
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: return
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:35
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:44
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:48
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: call
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:35
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:48
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:35
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:48
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:36
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:48
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: return
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:36
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:48
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: call
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:36
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:48
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:36
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:48
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:37
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:48
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: return
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:37
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:48
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: call
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:37
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:48
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:37
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:48
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:38
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:48
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: return
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:38
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:48
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: exception
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:48
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:50
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:51
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
test_generator 7 8
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:53
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:54
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: call
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:34
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:35
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: return
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:35
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:56
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: call
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:35
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:35
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:36
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: return
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:36
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:56
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: call
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:36
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:36
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:37
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: return
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:37
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:56
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: call
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:37
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:37
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:38
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: return
0: @__main__:make_gen => miscmisc/sys_settrace_generator.py:38
1: @__main__:test_generator => miscmisc/sys_settrace_generator.py:55
2: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
### trace_handler::main event: line
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:57
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
7
### trace_handler::main event: return
0: @__main__:test_generator => miscmisc/sys_settrace_generator.py:57
1: @__main__:<module> => miscmisc/sys_settrace_generator.py:60
Total traces executed: 54

View File

@ -0,0 +1,51 @@
# test sys.settrace with while and for loops
import sys
try:
sys.settrace
except AttributeError:
print("SKIP")
raise SystemExit
def print_stacktrace(frame, level=0):
print("%2d: %s@%s:%s => %s:%d" % (
level, " ",
frame.f_globals['__name__'],
frame.f_code.co_name,
# reduce full path to some pseudo-relative
'misc' + ''.join(frame.f_code.co_filename.split('tests/misc')[-1:]),
frame.f_lineno,
))
if frame.f_back:
print_stacktrace(frame.f_back, level + 1)
trace_count = 0
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
def test_loop():
# for loop
r = 0
for i in range(3):
r += i
print("test_for_loop", r)
# while loop
r = 0
i = 0
while i < 3:
r += i
i += 1
print("test_while_loop", i)
sys.settrace(trace_tick_handler)
test_loop()
sys.settrace(None)
print("Total traces executed: ", trace_count)

View File

@ -0,0 +1,72 @@
### trace_handler::main event: call
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:33
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:35
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:36
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:37
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:36
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:37
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:36
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:37
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:36
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:37
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:38
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
test_for_loop 3
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:41
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:42
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:43
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:45
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:44
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:45
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:44
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:45
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:44
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:45
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
### trace_handler::main event: line
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:46
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
test_while_loop 3
### trace_handler::main event: return
0: @__main__:test_loop => miscmisc/sys_settrace_loop.py:46
1: @__main__:<module> => miscmisc/sys_settrace_loop.py:49
Total traces executed: 23

View File

@ -0,0 +1,82 @@
print("Now comes the language constructions tests.")
# function
def test_func():
def test_sub_func():
print("test_function")
test_sub_func()
# closure
def test_closure(msg):
def make_closure():
print(msg)
return make_closure
# exception
def test_exception():
try:
raise Exception("test_exception")
except Exception:
pass
finally:
pass
# listcomp
def test_listcomp():
print("test_listcomp", [x for x in range(3)])
# lambda
def test_lambda():
func_obj_1 = lambda a, b: a + b
print(func_obj_1(10, 20))
# import
def test_import():
from sys_settrace_subdir import trace_importme
trace_importme.dummy()
trace_importme.saysomething()
# class
class TLClass():
def method():
pass
pass
def test_class():
class TestClass:
__anynum = -9
def method(self):
print("test_class_method")
self.__anynum += 1
def prprty_getter(self):
return self.__anynum
def prprty_setter(self, what):
self.__anynum = what
prprty = property(prprty_getter, prprty_setter)
cls = TestClass()
cls.method()
print("test_class_property", cls.prprty)
cls.prprty = 12
print("test_class_property", cls.prprty)
def run_tests():
test_func()
test_closure_inst = test_closure("test_closure")
test_closure_inst()
test_exception()
test_listcomp()
test_lambda()
test_class()
test_import()
print("And it's done!")

View File

@ -0,0 +1,24 @@
print("Yep, I got imported.")
try:
x = const(1)
except NameError:
print('const not defined')
const = lambda x: x
_CNT01 = "CONST01"
_CNT02 = const(123)
A123 = const(123)
a123 = const(123)
def dummy():
return False
def saysomething():
print("There, I said it.")
def neverexecuted():
print("Never got here!")
print("Yep, got here")

View File

@ -83,6 +83,10 @@ exclude_tests = (
'micropython/meminfo.py',
# needs sys stdfiles
'misc/print_exception.py',
# settrace .exp files are too large
'misc/sys_settrace_loop.py',
'misc/sys_settrace_generator.py',
'misc/sys_settrace_features.py',
)
output = []