glibc.py 3.1 KB

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