disable raise ... from ... unittest in py3 (but other syntax checks still work)
[cython.git] / tests / run / pstats_profile_test.pyx
1 # cython: profile = True
2
3 __doc__ = u"""
4     >>> import os, tempfile, cProfile as profile, pstats
5     >>> statsfile = tempfile.mkstemp()[1]
6     >>> profile.runctx("test_profile(100)", locals(), globals(), statsfile)
7     >>> s = pstats.Stats(statsfile)
8     >>> short_stats = dict([(k[2], v[1]) for k,v in s.stats.items()])
9     >>> short_stats['f_def']
10     100
11     >>> short_stats['f_cdef']
12     100
13     >>> short_stats['f_inline']
14     100
15     >>> short_stats['f_inline_prof']
16     100
17     >>> short_stats['f_noprof']
18     Traceback (most recent call last):
19     ...
20     KeyError: 'f_noprof'
21     >>> short_stats['f_raise']
22     100
23     >>> try:
24     ...    os.unlink(statsfile)
25     ... except:
26     ...    pass
27 """
28
29 import sys
30 if sys.version_info < (2,5):
31     # disable in earlier versions
32     __doc__ = """
33 >>> # nothing to test here ...
34 """
35
36 cimport cython
37
38 def test_profile(long N):
39     cdef long i, n = 0
40     for i from 0 <= i < N:
41         n += f_def(i)
42         n += f_cdef(i)
43         n += f_inline(i)
44         n += f_inline_prof(i)
45         n += f_noprof(i)
46         try:
47             n += f_raise(i+2)
48         except RuntimeError:
49             pass
50     return n
51
52 def f_def(long a):
53     return a
54
55 cdef long f_cdef(long a):
56     return a
57
58 cdef inline long f_inline(long a):
59     return a
60
61 @cython.profile(True)
62 cdef inline long f_inline_prof(long a):
63     return a
64
65 @cython.profile(False)
66 cdef int f_noprof(long a):
67     return a
68
69 cdef long f_raise(long) except -2:
70     raise RuntimeError