Skip to content

Commit 3277076

Browse files
committed
Move stuff to common module
1 parent b2ec363 commit 3277076

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

Lib/profiling/sampling/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
call stack rather than tracing every function call.
55
"""
66

7-
from profiling.collector import Collector
8-
from profiling.pstats_collector import PstatsCollector
9-
from profiling.stack_collector import CollapsedStackCollector
7+
from profiling.sampling.collector import Collector
8+
from profiling.sampling.pstats_collector import PstatsCollector
9+
from profiling.sampling.stack_collector import CollapsedStackCollector
1010

1111
__all__ = ("Collector", "PstatsCollector", "CollapsedStackCollector")

Lib/profiling/tracing/__init__.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,32 @@
1010
import importlib.machinery
1111
import importlib.util
1212
import io
13-
import profile as _pyprofile
13+
from profiling.tracing._utils import _Utils
1414

1515
# ____________________________________________________________
1616
# Simple interface
1717

1818
def run(statement, filename=None, sort=-1):
19-
return _pyprofile._Utils(Profile).run(statement, filename, sort)
19+
"""Run statement under profiler optionally saving results in filename
20+
21+
This function takes a single argument that can be passed to the
22+
"exec" statement, and an optional file name. In all cases this
23+
routine attempts to "exec" its first argument and gather profiling
24+
statistics from the execution. If no file name is present, then this
25+
function automatically prints a simple profiling report, sorted by the
26+
standard name string (file/line/function-name) that is presented in
27+
each line.
28+
"""
29+
return _Utils(Profile).run(statement, filename, sort)
2030

2131
def runctx(statement, globals, locals, filename=None, sort=-1):
22-
return _pyprofile._Utils(Profile).runctx(statement, globals, locals,
23-
filename, sort)
32+
"""Run statement under profiler, supplying your own globals and locals,
33+
optionally saving results in filename.
2434
25-
run.__doc__ = _pyprofile.run.__doc__
26-
runctx.__doc__ = _pyprofile.runctx.__doc__
35+
statement and filename have the same semantics as profile.run
36+
"""
37+
return _Utils(Profile).runctx(statement, globals, locals,
38+
filename, sort)
2739

2840
# ____________________________________________________________
2941

Lib/profiling/tracing/_utils.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class _Utils:
2+
"""Support class for utility functions which are shared by
3+
profile.py and cProfile.py modules.
4+
Not supposed to be used directly.
5+
"""
6+
7+
def __init__(self, profiler):
8+
self.profiler = profiler
9+
10+
def run(self, statement, filename, sort):
11+
prof = self.profiler()
12+
try:
13+
prof.run(statement)
14+
except SystemExit:
15+
pass
16+
finally:
17+
self._show(prof, filename, sort)
18+
19+
def runctx(self, statement, globals, locals, filename, sort):
20+
prof = self.profiler()
21+
try:
22+
prof.runctx(statement, globals, locals)
23+
except SystemExit:
24+
pass
25+
finally:
26+
self._show(prof, filename, sort)
27+
28+
def _show(self, prof, filename, sort):
29+
if filename is not None:
30+
prof.dump_stats(filename)
31+
else:
32+
prof.print_stats(sort)

0 commit comments

Comments
 (0)