[Python-checkins] r83524 - in python/branches/py3k: Lib/cProfile.py Lib/profile.py Misc/NEWS

georg.brandl python-checkins at python.org
Mon Aug 2 14:20:23 CEST 2010


Author: georg.brandl
Date: Mon Aug  2 14:20:23 2010
New Revision: 83524

Log:
#9428: fix running scripts from profile/cProfile with their own name and the right namespace.  Same fix as for trace.py in #1690103.

Modified:
   python/branches/py3k/Lib/cProfile.py
   python/branches/py3k/Lib/profile.py
   python/branches/py3k/Misc/NEWS

Modified: python/branches/py3k/Lib/cProfile.py
==============================================================================
--- python/branches/py3k/Lib/cProfile.py	(original)
+++ python/branches/py3k/Lib/cProfile.py	Mon Aug  2 14:20:23 2010
@@ -36,7 +36,7 @@
             result = prof.print_stats(sort)
     return result
 
-def runctx(statement, globals, locals, filename=None):
+def runctx(statement, globals, locals, filename=None, sort=-1):
     """Run statement under profiler, supplying your own globals and locals,
     optionally saving results in filename.
 
@@ -53,7 +53,7 @@
         if filename is not None:
             prof.dump_stats(filename)
         else:
-            result = prof.print_stats()
+            result = prof.print_stats(sort)
     return result
 
 # ____________________________________________________________
@@ -164,7 +164,8 @@
     parser.add_option('-o', '--outfile', dest="outfile",
         help="Save stats to <outfile>", default=None)
     parser.add_option('-s', '--sort', dest="sort",
-        help="Sort order when printing to stdout, based on pstats.Stats class", default=-1)
+        help="Sort order when printing to stdout, based on pstats.Stats class",
+        default=-1)
 
     if not sys.argv[1:]:
         parser.print_usage()
@@ -173,14 +174,18 @@
     (options, args) = parser.parse_args()
     sys.argv[:] = args
 
-    if (len(sys.argv) > 0):
-        sys.path.insert(0, os.path.dirname(sys.argv[0]))
-        fp = open(sys.argv[0])
-        try:
-            script = fp.read()
-        finally:
-            fp.close()
-        run('exec(%r)' % script, options.outfile, options.sort)
+    if len(args) > 0:
+        progname = args[0]
+        sys.path.insert(0, os.path.dirname(progname))
+        with open(progname, 'rb') as fp:
+            code = compile(fp.read(), progname, 'exec')
+        globs = {
+            '__file__': progname,
+            '__name__': '__main__',
+            '__package__': None,
+            '__cached__': None,
+        }
+        runctx(code, globs, None, options.outfile, options.sort)
     else:
         parser.print_usage()
     return parser

Modified: python/branches/py3k/Lib/profile.py
==============================================================================
--- python/branches/py3k/Lib/profile.py	(original)
+++ python/branches/py3k/Lib/profile.py	Mon Aug  2 14:20:23 2010
@@ -75,7 +75,7 @@
     else:
         return prof.print_stats(sort)
 
-def runctx(statement, globals, locals, filename=None):
+def runctx(statement, globals, locals, filename=None, sort=-1):
     """Run statement under profiler, supplying your own globals and locals,
     optionally saving results in filename.
 
@@ -90,7 +90,7 @@
     if filename is not None:
         prof.dump_stats(filename)
     else:
-        return prof.print_stats()
+        return prof.print_stats(sort)
 
 if hasattr(os, "times"):
     def _get_time_times(timer=os.times):
@@ -582,20 +582,28 @@
     parser.add_option('-o', '--outfile', dest="outfile",
         help="Save stats to <outfile>", default=None)
     parser.add_option('-s', '--sort', dest="sort",
-        help="Sort order when printing to stdout, based on pstats.Stats class", default=-1)
+        help="Sort order when printing to stdout, based on pstats.Stats class",
+        default=-1)
 
     if not sys.argv[1:]:
         parser.print_usage()
         sys.exit(2)
 
     (options, args) = parser.parse_args()
+    sys.argv[:] = args
 
-    if (len(args) > 0):
-        sys.argv[:] = args
-        sys.path.insert(0, os.path.dirname(sys.argv[0]))
-        with open(sys.argv[0], 'rb') as fp:
-            script = fp.read()
-        run('exec(%r)' % script, options.outfile, options.sort)
+    if len(args) > 0:
+        progname = args[0]
+        sys.path.insert(0, os.path.dirname(progname))
+        with open(progname, 'rb') as fp:
+            code = compile(fp.read(), progname, 'exec')
+        globs = {
+            '__file__': progname,
+            '__name__': '__main__',
+            '__package__': None,
+            '__cached__': None,
+        }
+        runctx(code, globs, None, options.outfile, options.sort)
     else:
         parser.print_usage()
     return parser

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Mon Aug  2 14:20:23 2010
@@ -29,6 +29,9 @@
 Library
 -------
 
+- Issue #9428: Fix running scripts with the profile/cProfile modules from
+  the command line.
+
 - Issue #7781: Fix restricting stats by entry counts in the pstats
   interactive browser.
 


More information about the Python-checkins mailing list