Skip to content

Fix ctypes.util and ctypes.util.find_library issues on mac #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/typecode/magic2.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# SOFTWARE.

import ctypes
import ctypes.util
import glob
import os
import sys
Expand Down Expand Up @@ -122,12 +123,21 @@ def load_lib_failover():
or ctypes.util.find_library('magic1')
or ctypes.util.find_library('cygmagic-1')
or ctypes.util.find_library('libmagic-1')
# for Darwin
or ctypes.util.find_library('libmagic')
or ctypes.util.find_library('libmagic.1')
or ctypes.util.find_library('libmagic.dylib')
# for MSYS2
or ctypes.util.find_library('msys-magic-1')
)
# necessary because find_library returns None if it doesn't find the library
if dll:
libmagic = ctypes.CDLL(dll)

# find_library doesn't look at homebrew. Give it a hand
homebrew_libmagic = '/opt/homebrew/lib/libmagic.dylib'
if (not dll) and sys.platform == 'darwin' and os.path.exists(homebrew_magic):
libmagic = ctypes.cdll.LoadLibrary(homebrew_magic)

if not (libmagic and libmagic._name):
windows_dlls = [
Expand All @@ -143,6 +153,7 @@ def load_lib_failover():
'/usr/local/lib/libmagic.dylib',
] +
# Assumes there will only be one version installed when using brew
glob.glob('/opt/homebrew/lib/libmagic*.dylib') +
glob.glob('/usr/local/Cellar/libmagic/*/lib/libmagic.dylib') +
glob.glob('/opt/homebrew/Cellar/libmagic/*/lib/libmagic.dylib')
),
Expand Down