summaryrefslogtreecommitdiffstats
path: root/Lib/profile.py
diff options
context:
space:
mode:
authorGiampaolo Rodola' <g.rodola@gmail.com>2013-02-25 10:36:40 (GMT)
committerGiampaolo Rodola' <g.rodola@gmail.com>2013-02-25 10:36:40 (GMT)
commitfca677a26aff3bce34b1b9b7662dd3a95cd285ba (patch)
treebb1029606f2410b0ee54231ff506bf3844f5ee11 /Lib/profile.py
parentd7c59e101f99cc06f524f48063ce96403c7a6c30 (diff)
downloadcpython-fca677a26aff3bce34b1b9b7662dd3a95cd285ba.zip
cpython-fca677a26aff3bce34b1b9b7662dd3a95cd285ba.tar.gz
cpython-fca677a26aff3bce34b1b9b7662dd3a95cd285ba.tar.bz2
Fix #17197: profile/cProfile modules refactored so that code of run() and runctx() utility functions is not duplicated in both modules.
Diffstat (limited to 'Lib/profile.py')
-rwxr-xr-xLib/profile.py56
1 files changed, 37 insertions, 19 deletions
diff --git a/Lib/profile.py b/Lib/profile.py
index 553f210..5d0e968 100755
--- a/Lib/profile.py
+++ b/Lib/profile.py
@@ -40,6 +40,40 @@ __all__ = ["run", "runctx", "Profile"]
# return i_count
#itimes = integer_timer # replace with C coded timer returning integers
+class _Utils:
+ """Support class for utility functions which are shared by
+ profile.py and cProfile.py modules.
+ Not supposed to be used directly.
+ """
+
+ def __init__(self, profiler):
+ self.profiler = profiler
+
+ def run(self, statement, filename, sort):
+ prof = self.profiler()
+ try:
+ prof.run(statement)
+ except SystemExit:
+ pass
+ finally:
+ self._show(prof, filename, sort)
+
+ def runctx(self, statement, globals, locals, filename, sort):
+ prof = self.profiler()
+ try:
+ prof.runctx(statement, globals, locals)
+ except SystemExit:
+ pass
+ finally:
+ self._show(prof, filename, sort)
+
+ def _show(self, prof, filename, sort):
+ if filename is not None:
+ prof.dump_stats(filename)
+ else:
+ prof.print_stats(sort)
+
+
#**************************************************************************
# The following are the static member functions for the profiler class
# Note that an instance of Profile() is *not* needed to call them.
@@ -56,15 +90,7 @@ def run(statement, filename=None, sort=-1):
standard name string (file/line/function-name) that is presented in
each line.
"""
- prof = Profile()
- try:
- prof = prof.run(statement)
- except SystemExit:
- pass
- if filename is not None:
- prof.dump_stats(filename)
- else:
- return prof.print_stats(sort)
+ return _Utils(Profile).run(statement, filename, sort)
def runctx(statement, globals, locals, filename=None, sort=-1):
"""Run statement under profiler, supplying your own globals and locals,
@@ -72,16 +98,8 @@ def runctx(statement, globals, locals, filename=None, sort=-1):
statement and filename have the same semantics as profile.run
"""
- prof = Profile()
- try:
- prof = prof.runctx(statement, globals, locals)
- except SystemExit:
- pass
-
- if filename is not None:
- prof.dump_stats(filename)
- else:
- return prof.print_stats(sort)
+ return _Utils(Profile).runctx(statement, globals, locals, filename, sort)
+
class Profile:
"""Profiler class.