| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- Subject: Clean up loader.py
- Origin: upstream, commit 0.4.27-31-gcf21065 <https://github.com/ahupp/python-magic/commit/0.4.27-31-gcf21065>
- Author: Christian Clauss <cclauss@me.com>
- Date: Wed May 22 16:15:04 2024 +0200
- --- a/magic/loader.py
- +++ b/magic/loader.py
- @@ -4,47 +4,65 @@
- import glob
- import os.path
-
- -def _lib_candidates():
-
- - yield find_library('magic')
- +def _lib_candidates_linux():
- + """Yield possible libmagic library names on Linux.
- +
- + This is necessary because alpine is bad
- + """
- + yield "libmagic.so.1"
-
- - if sys.platform == 'darwin':
-
- +def _lib_candidates_macos():
- + """Yield possible libmagic library names on macOS."""
- paths = [
- - '/opt/local/lib',
- - '/usr/local/lib',
- - '/opt/homebrew/lib',
- - ] + glob.glob('/usr/local/Cellar/libmagic/*/lib')
- -
- - for i in paths:
- - yield os.path.join(i, 'libmagic.dylib')
- -
- - elif sys.platform in ('win32', 'cygwin'):
- -
- - prefixes = ['libmagic', 'magic1', 'magic-1', 'cygmagic-1', 'libmagic-1', 'msys-magic-1']
- -
- - for i in prefixes:
- - # find_library searches in %PATH% but not the current directory,
- - # so look for both
- - yield './%s.dll' % (i,)
- - yield find_library(i)
- -
- - elif sys.platform == 'linux':
- - # This is necessary because alpine is bad
- - yield 'libmagic.so.1'
- + "/opt/homebrew/lib",
- + "/opt/local/lib",
- + "/usr/local/lib",
- + ] + glob.glob("/usr/local/Cellar/libmagic/*/lib")
- + for path in paths:
- + yield os.path.join(path, "libmagic.dylib")
- +
- +
- +def _lib_candidates_windows():
- + """Yield possible libmagic library names on Windows."""
- + prefixes = (
- + "libmagic",
- + "magic1",
- + "magic-1",
- + "cygmagic-1",
- + "libmagic-1",
- + "msys-magic-1",
- + )
- + for prefix in prefixes:
- + # find_library searches in %PATH% but not the current directory,
- + # so look for both
- + yield "./%s.dll" % (prefix,)
- + yield find_library(prefix)
- +
- +
- +def _lib_candidates():
- + yield find_library("magic")
- +
- + func = {
- + "cygwin": _lib_candidates_windows,
- + "darwin": _lib_candidates_macos,
- + "linux": _lib_candidates_linux,
- + "win32": _lib_candidates_windows,
- + }[sys.platform]
- + # When we drop legacy Python, we can just `yield from func()`
- + for path in func():
- + yield path
-
-
- 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
-
- - for lib in _lib_candidates():
- - # find_library returns None when lib not found
- - if lib is None:
- - continue
- - try:
- - return ctypes.CDLL(lib)
- - except OSError:
- - pass
- - else:
- # It is better to raise an ImportError since we are importing magic module
- - raise ImportError('failed to find libmagic. Check your installation')
- -
- + raise ImportError("failed to find libmagic. Check your installation")
|