From 338e784774c025ae3175050944665b9d164990f4 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 4 May 2020 12:16:20 -0700 Subject: [PATCH 1/2] Fix `--outfile` when the program being profiled changes the working directory --- Lib/cProfile.py | 5 +++++ Lib/profile.py | 5 +++++ Lib/test/test_profile.py | 16 +++++++++++++++- .../2020-05-04-12-16-00.bpo-40492.ONk9Na.rst | 2 ++ 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2020-05-04-12-16-00.bpo-40492.ONk9Na.rst diff --git a/Lib/cProfile.py b/Lib/cProfile.py index 4f202038d61260..8d347988666858 100755 --- a/Lib/cProfile.py +++ b/Lib/cProfile.py @@ -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__')" diff --git a/Lib/profile.py b/Lib/profile.py index aad458dc951f41..3124d04fe62f6f 100755 --- a/Lib/profile.py +++ b/Lib/profile.py @@ -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 diff --git a/Lib/test/test_profile.py b/Lib/test/test_profile.py index 738be85bedf3c9..1bdf30acbb54b2 100644 --- a/Lib/test/test_profile.py +++ b/Lib/test/test_profile.py @@ -7,7 +7,7 @@ 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 @@ def test_run_profile_as_module(self): 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 index 00000000000000..6ed1575b7a9a27 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-05-04-12-16-00.bpo-40492.ONk9Na.rst @@ -0,0 +1,2 @@ +Fix ``--outfile`` for :mod:`cProfile` / :mod:`profile` when the program being +profiled changes the working directory - by Anthony Sottile. From 9672baf1e9f58dce3e530eb05cdd36bc4c4c87fb Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 18 Oct 2020 11:06:02 -0700 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Tal Einat --- Lib/cProfile.py | 4 ++-- Lib/profile.py | 4 ++-- .../next/Library/2020-05-04-12-16-00.bpo-40492.ONk9Na.rst | 5 +++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Lib/cProfile.py b/Lib/cProfile.py index 8d347988666858..59b4699feb5062 100755 --- a/Lib/cProfile.py +++ b/Lib/cProfile.py @@ -152,8 +152,8 @@ 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 + # 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) diff --git a/Lib/profile.py b/Lib/profile.py index 3124d04fe62f6f..5cb017ed830099 100755 --- a/Lib/profile.py +++ b/Lib/profile.py @@ -571,8 +571,8 @@ 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 + # 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) 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 index 6ed1575b7a9a27..86bc08c79e21e2 100644 --- 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 @@ -1,2 +1,3 @@ -Fix ``--outfile`` for :mod:`cProfile` / :mod:`profile` when the program being -profiled changes the working directory - by Anthony Sottile. +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.