Skip to content

Commit

Permalink
Handle ImportError and OSError when importing ctypes (pypa#6543)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
indygreg committed May 27, 2019
1 parent 6178f96 commit 5139dc4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions news/6543.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Work around failure to import ctypes when resolving glibc version string.
10 changes: 9 additions & 1 deletion src/pip/_internal/utils/glibc.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down

0 comments on commit 5139dc4

Please sign in to comment.