Debian Codegraph @

lib-python/3/test/test_cprofile.py

raw symbols SCIP
1 """Test suite for the cProfile module."""
2
3 import sys
4 from test.support import cpython_only
5 import unittest
6
7 # rip off all interesting stuff from test_profile
8 import cProfile
9 from test.test_profile import ProfileTest, regenerate_expected_output
10 from test.support.script_helper import assert_python_failure
11 from test import support
12
13
14 class CProfileTest(ProfileTest):
15 profilerclass = cProfile.Profile
16 profilermodule = cProfile
17 expected_max_output = "{built-in method builtins.max}"
18
19 def get_expected_output(self):
20 return _ProfileOutput
21
22 @cpython_only
23 def test_bad_counter_during_dealloc(self):
24 # bpo-3895
25 import _lsprof
26
27 with support.catch_unraisable_exception() as cm:
28 obj = _lsprof.Profiler(lambda: int)
29 obj.enable()
30 obj = _lsprof.Profiler(1)
31 obj.disable()
32 obj.clear()
33
34 self.assertEqual(cm.unraisable.exc_type, TypeError)
35
36 def test_profile_enable_disable(self):
37 prof = self.profilerclass()
38 # Make sure we clean ourselves up if the test fails for some reason.
39 self.addCleanup(prof.disable)
40
41 prof.enable()
42 self.assertIs(sys.getprofile(), prof)
43
44 prof.disable()
45 self.assertIs(sys.getprofile(), None)
46
47 def test_profile_as_context_manager(self):
48 prof = self.profilerclass()
49 # Make sure we clean ourselves up if the test fails for some reason.
50 self.addCleanup(prof.disable)
51
52 with prof as __enter__return_value:
53 # profile.__enter__ should return itself.
54 self.assertIs(prof, __enter__return_value)
55
56 # profile should be set as the global profiler inside the
57 # with-block
58 self.assertIs(sys.getprofile(), prof)
59
60 # profile shouldn't be set once we leave the with-block.
61 self.assertIs(sys.getprofile(), None)
62
63 class TestCommandLine(unittest.TestCase):
64 def test_sort(self):
65 rc, out, err = assert_python_failure('-m', 'cProfile', '-s', 'demo')
66 self.assertGreater(rc, 0)
67 self.assertIn(b"option -s: invalid choice: 'demo'", err)
68
69
70 def main():
71 if '-r' not in sys.argv:
72 unittest.main()
73 else:
74 regenerate_expected_output(__file__, CProfileTest)
75
76
77 # Don't remove this comment. Everything below it is auto-generated.
78 #--cut--------------------------------------------------------------------------
79 _ProfileOutput = {}
80 _ProfileOutput['print_stats'] = """\
81 28 0.028 0.001 0.028 0.001 profilee.py:110(__getattr__)
82 1 0.270 0.270 1.000 1.000 profilee.py:25(testfunc)
83 23/3 0.150 0.007 0.170 0.057 profilee.py:35(factorial)
84 20 0.020 0.001 0.020 0.001 profilee.py:48(mul)
85 2 0.040 0.020 0.600 0.300 profilee.py:55(helper)
86 4 0.116 0.029 0.120 0.030 profilee.py:73(helper1)
87 2 0.000 0.000 0.140 0.070 profilee.py:84(helper2_indirect)
88 8 0.312 0.039 0.400 0.050 profilee.py:88(helper2)
89 8 0.064 0.008 0.080 0.010 profilee.py:98(subhelper)"""
90 _ProfileOutput['print_callers'] = """\
91 profilee.py:110(__getattr__) <- 16 0.016 0.016 profilee.py:98(subhelper)
92 profilee.py:25(testfunc) <- 1 0.270 1.000 <string>:1(<module>)
93 profilee.py:35(factorial) <- 1 0.014 0.130 profilee.py:25(testfunc)
94 20/3 0.130 0.147 profilee.py:35(factorial)
95 2 0.006 0.040 profilee.py:84(helper2_indirect)
96 profilee.py:48(mul) <- 20 0.020 0.020 profilee.py:35(factorial)
97 profilee.py:55(helper) <- 2 0.040 0.600 profilee.py:25(testfunc)
98 profilee.py:73(helper1) <- 4 0.116 0.120 profilee.py:55(helper)
99 profilee.py:84(helper2_indirect) <- 2 0.000 0.140 profilee.py:55(helper)
100 profilee.py:88(helper2) <- 6 0.234 0.300 profilee.py:55(helper)
101 2 0.078 0.100 profilee.py:84(helper2_indirect)
102 profilee.py:98(subhelper) <- 8 0.064 0.080 profilee.py:88(helper2)
103 {built-in method builtins.hasattr} <- 4 0.000 0.004 profilee.py:73(helper1)
104 8 0.000 0.008 profilee.py:88(helper2)
105 {built-in method sys.exc_info} <- 4 0.000 0.000 profilee.py:73(helper1)
106 {method 'append' of 'list' objects} <- 4 0.000 0.000 profilee.py:73(helper1)"""
107 _ProfileOutput['print_callees'] = """\
108 <string>:1(<module>) -> 1 0.270 1.000 profilee.py:25(testfunc)
109 profilee.py:110(__getattr__) ->
110 profilee.py:25(testfunc) -> 1 0.014 0.130 profilee.py:35(factorial)
111 2 0.040 0.600 profilee.py:55(helper)
112 profilee.py:35(factorial) -> 20/3 0.130 0.147 profilee.py:35(factorial)
113 20 0.020 0.020 profilee.py:48(mul)
114 profilee.py:48(mul) ->
115 profilee.py:55(helper) -> 4 0.116 0.120 profilee.py:73(helper1)
116 2 0.000 0.140 profilee.py:84(helper2_indirect)
117 6 0.234 0.300 profilee.py:88(helper2)
118 profilee.py:73(helper1) -> 4 0.000 0.004 {built-in method builtins.hasattr}
119 profilee.py:84(helper2_indirect) -> 2 0.006 0.040 profilee.py:35(factorial)
120 2 0.078 0.100 profilee.py:88(helper2)
121 profilee.py:88(helper2) -> 8 0.064 0.080 profilee.py:98(subhelper)
122 profilee.py:98(subhelper) -> 16 0.016 0.016 profilee.py:110(__getattr__)
123 {built-in method builtins.hasattr} -> 12 0.012 0.012 profilee.py:110(__getattr__)"""
124
125 if __name__ == "__main__":
126 main()