Repository navigation
gh-158068: venv: do not clear env_dir if it's a symlink or a file - #158075
frenzymadness wants to merge 3 commits into
Conversation
Previously `venv --clear` called clear_directory() on env_dir whenever it existed. If env_dir was a symlink to a directory, this followed the link and wiped the contents of the target directory -- possible data loss. If it was a regular file it failed with an unclear error. Now creating a venv over an existing symlink or file raises ValueError before anything is removed, matching the check already done for the non-clear case. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Thank you for the review, Victor, and sorry for the delay. All comments addressed in the newest commit. |
vstinner
left a comment
There was a problem hiding this comment.
LGTM.
Ok. I can reproduce the bug in the main branch if the destination is symlink to a directory:
$ mkdir demo
$ echo data > demo/precious_file.txt
$ ln -s demo venv
'venv' -> 'demo'
$ ./python -m venv venv --clear
Error: Unable to create directory '/home/vstinner/python/main/venv'
$ file venv
venv: symbolic link to demo
$ ls demo/ # empty directory!!!
Currently, demo/precious_file.txt is removed even if venv fails with "Unable to create directory" error!
With the change, demo/ contents is no longer removed:
(...)
$ file venv
venv: symbolic link to demo
$ ls demo/
precious_file.txt
Note: If the destination is a regular file, it's not removed on the main branch:
$ echo "precious data" > env
$ ./python -m venv env --clear
Error: Unable to create directory '/home/vstinner/python/main/env': the existing file is not a directory
$ file env
env: ASCII text
$ cat env
precious data
vstinner
left a comment
There was a problem hiding this comment.
Ah, there is now a merge conflict. Can you merge the main branch in your branch and try to solve the conflict?
|
@JelleZijlstra: Would you mind to review this change? |
3f29b6d to
698d98a
Compare
|
Merged. There is one new commit in the main branch and it's related to this: bf6623a |
Previously
venv --clearcalled clear_directory() on env_dir whenever it existed. If env_dir was a symlink to a directory, this followed the link and wiped the contents of the target directory -- possible data loss. If it was a regular file it failed with an unclear error.Now creating a venv over an existing symlink or file raises ValueError before anything is removed, matching the check already done for the non-clear case.
--cleardeletes symlink target contents before rejecting the symlink #158068