Skip to content

Commit 5139dc4

Browse files
committed
Handle ImportError and OSError when importing ctypes (pypa#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.
1 parent 6178f96 commit 5139dc4

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

news/6543.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Work around failure to import ctypes when resolving glibc version string.

src/pip/_internal/utils/glibc.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
from __future__ import absolute_import
22

3-
import ctypes
43
import re
54
import warnings
65

76
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
87

8+
# Work around https://bugs.python.org/issue37060.
9+
try:
10+
import ctypes
11+
except (ImportError, OSError):
12+
ctypes = None
13+
914
if MYPY_CHECK_RUNNING:
1015
from typing import Optional, Tuple
1116

@@ -14,6 +19,9 @@ def glibc_version_string():
1419
# type: () -> Optional[str]
1520
"Returns glibc version string, or None if not using glibc."
1621

22+
if not ctypes:
23+
return None
24+
1725
# ctypes.CDLL(None) internally calls dlopen(NULL), and as the dlopen
1826
# manpage says, "If filename is NULL, then the returned handle is for the
1927
# main program". This way we can let the linker do the work to figure out

0 commit comments

Comments
 (0)