|
18 | 18 | SHLIB_SUFFIX = sysconfig.get_config_var("SHLIB_SUFFIX") or ".so"
|
19 | 19 |
|
20 | 20 |
|
| 21 | +def linked_libpython(): |
| 22 | + """ |
| 23 | + Find the linked libpython using dladdr (in *nix). |
| 24 | +
|
| 25 | + Calling this in Windows always return `None` at the moment. |
| 26 | +
|
| 27 | + Returns |
| 28 | + ------- |
| 29 | + path : str or None |
| 30 | + A path to linked libpython. Return `None` if statically linked. |
| 31 | + """ |
| 32 | + if platform.system() == "Windows": |
| 33 | + return None |
| 34 | + return _linked_libpython_unix() |
| 35 | + |
| 36 | + |
| 37 | +class Dl_info(ctypes.Structure): |
| 38 | + _fields_ = [ |
| 39 | + ("dli_fname", ctypes.c_char_p), |
| 40 | + ("dli_fbase", ctypes.c_void_p), |
| 41 | + ("dli_sname", ctypes.c_char_p), |
| 42 | + ("dli_saddr", ctypes.c_void_p), |
| 43 | + ] |
| 44 | + |
| 45 | + |
| 46 | +def _linked_libpython_unix(): |
| 47 | + libdl = ctypes.CDLL(ctypes.util.find_library("dl")) |
| 48 | + libdl.dladdr.argtypes = [ctypes.c_void_p, ctypes.POINTER(Dl_info)] |
| 49 | + libdl.dladdr.restype = ctypes.c_int |
| 50 | + |
| 51 | + dlinfo = Dl_info() |
| 52 | + retcode = libdl.dladdr( |
| 53 | + ctypes.cast(ctypes.pythonapi.Py_GetVersion, ctypes.c_void_p), |
| 54 | + ctypes.pointer(dlinfo)) |
| 55 | + if retcode == 0: # means error |
| 56 | + return None |
| 57 | + path = os.path.realpath(dlinfo.dli_fname.decode()) |
| 58 | + if path == os.path.realpath(sys.executable): |
| 59 | + return None |
| 60 | + return path |
| 61 | + |
| 62 | + |
21 | 63 | def library_name(name, suffix=SHLIB_SUFFIX,
|
22 | 64 | is_windows=platform.system() == "Windows"):
|
23 | 65 | """
|
@@ -54,6 +96,9 @@ def libpython_candidates(suffix=SHLIB_SUFFIX):
|
54 | 96 | Candidate path to libpython. The path may not be a fullpath
|
55 | 97 | and may not exist.
|
56 | 98 | """
|
| 99 | + |
| 100 | + yield linked_libpython() |
| 101 | + |
57 | 102 | is_windows = platform.system() == "Windows"
|
58 | 103 |
|
59 | 104 | # List candidates for libpython basenames
|
|
0 commit comments