Skip to content

Commit 065bed6

Browse files
committed
Use readlink to read /proc/self/exe
Signed-off-by: Filipe Laíns <lains@riseup.net>
1 parent 6ecb062 commit 065bed6

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

Modules/getpath.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,11 @@ progname_to_dict(PyObject *dict, const char *key)
797797
PyMem_RawFree(path);
798798
break;
799799
}
800+
#elif defined(HAVE_READLINK)
801+
wchar_t resolved[MAXPATHLEN + 1];
802+
if (_Py_wreadlink(L"/proc/self/exe", resolved, Py_ARRAY_LENGTH(resolved)) != -1) {
803+
return wchar_to_dict(dict, key, resolved);
804+
}
800805
#endif
801806
return PyDict_SetItemString(dict, key, Py_None) == 0;
802807
}

Modules/getpath.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,6 @@ def search_up(prefix, *landmarks, test=isfile):
265265
if not executable:
266266
executable = real_executable
267267

268-
if not executable and os_name == 'posix':
269-
# On Linux, try resolving the executable path via procfs
270-
try:
271-
executable = realpath('/proc/self/exe')
272-
except (OSError, MemoryError):
273-
pass
274-
275268
if not executable and SEP in program_name:
276269
# Resolve partial path program_name against current directory
277270
executable = abspath(program_name)
@@ -287,6 +280,17 @@ def search_up(prefix, *landmarks, test=isfile):
287280
# whether we are in a build tree. This is true even if the
288281
# executable path was provided in the config.
289282
real_executable = executable
283+
elif os_name == 'posix':
284+
# real_executable is more accurate than the value we have computed for
285+
# executable, so use it instead if it resolves to a different path
286+
# (eg. GH-124241).
287+
# If real_executable and executable resolve to the same path, prefer
288+
# executable, as that is much more likely to be the path the user is using.
289+
try:
290+
if realpath(executable) != real_executable:
291+
executable = real_executable
292+
except OSError:
293+
pass
290294

291295
if not executable and program_name and ENV_PATH:
292296
# Resolve names against PATH.

0 commit comments

Comments
 (0)