From a092f11986234c51b492318a96c74e37dceca5e2 Mon Sep 17 00:00:00 2001 From: wmazur-splunk <108666827+wmazur-splunk@users.noreply.github.com> Date: Mon, 29 Aug 2022 14:04:34 +0200 Subject: [PATCH] DO NOT swallow the exceptions At least you will know why loading the library fails --- magic/loader.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/magic/loader.py b/magic/loader.py index 228a35c..d3dd768 100644 --- a/magic/loader.py +++ b/magic/loader.py @@ -1,5 +1,6 @@ from ctypes.util import find_library import ctypes +import logging import sys import glob import os.path @@ -40,10 +41,12 @@ def load_lib(): # find_library returns None when lib not found if lib is None: continue - try: - return ctypes.CDLL(lib) - except OSError: - pass + if os.path.exists(lib): + try: + return ctypes.CDLL(lib) + except OSError as err: + logging.warning(err) + pass else: # It is better to raise an ImportError since we are importing magic module raise ImportError('failed to find libmagic. Check your installation')