glibc.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import os
  2. import sys
  3. from typing import Optional, Tuple
  4. def glibc_version_string() -> Optional[str]:
  5. "Returns glibc version string, or None if not using glibc."
  6. return glibc_version_string_confstr() or glibc_version_string_ctypes()
  7. def glibc_version_string_confstr() -> Optional[str]:
  8. "Primary implementation of glibc_version_string using os.confstr."
  9. # os.confstr is quite a bit faster than ctypes.DLL. It's also less likely
  10. # to be broken or missing. This strategy is used in the standard library
  11. # platform module:
  12. # https://github.com/python/cpython/blob/fcf1d003bf4f0100c9d0921ff3d70e1127ca1b71/Lib/platform.py#L175-L183
  13. if sys.platform == "win32":
  14. return None
  15. try:
  16. gnu_libc_version = os.confstr("CS_GNU_LIBC_VERSION")
  17. if gnu_libc_version is None:
  18. return None
  19. # os.confstr("CS_GNU_LIBC_VERSION") returns a string like "glibc 2.17":
  20. _, version = gnu_libc_version.split()
  21. except (AttributeError, OSError, ValueError):
  22. # os.confstr() or CS_GNU_LIBC_VERSION not available (or a bad value)...
  23. return None
  24. return version
  25. def glibc_version_string_ctypes() -> Optional[str]:
  26. "Fallback implementation of glibc_version_string using ctypes."
  27. try:
  28. import ctypes
  29. except ImportError:
  30. return None
  31. # ctypes.CDLL(None) internally calls dlopen(NULL), and as the dlopen
  32. # manpage says, "If filename is NULL, then the returned handle is for the
  33. # main program". This way we can let the linker do the work to figure out
  34. # which libc our process is actually using.
  35. process_namespace = ctypes.CDLL(None)
  36. try:
  37. gnu_get_libc_version = process_namespace.gnu_get_libc_version
  38. except AttributeError:
  39. # Symbol doesn't exist -> therefore, we are not linked to
  40. # glibc.
  41. return None
  42. # Call gnu_get_libc_version, which returns a string like "2.5"
  43. gnu_get_libc_version.restype = ctypes.c_char_p
  44. version_str = gnu_get_libc_version()
  45. # py2 / py3 compatibility:
  46. if not isinstance(version_str, str):
  47. version_str = version_str.decode("ascii")
  48. return version_str
  49. # platform.libc_ver regularly returns completely nonsensical glibc
  50. # versions. E.g. on my computer, platform says:
  51. #
  52. # ~$ python2.7 -c 'import platform; print(platform.libc_ver())'
  53. # ('glibc', '2.7')
  54. # ~$ python3.5 -c 'import platform; print(platform.libc_ver())'
  55. # ('glibc', '2.9')
  56. #
  57. # But the truth is:
  58. #
  59. # ~$ ldd --version
  60. # ldd (Debian GLIBC 2.22-11) 2.22
  61. #
  62. # This is unfortunate, because it means that the linehaul data on libc
  63. # versions that was generated by pip 8.1.2 and earlier is useless and
  64. # misleading. Solution: instead of using platform, use our code that actually
  65. # works.
  66. def libc_ver() -> Tuple[str, str]:
  67. """Try to determine the glibc version
  68. Returns a tuple of strings (lib, version) which default to empty strings
  69. in case the lookup fails.
  70. """
  71. glibc_version = glibc_version_string()
  72. if glibc_version is None:
  73. return ("", "")
  74. else:
  75. return ("glibc", glibc_version)