From 5139dc494f624039d8a04e8b0a538cbe7c754fd1 Mon Sep 17 00:00:00 2001 From: Gregory Szorc Date: Sun, 26 May 2019 17:11:01 -0700 Subject: [PATCH] Handle ImportError and OSError when importing ctypes (#6543) Non-dynamic executables can raise OSError when importing ctypes because dlopen(NULL) is called on module import and dlopen() won't work on non-dynamic executables. This commit teaches the glibc version sniffing module to handle a missing or not working ctypes module. --- news/6543.bugfix | 1 + src/pip/_internal/utils/glibc.py | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 news/6543.bugfix diff --git a/news/6543.bugfix b/news/6543.bugfix new file mode 100644 index 00000000000..6c06999c4f3 --- /dev/null +++ b/news/6543.bugfix @@ -0,0 +1 @@ +Work around failure to import ctypes when resolving glibc version string. \ No newline at end of file diff --git a/src/pip/_internal/utils/glibc.py b/src/pip/_internal/utils/glibc.py index 5bea655ebcc..256552a42b2 100644 --- a/src/pip/_internal/utils/glibc.py +++ b/src/pip/_internal/utils/glibc.py @@ -1,11 +1,16 @@ from __future__ import absolute_import -import ctypes import re import warnings from pip._internal.utils.typing import MYPY_CHECK_RUNNING +# Work around https://bugs.python.org/issue37060. +try: + import ctypes +except (ImportError, OSError): + ctypes = None + if MYPY_CHECK_RUNNING: from typing import Optional, Tuple @@ -14,6 +19,9 @@ def glibc_version_string(): # type: () -> Optional[str] "Returns glibc version string, or None if not using glibc." + if not ctypes: + return None + # ctypes.CDLL(None) internally calls dlopen(NULL), and as the dlopen # manpage says, "If filename is NULL, then the returned handle is for the # main program". This way we can let the linker do the work to figure out