]> granicus.if.org Git - python/commitdiff
Merged revisions 83524,84776 via svnmerge from
authorFlorent Xicluna <florent.xicluna@gmail.com>
Mon, 13 Sep 2010 17:36:36 +0000 (17:36 +0000)
committerFlorent Xicluna <florent.xicluna@gmail.com>
Mon, 13 Sep 2010 17:36:36 +0000 (17:36 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r83524 | georg.brandl | 2010-08-02 14:20:23 +0200 (lun., 02 août 2010) | 1 line

  #9428: fix running scripts from profile/cProfile with their own name and the right namespace.  Same fix as for trace.py in #1690103.
........
  r84776 | florent.xicluna | 2010-09-13 18:35:02 +0200 (lun., 13 sept. 2010) | 1 line

  Make test.regrtest.__file__ absolute, this was not always the case when running profile or trace, for example.  (issue #9323)
........

Lib/cProfile.py
Lib/profile.py
Lib/test/regrtest.py
Misc/NEWS

index 19d58048accd0d295a578831441b00cfbc8409a0..b2efd047d397142e5a8ef69423461e9bb423e86f 100755 (executable)
@@ -36,7 +36,7 @@ def run(statement, filename=None, sort=-1):
             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 @@ def runctx(statement, globals, locals, filename=None):
         if filename is not None:
             prof.dump_stats(filename)
         else:
-            result = prof.print_stats()
+            result = prof.print_stats(sort)
     return result
 
 # Backwards compatibility.
@@ -169,7 +169,8 @@ def main():
     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()
@@ -178,9 +179,17 @@ def main():
     (options, args) = parser.parse_args()
     sys.argv[:] = args
 
-    if (len(sys.argv) > 0):
-        sys.path.insert(0, os.path.dirname(sys.argv[0]))
-        run('execfile(%r)' % (sys.argv[0],), 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,
+        }
+        runctx(code, globs, None, options.outfile, options.sort)
     else:
         parser.print_usage()
     return parser
index 9a4336ec474614fe42159b7897f1f2af2657bdd3..2d1f4864056c213074f641743fa82a992bfdc8bc 100755 (executable)
@@ -75,7 +75,7 @@ def run(statement, filename=None, sort=-1):
     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 @@ def runctx(statement, globals, locals, filename=None):
     if filename is not None:
         prof.dump_stats(filename)
     else:
-        return prof.print_stats()
+        return prof.print_stats(sort)
 
 # Backwards compatibility.
 def help():
@@ -589,18 +589,27 @@ def main():
     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()
-
-    if (len(args) > 0):
-        sys.argv[:] = args
-        sys.path.insert(0, os.path.dirname(sys.argv[0]))
-        run('execfile(%r)' % (sys.argv[0],), options.outfile, options.sort)
+    sys.argv[:] = args
+
+    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,
+        }
+        runctx(code, globs, None, options.outfile, options.sort)
     else:
         parser.print_usage()
     return parser
index cd7bc484a5237c7487ab9b5fb52a69e3fa8394f7..fc0e823b80caf8095f0c6b0c2fdc05f1f08ce157 100755 (executable)
@@ -1501,7 +1501,13 @@ class _ExpectedSkips:
         return self.expected
 
 if __name__ == '__main__':
-    # Simplification for findtestdir().
+    # findtestdir() gets the dirname out of __file__, so we have to make it
+    # absolute before changing the working directory.
+    # For example __file__ may be relative when running trace or profile.
+    # See issue #9323.
+    __file__ = os.path.abspath(__file__)
+
+    # sanity check
     assert __file__ == os.path.abspath(sys.argv[0])
 
     # When tests are run from the Python build directory, it is best practice
index 9efd2513e1f7444f343ddd64810c7221e9ff2b1f..645f0e65dc96b42d51ae9897c932dd50fcef5758 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -151,6 +151,9 @@ Library
 
 - Issue #9354: Provide getsockopt() in asyncore's file_wrapper.
 
+- 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.
 
@@ -340,8 +343,11 @@ Build
 Tests
 -----
 
+- Issue #9323: Make test.regrtest.__file__ absolute, this was not always the
+  case when running profile or trace, for example.
+
 - Issue #9315: Added tests for the trace module.  Patch by Eli Bendersky.
+
 - Strengthen test_unicode with explicit type checking for assertEqual tests.
 
 - Issue #8857: Provide a test case for socket.getaddrinfo.