Skip to content

bpo-39469: Support relative home paths for pyvenv.cfg #18213

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ def venv(known_paths):
if key == 'include-system-site-packages':
system_site = value.lower()
elif key == 'home':
if value.startswith('.'):
value = os.path.join(os.path.dirname(virtual_conf), value)
sys._home = value

sys.prefix = sys.exec_prefix = site_prefix
Expand Down
21 changes: 16 additions & 5 deletions Modules/getpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@ calculate_argv0_path(PyCalculatePath *calculate,


static PyStatus
calculate_open_pyenv(PyCalculatePath *calculate, FILE **env_file_p)
calculate_open_pyenv(PyCalculatePath *calculate, FILE **env_file_p, wchar_t **pyenv_filename)
{
*env_file_p = NULL;

Expand All @@ -1210,12 +1210,13 @@ calculate_open_pyenv(PyCalculatePath *calculate, FILE **env_file_p)
}

*env_file_p = _Py_wfopen(filename, L"r");
PyMem_RawFree(filename);

if (*env_file_p != NULL) {
*pyenv_filename = filename;
return _PyStatus_OK();

}
PyMem_RawFree(filename);

/* fopen() failed: reset errno */
errno = 0;
Expand All @@ -1234,11 +1235,13 @@ calculate_open_pyenv(PyCalculatePath *calculate, FILE **env_file_p)
}

*env_file_p = _Py_wfopen(filename, L"r");
PyMem_RawFree(filename);

if (*env_file_p == NULL) {
PyMem_RawFree(filename);
/* fopen() failed: reset errno */
errno = 0;
} else {
*pyenv_filename = filename;
}
return _PyStatus_OK();
}
Expand All @@ -1254,8 +1257,9 @@ calculate_read_pyenv(PyCalculatePath *calculate)
{
PyStatus status;
FILE *env_file = NULL;
wchar_t *pyenv_filename = NULL;

status = calculate_open_pyenv(calculate, &env_file);
status = calculate_open_pyenv(calculate, &env_file, &pyenv_filename);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
Expand All @@ -1274,7 +1278,14 @@ calculate_read_pyenv(PyCalculatePath *calculate)

if (home) {
PyMem_RawFree(calculate->argv0_path);
calculate->argv0_path = home;
if (home[0] == '.') {
/* Relative home path to pyvenv_filename */
reduce(pyenv_filename);
calculate->argv0_path = joinpath2(pyenv_filename, home);
} else {
/* Absolute home path */
calculate->argv0_path = home;
}
}
fclose(env_file);
return _PyStatus_OK();
Expand Down
9 changes: 8 additions & 1 deletion PC/getpathp.c
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,14 @@ calculate_pyvenv_file(PyCalculatePath *calculate,
return status;
}
if (home) {
wcscpy_s(argv0_path, argv0_path_len, home);
if (home[0] == '.') {
/* Home path is relative to directory of pyvenv.cfg file */
reduce(filename);
join(filename, home);
wcscpy_s(argv0_path, argv0_path_len, filename);
} else {
wcscpy_s(argv0_path, argv0_path_len, home);
}
PyMem_RawFree(home);
}
fclose(env_file);
Expand Down