File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import pdb
22import sys
33import warnings
4+ from contextlib import contextmanager
45from functools import wraps
56
67from .code_map import CodeMap
@@ -48,27 +49,29 @@ def add_function(self, func):
4849 else :
4950 self .code_map .add (code )
5051
52+ @contextmanager
53+ def count_ctxmgr (self ):
54+ self .enable_by_count ()
55+ try :
56+ yield
57+ finally :
58+ self .disable_by_count ()
59+
5160 def wrap_function (self , func ):
5261 """ Wrap a function to profile it.
5362 """
5463
5564 def f (* args , ** kwds ):
56- self .enable_by_count ()
57- try :
65+ with self .count_ctxmgr ():
5866 return func (* args , ** kwds )
59- finally :
60- self .disable_by_count ()
6167
6268 return f
6369
6470 def runctx (self , cmd , globals , locals ):
6571 """ Profile a single executable statement in the given namespaces.
6672 """
67- self .enable_by_count ()
68- try :
73+ with self .count_ctxmgr ():
6974 exec (cmd , globals , locals )
70- finally :
71- self .disable_by_count ()
7275 return self
7376
7477 def enable_by_count (self ):
You can’t perform that action at this time.
0 commit comments