]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40492: Fix --outfile with relative path when the program changes it working dir...
authorAnthony Sottile <asottile@umich.edu>
Sun, 18 Oct 2020 20:48:31 +0000 (13:48 -0700)
committerGitHub <noreply@github.com>
Sun, 18 Oct 2020 20:48:31 +0000 (23:48 +0300)
Lib/cProfile.py
Lib/profile.py
Lib/test/test_profile.py
Misc/NEWS.d/next/Library/2020-05-04-12-16-00.bpo-40492.ONk9Na.rst [new file with mode: 0644]

index 4f202038d61260a250138c08d319c1cd499bf00e..59b4699feb5062aef3fef5110be0acf8d7ae4e61 100755 (executable)
@@ -152,6 +152,11 @@ def main():
     (options, args) = parser.parse_args()
     sys.argv[:] = args
 
+    # The script that we're profiling may chdir, so capture the absolute path
+    # to the output file at startup.
+    if options.outfile is not None:
+        options.outfile = os.path.abspath(options.outfile)
+
     if len(args) > 0:
         if options.module:
             code = "run_module(modname, run_name='__main__')"
index aad458dc951f4113c723cfb1dbd678f59817ee32..5cb017ed8300993ad9767db967e105926e2f0159 100755 (executable)
@@ -571,6 +571,11 @@ def main():
     (options, args) = parser.parse_args()
     sys.argv[:] = args
 
+    # The script that we're profiling may chdir, so capture the absolute path
+    # to the output file at startup.
+    if options.outfile is not None:
+        options.outfile = os.path.abspath(options.outfile)
+
     if len(args) > 0:
         if options.module:
             import runpy
index 738be85bedf3c9443945582f22d994bb3cc6d983..1bdf30acbb54b23b0c3abcd119d44d2c413f0446 100644 (file)
@@ -7,7 +7,7 @@ import os
 from difflib import unified_diff
 from io import StringIO
 from test.support import run_unittest
-from test.support.os_helper import TESTFN, unlink
+from test.support.os_helper import TESTFN, unlink, temp_dir, change_cwd
 from contextlib import contextmanager
 
 import profile
@@ -112,6 +112,20 @@ class ProfileTest(unittest.TestCase):
         assert_python_ok('-m', self.profilermodule.__name__,
                          '-m', 'timeit', '-n', '1')
 
+    def test_output_file_when_changing_directory(self):
+        with temp_dir() as tmpdir, change_cwd(tmpdir):
+            os.mkdir('dest')
+            with open('demo.py', 'w') as f:
+                f.write('import os; os.chdir("dest")')
+
+            assert_python_ok(
+                '-m', self.profilermodule.__name__,
+                '-o', 'out.pstats',
+                'demo.py',
+            )
+
+            self.assertTrue(os.path.exists('out.pstats'))
+
 
 def regenerate_expected_output(filename, cls):
     filename = filename.rstrip('co')
diff --git a/Misc/NEWS.d/next/Library/2020-05-04-12-16-00.bpo-40492.ONk9Na.rst b/Misc/NEWS.d/next/Library/2020-05-04-12-16-00.bpo-40492.ONk9Na.rst
new file mode 100644 (file)
index 0000000..86bc08c
--- /dev/null
@@ -0,0 +1,3 @@
+Fix ``--outfile`` for :mod:`cProfile` / :mod:`profile` not writing the output
+file in the original directory when the program being profiled changes the
+working directory.  PR by Anthony Sottile.