33""" Script to conveniently run profilers on code in a variety of circumstances.
44"""
55
6+ import functools
67import optparse
78import os
89import sys
@@ -75,21 +76,18 @@ def __call__(self, func):
7576 # FIXME: refactor this into a utility function so that both it and
7677 # line_profiler can use it.
7778 if is_generator (func ):
78- f = self .wrap_generator (func )
79+ wrapper = self .wrap_generator (func )
7980 else :
80- f = self .wrap_function (func )
81- f .__module__ = func .__module__
82- f .__name__ = func .__name__
83- f .__doc__ = func .__doc__
84- f .__dict__ .update (getattr (func , '__dict__' , {}))
85- return f
81+ wrapper = self .wrap_function (func )
82+ return wrapper
8683
8784 # FIXME: refactor this stuff so that both LineProfiler and
8885 # ContextualProfile can use the same implementation.
8986 def wrap_generator (self , func ):
9087 """ Wrap a generator to profile it.
9188 """
92- def f (* args , ** kwds ):
89+ @functools .wraps (func )
90+ def wrapper (* args , ** kwds ):
9391 g = func (* args , ** kwds )
9492 # The first iterate will not be a .send()
9593 self .enable_by_count ()
@@ -106,19 +104,20 @@ def f(*args, **kwds):
106104 finally :
107105 self .disable_by_count ()
108106 input = (yield item )
109- return f
107+ return wrapper
110108
111109 def wrap_function (self , func ):
112110 """ Wrap a function to profile it.
113111 """
114- def f (* args , ** kwds ):
112+ @functools .wraps (func )
113+ def wrapper (* args , ** kwds ):
115114 self .enable_by_count ()
116115 try :
117116 result = func (* args , ** kwds )
118117 finally :
119118 self .disable_by_count ()
120119 return result
121- return f
120+ return wrapper
122121
123122 def __enter__ (self ):
124123 self .enable_by_count ()
0 commit comments