Skip to content

gh-158068: venv: do not clear env_dir if it's a symlink or a file - #158075

Open
frenzymadness wants to merge 3 commits into
python:mainfrom
frenzymadness:venv_clean_158068
Open

frenzymadness wants to merge 3 commits into
python:mainfrom
frenzymadness:venv_clean_158068

Conversation

@frenzymadness

@frenzymadness frenzymadness commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

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.

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>
Comment thread Lib/venv/__init__.py Resolved
Comment thread Lib/venv/__init__.py Resolved
Comment thread Lib/test/test_venv.py Resolved
Comment thread Lib/test/test_venv.py Resolved
@frenzymadness

Copy link
Copy Markdown
Contributor Author

Thank you for the review, Victor, and sorry for the delay. All comments addressed in the newest commit.

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, there is now a merge conflict. Can you merge the main branch in your branch and try to solve the conflict?

@vstinner

vstinner commented Nov 9, 2026

Copy link
Copy Markdown
Member

@JelleZijlstra: Would you mind to review this change?

@frenzymadness

Copy link
Copy Markdown
Contributor Author

Merged. There is one new commit in the main branch and it's related to this: bf6623a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants