summaryrefslogtreecommitdiff
path: root/lldb/test/API/get_darwin_real_python.py
blob: 3521fab675799aac63eb6216e6f30e472d37f9c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# On macOS, system python binaries like /usr/bin/python and $(xcrun -f python3)
# are shims. They do some light validation work and then spawn the "real" python
# binary. Find the "real" python by asking dyld -- sys.executable reports the
# wrong thing more often than not. This is also useful when we're running under
# a Homebrew python3 binary, which also appears to be some kind of shim.
def getDarwinRealPythonExecutable():
    import ctypes
    dyld = ctypes.cdll.LoadLibrary('/usr/lib/system/libdyld.dylib')
    namelen = ctypes.c_ulong(1024)
    name = ctypes.create_string_buffer(b'\000', namelen.value)
    dyld._NSGetExecutablePath(ctypes.byref(name), ctypes.byref(namelen))
    return name.value.decode('utf-8').strip()

print(getDarwinRealPythonExecutable())