1 """Test suite for the cProfile module."""
2
3 import sys
4 import unittest
5
6 # rip off all interesting stuff from test_profile
7 import cProfile
8 from test.test_profile import ProfileTest, regenerate_expected_output
9 from test.support.script_helper import assert_python_failure
10 from test import support
11
12
13 class ESC[4;38;5;81mCProfileTest(ESC[4;38;5;149mProfileTest):
14 profilerclass = cProfile.Profile
15 profilermodule = cProfile
16 expected_max_output = "{built-in method builtins.max}"
17
18 def get_expected_output(self):
19 return _ProfileOutput
20
21 def test_bad_counter_during_dealloc(self):
22 # bpo-3895
23 import _lsprof
24
25 with support.catch_unraisable_exception() as cm:
26 obj = _lsprof.Profiler(lambda: int)
27 obj.enable()
28 obj.disable()
29 obj.clear()
30
31 self.assertEqual(cm.unraisable.exc_type, TypeError)
32
33 def test_profile_enable_disable(self):
34 prof = self.profilerclass()
35 # Make sure we clean ourselves up if the test fails for some reason.
36 self.addCleanup(prof.disable)
37
38 prof.enable()
39 self.assertEqual(
40 sys.monitoring.get_tool(sys.monitoring.PROFILER_ID), "cProfile")
41
42 prof.disable()
43 self.assertIs(sys.monitoring.get_tool(sys.monitoring.PROFILER_ID), None)
44
45 def test_profile_as_context_manager(self):
46 prof = self.profilerclass()
47 # Make sure we clean ourselves up if the test fails for some reason.
48 self.addCleanup(prof.disable)
49
50 with prof as __enter__return_value:
51 # profile.__enter__ should return itself.
52 self.assertIs(prof, __enter__return_value)
53
54 # profile should be set as the global profiler inside the
55 # with-block
56 self.assertEqual(
57 sys.monitoring.get_tool(sys.monitoring.PROFILER_ID), "cProfile")
58
59 # profile shouldn't be set once we leave the with-block.
60 self.assertIs(sys.monitoring.get_tool(sys.monitoring.PROFILER_ID), None)
61
62 def test_second_profiler(self):
63 pr = self.profilerclass()
64 pr2 = self.profilerclass()
65 pr.enable()
66 self.assertRaises(ValueError, pr2.enable)
67 pr.disable()
68
69 def test_throw(self):
70 """
71 gh-106152
72 generator.throw() should trigger a call in cProfile
73 In the any() call below, there should be two entries for the generator:
74 * one for the call to __next__ which gets a True and terminates any
75 * one when the generator is garbage collected which will effectively
76 do a throw.
77 """
78 pr = self.profilerclass()
79 pr.enable()
80 any(a == 1 for a in (1, 2))
81 pr.disable()
82 pr.create_stats()
83
84 for func, (cc, nc, _, _, _) in pr.stats.items():
85 if func[2] == "<genexpr>":
86 self.assertEqual(cc, 2)
87 self.assertEqual(nc, 2)
88
89
90 class ESC[4;38;5;81mTestCommandLine(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
91 def test_sort(self):
92 rc, out, err = assert_python_failure('-m', 'cProfile', '-s', 'demo')
93 self.assertGreater(rc, 0)
94 self.assertIn(b"option -s: invalid choice: 'demo'", err)
95
96
97 def main():
98 if '-r' not in sys.argv:
99 unittest.main()
100 else:
101 regenerate_expected_output(__file__, CProfileTest)
102
103
104 # Don't remove this comment. Everything below it is auto-generated.
105 #--cut--------------------------------------------------------------------------
106 _ProfileOutput = {}
107 _ProfileOutput['print_stats'] = """\
108 28 0.028 0.001 0.028 0.001 profilee.py:110(__getattr__)
109 1 0.270 0.270 1.000 1.000 profilee.py:25(testfunc)
110 23/3 0.150 0.007 0.170 0.057 profilee.py:35(factorial)
111 20 0.020 0.001 0.020 0.001 profilee.py:48(mul)
112 2 0.040 0.020 0.600 0.300 profilee.py:55(helper)
113 4 0.116 0.029 0.120 0.030 profilee.py:73(helper1)
114 2 0.000 0.000 0.140 0.070 profilee.py:84(helper2_indirect)
115 8 0.312 0.039 0.400 0.050 profilee.py:88(helper2)
116 8 0.064 0.008 0.080 0.010 profilee.py:98(subhelper)"""
117 _ProfileOutput['print_callers'] = """\
118 profilee.py:110(__getattr__) <- 16 0.016 0.016 profilee.py:98(subhelper)
119 profilee.py:25(testfunc) <- 1 0.270 1.000 <string>:1(<module>)
120 profilee.py:35(factorial) <- 1 0.014 0.130 profilee.py:25(testfunc)
121 20/3 0.130 0.147 profilee.py:35(factorial)
122 2 0.006 0.040 profilee.py:84(helper2_indirect)
123 profilee.py:48(mul) <- 20 0.020 0.020 profilee.py:35(factorial)
124 profilee.py:55(helper) <- 2 0.040 0.600 profilee.py:25(testfunc)
125 profilee.py:73(helper1) <- 4 0.116 0.120 profilee.py:55(helper)
126 profilee.py:84(helper2_indirect) <- 2 0.000 0.140 profilee.py:55(helper)
127 profilee.py:88(helper2) <- 6 0.234 0.300 profilee.py:55(helper)
128 2 0.078 0.100 profilee.py:84(helper2_indirect)
129 profilee.py:98(subhelper) <- 8 0.064 0.080 profilee.py:88(helper2)
130 {built-in method builtins.hasattr} <- 4 0.000 0.004 profilee.py:73(helper1)
131 8 0.000 0.008 profilee.py:88(helper2)
132 {built-in method sys.exception} <- 4 0.000 0.000 profilee.py:73(helper1)
133 {method 'append' of 'list' objects} <- 4 0.000 0.000 profilee.py:73(helper1)"""
134 _ProfileOutput['print_callees'] = """\
135 <string>:1(<module>) -> 1 0.270 1.000 profilee.py:25(testfunc)
136 profilee.py:110(__getattr__) ->
137 profilee.py:25(testfunc) -> 1 0.014 0.130 profilee.py:35(factorial)
138 2 0.040 0.600 profilee.py:55(helper)
139 profilee.py:35(factorial) -> 20/3 0.130 0.147 profilee.py:35(factorial)
140 20 0.020 0.020 profilee.py:48(mul)
141 profilee.py:48(mul) ->
142 profilee.py:55(helper) -> 4 0.116 0.120 profilee.py:73(helper1)
143 2 0.000 0.140 profilee.py:84(helper2_indirect)
144 6 0.234 0.300 profilee.py:88(helper2)
145 profilee.py:73(helper1) -> 4 0.000 0.004 {built-in method builtins.hasattr}
146 profilee.py:84(helper2_indirect) -> 2 0.006 0.040 profilee.py:35(factorial)
147 2 0.078 0.100 profilee.py:88(helper2)
148 profilee.py:88(helper2) -> 8 0.064 0.080 profilee.py:98(subhelper)
149 profilee.py:98(subhelper) -> 16 0.016 0.016 profilee.py:110(__getattr__)
150 {built-in method builtins.hasattr} -> 12 0.012 0.012 profilee.py:110(__getattr__)"""
151
152 if __name__ == "__main__":
153 main()