4040# return i_count
4141#itimes = integer_timer # replace with C coded timer returning integers
4242
43+ class _Utils :
44+ """Support class for utility functions which are shared by
45+ profile.py and cProfile.py modules.
46+ Not supposed to be used directly.
47+ """
48+
49+ def __init__ (self , profiler ):
50+ self .profiler = profiler
51+
52+ def run (self , statement , filename , sort ):
53+ prof = self .profiler ()
54+ try :
55+ prof .run (statement )
56+ except SystemExit :
57+ pass
58+ finally :
59+ self ._show (prof , filename , sort )
60+
61+ def runctx (self , statement , globals , locals , filename , sort ):
62+ prof = self .profiler ()
63+ try :
64+ prof .runctx (statement , globals , locals )
65+ except SystemExit :
66+ pass
67+ finally :
68+ self ._show (prof , filename , sort )
69+
70+ def _show (self , prof , filename , sort ):
71+ if filename is not None :
72+ prof .dump_stats (filename )
73+ else :
74+ prof .print_stats (sort )
75+
76+
4377#**************************************************************************
4478# The following are the static member functions for the profiler class
4579# Note that an instance of Profile() is *not* needed to call them.
@@ -56,32 +90,16 @@ def run(statement, filename=None, sort=-1):
5690 standard name string (file/line/function-name) that is presented in
5791 each line.
5892 """
59- prof = Profile ()
60- try :
61- prof = prof .run (statement )
62- except SystemExit :
63- pass
64- if filename is not None :
65- prof .dump_stats (filename )
66- else :
67- return prof .print_stats (sort )
93+ return _Utils (Profile ).run (statement , filename , sort )
6894
6995def runctx (statement , globals , locals , filename = None , sort = - 1 ):
7096 """Run statement under profiler, supplying your own globals and locals,
7197 optionally saving results in filename.
7298
7399 statement and filename have the same semantics as profile.run
74100 """
75- prof = Profile ()
76- try :
77- prof = prof .runctx (statement , globals , locals )
78- except SystemExit :
79- pass
80-
81- if filename is not None :
82- prof .dump_stats (filename )
83- else :
84- return prof .print_stats (sort )
101+ return _Utils (Profile ).runctx (statement , globals , locals , filename , sort )
102+
85103
86104class Profile :
87105 """Profiler class.
0 commit comments