Debian Codegraph @

pypy/module/_hpy_universal/test/_vendored/trace/apptest_trace.py

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