11#!/usr/bin/env python
22# -*- coding: UTF-8 -*-
3- from __future__ import with_statement
43try :
54 import cPickle as pickle
65except ImportError :
2322# ===========================================================
2423PY3 = sys .version_info [0 ] == 3
2524
26- # next:
27- try :
28- next_ = next
29- except NameError :
30- next_ = lambda obj : obj .next ()
31-
3225# exec (from https://bitbucket.org/gutworth/six/):
3326if PY3 :
3427 import builtins
@@ -53,36 +46,10 @@ def exec_(_code_, _globs_=None, _locs_=None):
5346def is_generator (f ):
5447 """ Return True if a function is a generator.
5548 """
56- func_code = f .__code__ if PY3 else f . func_code
49+ func_code = f .__code__
5750 isgen = (func_code .co_flags & CO_GENERATOR ) != 0
5851 return isgen
5952
60- # Code to exec inside of LineProfiler.__call__ to support PEP-342-style
61- # generators in Python 2.5+.
62- pep342_gen_wrapper = '''
63- def wrap_generator(self, func):
64- """ Wrap a generator to profile it.
65- """
66- def f(*args, **kwds):
67- g = func(*args, **kwds)
68- # The first iterate will not be a .send()
69- self.enable_by_count()
70- try:
71- item = next_(g)
72- finally:
73- self.disable_by_count()
74- input = (yield item)
75- # But any following one might be.
76- while True:
77- self.enable_by_count()
78- try:
79- item = g.send(input)
80- finally:
81- self.disable_by_count()
82- input = (yield item)
83- return f
84- '''
85-
8653class LineProfiler (CLineProfiler ):
8754 """ A profiler that records the execution times of individual lines.
8855 """
@@ -102,24 +69,27 @@ def __call__(self, func):
10269 f .__dict__ .update (getattr (func , '__dict__' , {}))
10370 return f
10471
105- if sys .version_info [:2 ] >= (2 ,5 ):
106- # Delay compilation because the syntax is not compatible with older
107- # Python versions.
108- exec_ (pep342_gen_wrapper )
109- else :
110- def wrap_generator (self , func ):
111- """ Wrap a generator to profile it.
112- """
113- def f (* args , ** kwds ):
114- g = func (* args , ** kwds )
115- while True :
116- self .enable_by_count ()
117- try :
118- item = next_ (g )
119- finally :
120- self .disable_by_count ()
121- yield item
122- return f
72+ def wrap_generator (self , func ):
73+ """ Wrap a generator to profile it.
74+ """
75+ def f (* args , ** kwds ):
76+ g = func (* args , ** kwds )
77+ # The first iterate will not be a .send()
78+ self .enable_by_count ()
79+ try :
80+ item = next (g )
81+ finally :
82+ self .disable_by_count ()
83+ input = (yield item )
84+ # But any following one might be.
85+ while True :
86+ self .enable_by_count ()
87+ try :
88+ item = g .send (input )
89+ finally :
90+ self .disable_by_count ()
91+ input = (yield item )
92+ return f
12393
12494 def wrap_function (self , func ):
12595 """ Wrap a function to profile it.
@@ -138,11 +108,8 @@ def dump_stats(self, filename):
138108 object from `get_stats()`.
139109 """
140110 lstats = self .get_stats ()
141- f = open (filename , 'wb' )
142- try :
111+ with open (filename , 'wb' ) as f :
143112 pickle .dump (lstats , f , pickle .HIGHEST_PROTOCOL )
144- finally :
145- f .close ()
146113
147114 def print_stats (self , stream = None ):
148115 """ Show the gathered statistics.
@@ -298,10 +265,7 @@ def magic_lprun(self, parameter_s=''):
298265 for name in opts .f :
299266 try :
300267 funcs .append (eval (name , global_ns , local_ns ))
301- except Exception :
302- # "except Exception as e" is not supported in Python 2.5
303- # so we're using a hack to get the exception
304- e = sys .exc_info ()[1 ]
268+ except Exception as e :
305269 raise UsageError ('Could not find function %r.\n %s: %s' % (name ,
306270 e .__class__ .__name__ , e ))
307271
0 commit comments