Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce pathlib on init_virtualenv. #12548

Merged
merged 3 commits into from Sep 17, 2020

Conversation

GabrielSimonetto
Copy link
Contributor

@GabrielSimonetto GabrielSimonetto commented Sep 9, 2020

Motivated by #12515

I'm still looking for a way to test the code on a windows VM, and will try to introduce automated tests later

Copy link
Contributor Author

@GabrielSimonetto GabrielSimonetto left a comment

Choose a reason for hiding this comment

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

Left some reasonings behind my decisions

p_exe_up2 = os.path.dirname(os.path.dirname(p))
if p_exe_up2 and os.path.exists(p_venv) and os.path.samefile(p_exe_up2, p_venv):
# Our exe is inside the virtualenv, don't need to do anything.
elif os.environ['VIRTUAL_ENV'] == '':
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm changing this because of this issue #11100
which was resolved this way #11172

The code I'm using here was suggested on the issue, and it seems better to me,
specially now that we are using Pathlib, since Path('') actualy means the path in which you currently are

So, as is, we would get a True when we should get a False
In [4]: Path('').exists()
Out[4]: True

If the warning is a bit excessive I can remove it and merge with the upwards if statement

# In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible
if p_venv.startswith('\\cygdrive'):
p_venv = p_venv[11:]
elif len(p_venv) >= 2 and p_venv[1] == ':':
p_venv = p_venv[2:]

if any(p_venv in p for p in paths):
# Running properly in the virtualenv, don't need to do anything
if any(os.fspath(p_venv) in os.fspath(p) for p in paths):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Found no pathlibian way to do this.

Can change to str(p_venv) if it's better, we can even forget running pathlib on this section, since the previous code seemed a little better to me

while p.is_symlink():
p = Path(os.readlink(p))
paths.append(p.resolve())

# In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Didn't change this code because I couldn't test it on my linux, currently looking for some VM thing


# executable path should end like /bin/python or \\scripts\\python.exe
p_exe_up2 = os.path.dirname(os.path.dirname(p))
if p_exe_up2 and os.path.exists(p_venv) and os.path.samefile(p_exe_up2, p_venv):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This condicional is already met on line 928, I have changed it's comments to reflect this change

paths.append(p)

while p.is_symlink():
p = Path(os.readlink(p))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to this link

I believe it's not possible to replicate this on pathlib
If someone sees a better solution please let me know.

Copy link
Member

Choose a reason for hiding this comment

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

👍 we are 3.7+ so p.resolve() might work, but we can check this later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

p.resolve() seems to follow symlinks all the way. Here we would like to follow them one by one to check later if any of them are inside the scope for any(os.fspath(p_venv) in os.fspath(p) for p in paths)

@Carreau Carreau added this to the 8.0 milestone Sep 14, 2020
@Carreau
Copy link
Member

Carreau commented Sep 14, 2020

Thanks that's looks great. If there are breakage we can still fix later and make sure there is a test case in the windows CI.

@MrMino
Copy link
Member

MrMino commented Sep 20, 2021

@Carreau can I backport this to 7.x? I need this for #13140.

@Carreau Carreau modified the milestones: 8.0, 7.28 Sep 21, 2021
@Carreau
Copy link
Member

Carreau commented Sep 21, 2021

@Carreau can I backport this to 7.x? I need this for #13140.

Sure, you are a core dev, you don't need my authorisation :-)

@meeseeksdev backport to 7.x

@MrMino
Copy link
Member

MrMino commented Sep 21, 2021

@Carreau I know, but I was worried that there are reasons behind not doing so that I'm not seeing. Thanks :)!

MrMino added a commit that referenced this pull request Sep 21, 2021
…548-on-7.x

Backport PR #12548 on branch 7.x (Introduce pathlib on init_virtualenv.)
@Carreau
Copy link
Member

Carreau commented Sep 24, 2021

but I was worried that there are reasons behind not doing so that I'm not seeing

I think It's also because i have less time than before and try to backport only what is critical and that was close to a year ago and I thoughts 8.0 was not that far down the line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants