Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7d76653

Browse files
committed
a trival fix to let test_profile pass if it runs after test_cprofile
1 parent 4e299c7 commit 7d76653

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

Lib/test/test_cprofile.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
class CProfileTest(ProfileTest):
1111
profilerclass = cProfile.Profile
1212

13+
def get_expected_output(self):
14+
return _ProfileOutput
15+
1316

1417
def test_main():
1518
run_unittest(CProfileTest)
@@ -23,7 +26,8 @@ def main():
2326

2427
# Don't remove this comment. Everything below it is auto-generated.
2528
#--cut--------------------------------------------------------------------------
26-
CProfileTest.expected_output['print_stats'] = """\
29+
_ProfileOutput = {}
30+
_ProfileOutput['print_stats'] = """\
2731
28 0.028 0.001 0.028 0.001 profilee.py:110(__getattr__)
2832
1 0.270 0.270 1.000 1.000 profilee.py:25(testfunc)
2933
23/3 0.150 0.007 0.170 0.057 profilee.py:35(factorial)
@@ -33,7 +37,7 @@ def main():
3337
2 0.000 0.000 0.140 0.070 profilee.py:84(helper2_indirect)
3438
8 0.312 0.039 0.400 0.050 profilee.py:88(helper2)
3539
8 0.064 0.008 0.080 0.010 profilee.py:98(subhelper)"""
36-
CProfileTest.expected_output['print_callers'] = """\
40+
_ProfileOutput['print_callers'] = """\
3741
profilee.py:110(__getattr__) <- 16 0.016 0.016 profilee.py:98(subhelper)
3842
profilee.py:25(testfunc) <- 1 0.270 1.000 <string>:1(<module>)
3943
profilee.py:35(factorial) <- 1 0.014 0.130 profilee.py:25(testfunc)
@@ -50,7 +54,7 @@ def main():
5054
{built-in method hasattr} <- 4 0.000 0.004 profilee.py:73(helper1)
5155
8 0.000 0.008 profilee.py:88(helper2)
5256
{method 'append' of 'list' objects} <- 4 0.000 0.000 profilee.py:73(helper1)"""
53-
CProfileTest.expected_output['print_callees'] = """\
57+
_ProfileOutput['print_callees'] = """\
5458
<string>:1(<module>) -> 1 0.270 1.000 profilee.py:25(testfunc)
5559
profilee.py:110(__getattr__) ->
5660
profilee.py:25(testfunc) -> 1 0.014 0.130 profilee.py:35(factorial)

Lib/test/test_profile.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ class ProfileTest(unittest.TestCase):
1616

1717
profilerclass = profile.Profile
1818
methodnames = ['print_stats', 'print_callers', 'print_callees']
19-
expected_output = {}
19+
20+
def get_expected_output(self):
21+
return _ProfileOutput
2022

2123
@classmethod
2224
def do_profiling(cls):
@@ -41,14 +43,15 @@ def do_profiling(cls):
4143

4244
def test_cprofile(self):
4345
results = self.do_profiling()
46+
expected = self.get_expected_output()
4447
self.assertEqual(results[0], 1000)
4548
for i, method in enumerate(self.methodnames):
46-
if results[i+1] != self.expected_output[method]:
49+
if results[i+1] != expected[method]:
4750
print("Stats.%s output for %s doesn't fit expectation!" %
4851
(method, self.profilerclass.__name__))
4952
print('\n'.join(unified_diff(
5053
results[i+1].split('\n'),
51-
self.expected_output[method].split('\n'))))
54+
expected[method].split('\n'))))
5255

5356

5457
def regenerate_expected_output(filename, cls):
@@ -65,9 +68,10 @@ def regenerate_expected_output(filename, cls):
6568

6669
with open(filename, 'w') as f:
6770
f.writelines(newfile)
71+
f.write("_ProfileOutput = {}\n")
6872
for i, method in enumerate(cls.methodnames):
69-
f.write('%s.expected_output[%r] = """\\\n%s"""\n' % (
70-
cls.__name__, method, results[i+1]))
73+
f.write('_ProfileOutput[%r] = """\\\n%s"""\n' % (
74+
method, results[i+1]))
7175
f.write('\nif __name__ == "__main__":\n main()\n')
7276

7377

@@ -83,7 +87,8 @@ def main():
8387

8488
# Don't remove this comment. Everything below it is auto-generated.
8589
#--cut--------------------------------------------------------------------------
86-
ProfileTest.expected_output['print_stats'] = """\
90+
_ProfileOutput = {}
91+
_ProfileOutput['print_stats'] = """\
8792
28 27.972 0.999 27.972 0.999 profilee.py:110(__getattr__)
8893
1 269.996 269.996 999.769 999.769 profilee.py:25(testfunc)
8994
23/3 149.937 6.519 169.917 56.639 profilee.py:35(factorial)
@@ -93,7 +98,7 @@ def main():
9398
2 -0.006 -0.003 139.946 69.973 profilee.py:84(helper2_indirect)
9499
8 311.976 38.997 399.912 49.989 profilee.py:88(helper2)
95100
8 63.976 7.997 79.960 9.995 profilee.py:98(subhelper)"""
96-
ProfileTest.expected_output['print_callers'] = """\
101+
_ProfileOutput['print_callers'] = """\
97102
:0(append) <- profilee.py:73(helper1)(4) 119.964
98103
:0(exc_info) <- profilee.py:73(helper1)(4) 119.964
99104
:0(hasattr) <- profilee.py:73(helper1)(4) 119.964
@@ -111,7 +116,7 @@ def main():
111116
profilee.py:88(helper2) <- profilee.py:55(helper)(6) 599.830
112117
profilee.py:84(helper2_indirect)(2) 139.946
113118
profilee.py:98(subhelper) <- profilee.py:88(helper2)(8) 399.912"""
114-
ProfileTest.expected_output['print_callees'] = """\
119+
_ProfileOutput['print_callees'] = """\
115120
:0(hasattr) -> profilee.py:110(__getattr__)(12) 27.972
116121
<string>:1(<module>) -> profilee.py:25(testfunc)(1) 999.769
117122
profilee.py:110(__getattr__) ->

0 commit comments

Comments
 (0)