This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: -m cProfile -o f.pstats with a script that does chdir() writes to the changed directory and not `.`
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Anthony Sottile, serhiy.storchaka, taleinat
Priority: normal Keywords: patch

Created on 2020-05-04 03:36 by Anthony Sottile, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 19910 merged Anthony Sottile, 2020-05-04 19:18
PR 22752 merged Anthony Sottile, 2020-10-18 20:54
PR 22753 closed taleinat, 2020-10-18 20:54
PR 22754 closed taleinat, 2020-10-18 20:56
PR 22755 merged Anthony Sottile, 2020-10-18 20:57
Messages (9)
msg368008 - (view) Author: Anthony Sottile (Anthony Sottile) * Date: 2020-05-04 03:36
just spent quite a bit of time debugging this, here's a minimal example:

```bash
# rm -rf a (if you want to reset)
cd /tmp
mkdir -p a/b
echo 'import os; os.chdir("..")' > a/b/c.py
cd a/b
python3 -m cProfile -o log.pstats c.py
ls -al
ls -al ..
```

example output:

```console
$ cd /tmp
$ mkdir -p a/b
$ echo 'import os; os.chdir("..")' > a/b/c.py
$ cd a/b
$ python3 -m cProfile -o log.pstats c.py
$ ls -al
total 12
drwxr-xr-x 2 asottile asottile 4096 May  3 20:35 .
drwxr-xr-x 3 asottile asottile 4096 May  3 20:35 ..
-rw-r--r-- 1 asottile asottile   26 May  3 20:35 c.py
$ ls -al ..
total 16
drwxr-xr-x  3 asottile asottile 4096 May  3 20:35 .
drwxrwxrwt 28 root     root     4096 May  3 20:35 ..
drwxr-xr-x  2 asottile asottile 4096 May  3 20:35 b
-rw-r--r--  1 asottile asottile  395 May  3 20:35 log.pstats
```

happy to work on a patch if this seems like a good idea to fix
msg368018 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-05-04 08:28
I think it is worth to fix and it should be not difficult to fix. Either make the output file name absolute before executing the script, or open the output file before executing the script (what is easier). Unless I miss something.
msg368918 - (view) Author: Anthony Sottile (Anthony Sottile) * Date: 2020-05-15 02:47
@serhiy: this was pretty straightfowrard to fix as you suspected -- would you be able to review it?
msg368930 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-05-15 07:43
I am not sure what is better: to fix it at high level, in main() for cProfile and cProfile, or at low level, in methods runctx() and run() of profile._Utils.
msg378851 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2020-10-18 06:27
> I am not sure what is better: to fix it at high level, in main() for cProfile and cProfile, or at low level, in methods runctx() and run() of profile._Utils.

Maybe both, to keep them decoupled and be sure of correct behavior in all cases?
msg378895 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2020-10-18 20:48
New changeset 3c0ac18504cfeed822439024339d5717f42bdd66 by Anthony Sottile in branch 'master':
bpo-40492: Fix --outfile with relative path when the program changes it working dir (GH-19910)
https://github.com/python/cpython/commit/3c0ac18504cfeed822439024339d5717f42bdd66
msg378897 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2020-10-18 20:59
Many thanks for the report and the PR, Anthony!

Serhiy, do you think it is worth also calling abspath in the profile._Utils as you mentioned, for cases where those may be called directly?
msg378900 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2020-10-18 21:14
New changeset 1c5a65723e623be32e246f58b8797a263f616295 by Anthony Sottile in branch '3.8':
bpo-40492: Fix --outfile with relative path when the program changes it working dir (GH-19910)
https://github.com/python/cpython/commit/1c5a65723e623be32e246f58b8797a263f616295
msg378901 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2020-10-18 21:16
New changeset 7c949020ef2520d7a7cbc978f0b34423e6c2a94c by Anthony Sottile in branch '3.9':
bpo-40492: Fix --outfile with relative path when the program changes it working dir (GH-19910)
https://github.com/python/cpython/commit/7c949020ef2520d7a7cbc978f0b34423e6c2a94c
History
Date User Action Args
2022-04-11 14:59:30adminsetgithub: 84672
2020-10-18 21:16:27taleinatsetmessages: + msg378901
2020-10-18 21:14:22taleinatsetmessages: + msg378900
2020-10-18 21:00:06taleinatsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-10-18 20:59:43taleinatsetmessages: + msg378897
2020-10-18 20:57:33Anthony Sottilesetpull_requests: + pull_request21718
2020-10-18 20:56:31taleinatsetpull_requests: + pull_request21717
2020-10-18 20:54:58taleinatsetpull_requests: + pull_request21716
2020-10-18 20:54:17Anthony Sottilesetpull_requests: + pull_request21715
2020-10-18 20:48:38taleinatsetmessages: + msg378895
2020-10-18 06:27:01taleinatsetnosy: + taleinat

messages: + msg378851
versions: + Python 3.10, - Python 3.7
2020-05-15 07:43:12serhiy.storchakasetmessages: + msg368930
2020-05-15 02:47:58Anthony Sottilesetmessages: + msg368918
2020-05-04 19:18:42Anthony Sottilesetkeywords: + patch
pull_requests: + pull_request19225
2020-05-04 08:28:13serhiy.storchakasetversions: + Python 3.7, Python 3.8
nosy: + serhiy.storchaka

messages: + msg368018

stage: patch review
2020-05-04 03:36:27Anthony Sottilecreate