Skip to content

Commit

Permalink
log warning on ctypes load error, adapted from #279
Browse files Browse the repository at this point in the history
  • Loading branch information
ahupp committed May 26, 2024
1 parent 339eac0 commit a9e6276
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions magic/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import sys
import glob
import os.path
import logging

logger = logging.getLogger(__name__)

def _lib_candidates_linux():
"""Yield possible libmagic library names on Linux.
Expand Down Expand Up @@ -61,11 +63,15 @@ def _lib_candidates():
def load_lib():
for lib in _lib_candidates():
# find_library returns None when lib not found
if lib:
try:
return ctypes.CDLL(lib)
except OSError:
pass
if lib is None:
continue
if not os.path.exists(lib):
continue

try:
return ctypes.CDLL(lib)
except OSError:
logger.warning("Failed to load: " + lib, exc_info=True)

# It is better to raise an ImportError since we are importing magic module
raise ImportError("python-magic: failed to find libmagic. Check your installation")

0 comments on commit a9e6276

Please sign in to comment.