1"""Test suite for the cProfile module."""
2
3import sys
4from test.support import run_unittest, TESTFN, unlink
5
6# rip off all interesting stuff from test_profile
7import cProfile
8from test.test_profile import ProfileTest, regenerate_expected_output
9
10
11class CProfileTest(ProfileTest):
12    profilerclass = cProfile.Profile
13    profilermodule = cProfile
14    expected_max_output = "{built-in method builtins.max}"
15
16    def get_expected_output(self):
17        return _ProfileOutput
18
19    # Issue 3895.
20    def test_bad_counter_during_dealloc(self):
21        import _lsprof
22        # Must use a file as StringIO doesn't trigger the bug.
23        orig_stderr = sys.stderr
24        try:
25            with open(TESTFN, 'w') as file:
26                sys.stderr = file
27                try:
28                    obj = _lsprof.Profiler(lambda: int)
29                    obj.enable()
30                    obj = _lsprof.Profiler(1)
31                    obj.disable()
32                    obj.clear()
33                finally:
34                    sys.stderr = orig_stderr
35        finally:
36            unlink(TESTFN)
37
38
39def test_main():
40    run_unittest(CProfileTest)
41
42def main():
43    if '-r' not in sys.argv:
44        test_main()
45    else:
46        regenerate_expected_output(__file__, CProfileTest)
47
48
49# Don't remove this comment. Everything below it is auto-generated.
50#--cut--------------------------------------------------------------------------
51_ProfileOutput = {}
52_ProfileOutput['print_stats'] = """\
53       28    0.028    0.001    0.028    0.001 profilee.py:110(__getattr__)
54        1    0.270    0.270    1.000    1.000 profilee.py:25(testfunc)
55     23/3    0.150    0.007    0.170    0.057 profilee.py:35(factorial)
56       20    0.020    0.001    0.020    0.001 profilee.py:48(mul)
57        2    0.040    0.020    0.600    0.300 profilee.py:55(helper)
58        4    0.116    0.029    0.120    0.030 profilee.py:73(helper1)
59        2    0.000    0.000    0.140    0.070 profilee.py:84(helper2_indirect)
60        8    0.312    0.039    0.400    0.050 profilee.py:88(helper2)
61        8    0.064    0.008    0.080    0.010 profilee.py:98(subhelper)"""
62_ProfileOutput['print_callers'] = """\
63profilee.py:110(__getattr__)                      <-      16    0.016    0.016  profilee.py:98(subhelper)
64profilee.py:25(testfunc)                          <-       1    0.270    1.000  <string>:1(<module>)
65profilee.py:35(factorial)                         <-       1    0.014    0.130  profilee.py:25(testfunc)
66                                                        20/3    0.130    0.147  profilee.py:35(factorial)
67                                                           2    0.006    0.040  profilee.py:84(helper2_indirect)
68profilee.py:48(mul)                               <-      20    0.020    0.020  profilee.py:35(factorial)
69profilee.py:55(helper)                            <-       2    0.040    0.600  profilee.py:25(testfunc)
70profilee.py:73(helper1)                           <-       4    0.116    0.120  profilee.py:55(helper)
71profilee.py:84(helper2_indirect)                  <-       2    0.000    0.140  profilee.py:55(helper)
72profilee.py:88(helper2)                           <-       6    0.234    0.300  profilee.py:55(helper)
73                                                           2    0.078    0.100  profilee.py:84(helper2_indirect)
74profilee.py:98(subhelper)                         <-       8    0.064    0.080  profilee.py:88(helper2)
75{built-in method builtins.hasattr}                <-       4    0.000    0.004  profilee.py:73(helper1)
76                                                           8    0.000    0.008  profilee.py:88(helper2)
77{built-in method sys.exc_info}                    <-       4    0.000    0.000  profilee.py:73(helper1)
78{method 'append' of 'list' objects}               <-       4    0.000    0.000  profilee.py:73(helper1)"""
79_ProfileOutput['print_callees'] = """\
80<string>:1(<module>)                              ->       1    0.270    1.000  profilee.py:25(testfunc)
81profilee.py:110(__getattr__)                      ->
82profilee.py:25(testfunc)                          ->       1    0.014    0.130  profilee.py:35(factorial)
83                                                           2    0.040    0.600  profilee.py:55(helper)
84profilee.py:35(factorial)                         ->    20/3    0.130    0.147  profilee.py:35(factorial)
85                                                          20    0.020    0.020  profilee.py:48(mul)
86profilee.py:48(mul)                               ->
87profilee.py:55(helper)                            ->       4    0.116    0.120  profilee.py:73(helper1)
88                                                           2    0.000    0.140  profilee.py:84(helper2_indirect)
89                                                           6    0.234    0.300  profilee.py:88(helper2)
90profilee.py:73(helper1)                           ->       4    0.000    0.004  {built-in method builtins.hasattr}
91profilee.py:84(helper2_indirect)                  ->       2    0.006    0.040  profilee.py:35(factorial)
92                                                           2    0.078    0.100  profilee.py:88(helper2)
93profilee.py:88(helper2)                           ->       8    0.064    0.080  profilee.py:98(subhelper)
94profilee.py:98(subhelper)                         ->      16    0.016    0.016  profilee.py:110(__getattr__)
95{built-in method builtins.hasattr}                ->      12    0.012    0.012  profilee.py:110(__getattr__)"""
96
97if __name__ == "__main__":
98    main()
99