Debian Codegraph @

extra_tests/hpy_tests/_vendored/trace/test_trace.py

raw symbols SCIP
1 import pytest
2 try:
3 from hpy.trace import get_call_counts, get_durations, set_trace_functions
4 except ModuleNotFoundError:
5 pytest.mark.skip("no hpy.trace")
6
7 @pytest.fixture
8 def hpy_abi():
9 return "trace"
10
11
12 def get_call_counter():
13 from collections import Counter
14 return Counter(get_call_counts())
15
16
17 def _assert_unchanged_except(expected, actual, *ignore):
18 import pytest
19 for x in expected.keys():
20 if x not in ignore and actual[x] != expected[x]:
21 pytest.fail("%s unexpectedly changed (expected %d but was %d)." %
22 (x, expected[x], actual[x]))
23
24
25 def test_get_call_counts(compiler):
26 import pytest
27 mod = compiler.make_module("""
28 HPyDef_METH(f, "f", HPyFunc_VARARGS)
29 static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs)
30 {
31 if (nargs != 2) {
32 HPyErr_SetString(ctx, ctx->h_TypeError, "expected exactly two args");
33 return HPy_NULL;
34 }
35 return HPy_Add(ctx, args[0], args[1]);
36 }
37
38 @EXPORT(f)
39 @INIT
40 """)
41 # Don't rely on absolute call count numbers since the module setup could
42 # already do some API calls we don't expect.
43 call_counter_0 = get_call_counter()
44
45 assert mod.f(1, 2) == 3
46 assert get_call_counter() - call_counter_0 == {"ctx_Add": 1}
47 assert mod.f(10, 20) == 30
48 assert get_call_counter() - call_counter_0 == {"ctx_Add": 2}
49 with pytest.raises(TypeError):
50 mod.f(1, 2, 3)
51 assert get_call_counter() - call_counter_0 == {"ctx_Add": 2, "ctx_Err_SetString": 1}
52
53
54 def test_get_durations(compiler):
55 import time
56 mod = compiler.make_module("""
57 HPyDef_METH(f, "f", HPyFunc_O)
58 static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
59 {
60 if (!HPyCallable_Check(ctx, arg)) {
61 HPyErr_SetString(ctx, ctx->h_TypeError, "expected callable");
62 return HPy_NULL;
63 }
64 return HPy_CallTupleDict(ctx, arg, HPy_NULL, HPy_NULL);
65 }
66
67 @EXPORT(f)
68 @INIT
69 """)
70 # Don't rely on absolute durations numbers since the module setup could
71 # already do some API calls we don't expect.
72 durations0 = get_durations().copy()
73
74 def callback():
75 time.sleep(0.3)
76 return 123
77
78 assert mod.f(callback) == 123
79 assert get_durations()["ctx_CallTupleDict"] >= durations0["ctx_CallTupleDict"] + 3e5
80 _assert_unchanged_except(durations0, get_durations(), "ctx_CallTupleDict", "ctx_Callable_Check")
81
82
83 def test_trace_funcs(compiler):
84 import pytest
85 mod = compiler.make_module("""
86 HPyDef_METH(f, "f", HPyFunc_VARARGS)
87 static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs)
88 {
89 if (nargs != 2) {
90 HPyErr_SetString(ctx, ctx->h_TypeError, "expected exactly two args");
91 return HPy_NULL;
92 }
93 return HPy_Add(ctx, args[0], args[1]);
94 }
95
96 @EXPORT(f)
97 @INIT
98 """)
99 on_enter_cnt = 0
100 on_exit_cnt = 0
101
102 def on_enter(name):
103 assert name == "ctx_Add"
104 nonlocal on_enter_cnt
105 on_enter_cnt += 1
106
107 def on_exit(name):
108 assert name == "ctx_Add"
109 nonlocal on_exit_cnt
110 on_exit_cnt += 1
111
112 set_trace_functions(on_enter)
113 assert on_enter_cnt == 0
114 assert on_exit_cnt == 0
115 assert mod.f(1, 2) == 3
116 assert on_enter_cnt == 1
117 assert on_exit_cnt == 0
118 # on_enter should stay untouched
119 set_trace_functions(on_exit=on_exit)
120 assert mod.f(2, 3) == 5
121 assert on_enter_cnt == 2
122 assert on_exit_cnt == 1
123 # clear on_enter func
124 set_trace_functions(on_enter=None)
125 assert mod.f(2, 3) == 5
126 assert on_enter_cnt == 2
127 assert on_exit_cnt == 2
128
129 with pytest.raises(TypeError):
130 set_trace_functions(1)