Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use readlink to read /proc/self/exe
Signed-off-by: Filipe Laíns <lains@riseup.net>
  • Loading branch information
FFY00 committed Mar 4, 2026
commit 065bed62bee6a34822e9a16594d82d72fd0321c0
5 changes: 5 additions & 0 deletions Modules/getpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,11 @@ progname_to_dict(PyObject *dict, const char *key)
PyMem_RawFree(path);
break;
}
#elif defined(HAVE_READLINK)
wchar_t resolved[MAXPATHLEN + 1];
if (_Py_wreadlink(L"/proc/self/exe", resolved, Py_ARRAY_LENGTH(resolved)) != -1) {
return wchar_to_dict(dict, key, resolved);
}
#endif
return PyDict_SetItemString(dict, key, Py_None) == 0;
}
Expand Down
18 changes: 11 additions & 7 deletions Modules/getpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,6 @@ def search_up(prefix, *landmarks, test=isfile):
if not executable:
executable = real_executable

if not executable and os_name == 'posix':
# On Linux, try resolving the executable path via procfs
try:
executable = realpath('/proc/self/exe')
except (OSError, MemoryError):
pass

if not executable and SEP in program_name:
# Resolve partial path program_name against current directory
executable = abspath(program_name)
Expand All @@ -287,6 +280,17 @@ def search_up(prefix, *landmarks, test=isfile):
# whether we are in a build tree. This is true even if the
# executable path was provided in the config.
real_executable = executable
elif os_name == 'posix':
# real_executable is more accurate than the value we have computed for
# executable, so use it instead if it resolves to a different path
# (eg. GH-124241).
# If real_executable and executable resolve to the same path, prefer
# executable, as that is much more likely to be the path the user is using.
try:
if realpath(executable) != real_executable:
executable = real_executable
except OSError:
pass

if not executable and program_name and ENV_PATH:
# Resolve names against PATH.
Expand Down
Loading