bpo-40492: Fix --outfile with relative path when the program changes it working dir (GH-19910)
(cherry picked from commit 3c0ac18504
)
This commit is contained in:
parent
e5c7ac7a22
commit
7c949020ef
|
@ -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__')"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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.
|
Loading…
Reference in New Issue