Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/magic/loader.py: 44%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

27 statements  

1from ctypes.util import find_library 

2import ctypes 

3import sys 

4import glob 

5import os.path 

6 

7def _lib_candidates(): 

8 

9 yield find_library('magic') 

10 

11 if sys.platform == 'darwin': 

12 

13 paths = [ 

14 '/opt/local/lib', 

15 '/usr/local/lib', 

16 '/opt/homebrew/lib', 

17 ] + glob.glob('/usr/local/Cellar/libmagic/*/lib') 

18 

19 for i in paths: 

20 yield os.path.join(i, 'libmagic.dylib') 

21 

22 elif sys.platform in ('win32', 'cygwin'): 

23 

24 prefixes = ['libmagic', 'magic1', 'cygmagic-1', 'libmagic-1', 'msys-magic-1'] 

25 

26 for i in prefixes: 

27 # find_library searches in %PATH% but not the current directory, 

28 # so look for both 

29 yield './%s.dll' % (i,) 

30 yield find_library(i) 

31 

32 elif sys.platform == 'linux': 

33 # This is necessary because alpine is bad 

34 yield 'libmagic.so.1' 

35 

36 

37def load_lib(): 

38 

39 for lib in _lib_candidates(): 

40 # find_library returns None when lib not found 

41 if lib is None: 

42 continue 

43 try: 

44 return ctypes.CDLL(lib) 

45 except OSError: 

46 pass 

47 else: 

48 # It is better to raise an ImportError since we are importing magic module 

49 raise ImportError('failed to find libmagic. Check your installation') 

50