[IMP] simplify @profile implementation
authorXavier Morel <xmo@openerp.com>
Wed, 5 Dec 2012 16:08:47 +0000 (17:08 +0100)
committerXavier Morel <xmo@openerp.com>
Wed, 5 Dec 2012 16:08:47 +0000 (17:08 +0100)
bzr revid: xmo@openerp.com-20121205160847-8zc8of4grueusbl2

openerp/tools/misc.py

index 4e3db23..209e4aa 100644 (file)
@@ -27,6 +27,7 @@ Miscellaneous tools used by OpenERP.
 """
 
 from functools import wraps
+import cProfile
 import subprocess
 import logging
 import os
@@ -592,16 +593,10 @@ class profile(object):
     def __call__(self, f):
         @wraps(f)
         def wrapper(*args, **kwargs):
-            class profile_wrapper(object):
-                def __init__(self):
-                    self.result = None
-                def __call__(self):
-                    self.result = f(*args, **kwargs)
-            pw = profile_wrapper()
-            import cProfile
-            fname = self.fname or ("%s.cprof" % (f.func_name,))
-            cProfile.runctx('pw()', globals(), locals(), filename=fname)
-            return pw.result
+            profile = cProfile.Profile()
+            result = profile.runcall(f, *args, **kwargs)
+            profile.dump_stats(self.fname or ("%s.cprof" % (f.func_name,)))
+            return result
 
         return wrapper