]> 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 21:14:18 +0000 (14:14 -0700)
committerGitHub <noreply@github.com>
Sun, 18 Oct 2020 21:14:18 +0000 (00:14 +0300)
(cherry picked from commit 3c0ac18504cfeed822439024339d5717f42bdd66)

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 369d02e22e24aa6a582c765ec36b4aac0892001b..47aacf9e2d494e680e91019fa129ec7ae5e071d4 100755 (executable)
@@ -168,6 +168,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 1346297c04a5931d133fe305b48ca7dfde89b51d..9df4435c5ae8374ca8f37326efefa400b0f00608 100755 (executable)
@@ -587,6 +587,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 01a8a6eaf5a23cc9e95e821b1cc044d0dbbfa290..233649899ec20c0cf1a25fc4d4b1c9eff6d9f86e 100644 (file)
@@ -6,7 +6,7 @@ import unittest
 import os
 from difflib import unified_diff
 from io import StringIO
-from test.support import TESTFN, run_unittest, unlink
+from test.support import TESTFN, run_unittest, unlink, temp_dir, change_cwd
 from contextlib import contextmanager
 
 import profile
@@ -111,6 +111,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.