glibc.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from __future__ import absolute_import
  2. import ctypes
  3. import re
  4. import warnings
  5. def glibc_version_string():
  6. "Returns glibc version string, or None if not using glibc."
  7. # ctypes.CDLL(None) internally calls dlopen(NULL), and as the dlopen
  8. # manpage says, "If filename is NULL, then the returned handle is for the
  9. # main program". This way we can let the linker do the work to figure out
  10. # which libc our process is actually using.
  11. process_namespace = ctypes.CDLL(None)
  12. try:
  13. gnu_get_libc_version = process_namespace.gnu_get_libc_version
  14. except AttributeError:
  15. # Symbol doesn't exist -> therefore, we are not linked to
  16. # glibc.
  17. return None
  18. # Call gnu_get_libc_version, which returns a string like "2.5"
  19. gnu_get_libc_version.restype = ctypes.c_char_p
  20. version_str = gnu_get_libc_version()
  21. # py2 / py3 compatibility:
  22. if not isinstance(version_str, str):
  23. version_str = version_str.decode("ascii")
  24. return version_str
  25. # Separated out from have_compatible_glibc for easier unit testing
  26. def check_glibc_version(version_str, required_major, minimum_minor):
  27. # Parse string and check against requested version.
  28. #
  29. # We use a regexp instead of str.split because we want to discard any
  30. # random junk that might come after the minor version -- this might happen
  31. # in patched/forked versions of glibc (e.g. Linaro's version of glibc
  32. # uses version strings like "2.20-2014.11"). See gh-3588.
  33. m = re.match(r"(?P<major>[0-9]+)\.(?P<minor>[0-9]+)", version_str)
  34. if not m:
  35. warnings.warn("Expected glibc version with 2 components major.minor,"
  36. " got: %s" % version_str, RuntimeWarning)
  37. return False
  38. return (int(m.group("major")) == required_major and
  39. int(m.group("minor")) >= minimum_minor)
  40. def have_compatible_glibc(required_major, minimum_minor):
  41. version_str = glibc_version_string()
  42. if version_str is None:
  43. return False
  44. return check_glibc_version(version_str, required_major, minimum_minor)
  45. # platform.libc_ver regularly returns completely nonsensical glibc
  46. # versions. E.g. on my computer, platform says:
  47. #
  48. # ~$ python2.7 -c 'import platform; print(platform.libc_ver())'
  49. # ('glibc', '2.7')
  50. # ~$ python3.5 -c 'import platform; print(platform.libc_ver())'
  51. # ('glibc', '2.9')
  52. #
  53. # But the truth is:
  54. #
  55. # ~$ ldd --version
  56. # ldd (Debian GLIBC 2.22-11) 2.22
  57. #
  58. # This is unfortunate, because it means that the linehaul data on libc
  59. # versions that was generated by pip 8.1.2 and earlier is useless and
  60. # misleading. Solution: instead of using platform, use our code that actually
  61. # works.
  62. def libc_ver():
  63. """Try to determine the glibc version
  64. Returns a tuple of strings (lib, version) which default to empty strings
  65. in case the lookup fails.
  66. """
  67. glibc_version = glibc_version_string()
  68. if glibc_version is None:
  69. return ("", "")
  70. else:
  71. return ("glibc", glibc_version)