glibc.py 3.0 KB

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