summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorNicholas Bastin <nick.bastin@gmail.com>2004-03-22 20:12:56 (GMT)
committerNicholas Bastin <nick.bastin@gmail.com>2004-03-22 20:12:56 (GMT)
commit1eb4bfc6579133c2d7a495141b1f754dfecf0fc6 (patch)
treeebdd1801beda401932abd45966cc052e461d74fc /Lib
parent706933821c88508299a292e8bb20bf22a9f6364b (diff)
downloadcpython-1eb4bfc6579133c2d7a495141b1f754dfecf0fc6.zip
cpython-1eb4bfc6579133c2d7a495141b1f754dfecf0fc6.tar.gz
cpython-1eb4bfc6579133c2d7a495141b1f754dfecf0fc6.tar.bz2
Added global runctx function to profile to fix SF Bug #716587
Diffstat (limited to 'Lib')
-rwxr-xr-xLib/profile.py17
-rw-r--r--Lib/test/output/test_profile12
-rw-r--r--Lib/test/test_profile.py14
3 files changed, 37 insertions, 6 deletions
diff --git a/Lib/profile.py b/Lib/profile.py
index 2dc6e87..1a4ff67 100755
--- a/Lib/profile.py
+++ b/Lib/profile.py
@@ -76,6 +76,23 @@ def run(statement, filename=None):
else:
return prof.print_stats()
+def runctx(statement, globals, locals, filename=None):
+ """Run statement under profiler, supplying your own globals and locals,
+ optionally saving results in filename.
+
+ 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()
+
# print help
def help():
for dirname in sys.path:
diff --git a/Lib/test/output/test_profile b/Lib/test/output/test_profile
index b36a016..917a18e 100644
--- a/Lib/test/output/test_profile
+++ b/Lib/test/output/test_profile
@@ -7,11 +7,11 @@ test_profile
1 0.000 0.000 1.000 1.000 <string>:1(?)
0 0.000 0.000 profile:0(profiler)
1 0.000 0.000 1.000 1.000 profile:0(testfunc())
- 1 0.400 0.400 1.000 1.000 test_profile.py:21(testfunc)
- 2 0.080 0.040 0.600 0.300 test_profile.py:30(helper)
- 4 0.116 0.029 0.120 0.030 test_profile.py:48(helper1)
- 8 0.312 0.039 0.400 0.050 test_profile.py:56(helper2)
- 8 0.064 0.008 0.080 0.010 test_profile.py:66(subhelper)
- 28 0.028 0.001 0.028 0.001 test_profile.py:78(__getattr__)
+ 1 0.400 0.400 1.000 1.000 test_profile.py:23(testfunc)
+ 2 0.080 0.040 0.600 0.300 test_profile.py:32(helper)
+ 4 0.116 0.029 0.120 0.030 test_profile.py:50(helper1)
+ 8 0.312 0.039 0.400 0.050 test_profile.py:58(helper2)
+ 8 0.064 0.008 0.080 0.010 test_profile.py:68(subhelper)
+ 28 0.028 0.001 0.028 0.001 test_profile.py:80(__getattr__)
diff --git a/Lib/test/test_profile.py b/Lib/test/test_profile.py
index 6ff5811..1b1f50b 100644
--- a/Lib/test/test_profile.py
+++ b/Lib/test/test_profile.py
@@ -1,6 +1,8 @@
"""Test suite for the profile module."""
import profile
+import os
+from test.test_support import TESTFN, vereq
# In order to have reproducible time, we simulate a timer in the global
# variable 'ticks', which represents simulated time in milliseconds.
@@ -82,5 +84,17 @@ class C:
ticks += 1
raise AttributeError
+
+def test_2():
+ d = globals().copy()
+ def testfunc():
+ global x
+ x = 1
+ d['testfunc'] = testfunc
+ profile.runctx("testfunc()", d, d, TESTFN)
+ vereq (x, 1)
+ os.unlink (TESTFN)
+
if __name__ == "__main__":
test_main()
+ test_2()