diff options
Diffstat (limited to 'magic.py')
| -rw-r--r-- | magic.py | 35 |
1 files changed, 26 insertions, 9 deletions
@@ -69,9 +69,9 @@ class Magic: return magic_file(self.cookie, filename) def __del__(self): - if self.cookie: - magic_close(self.cookie) - self.cookie = None + if self.cookie: + magic_close(self.cookie) + self.cookie = None _magic_mime = None _magic = None @@ -105,17 +105,34 @@ def from_buffer(buffer, mime=False): -libmagic = ctypes.CDLL(ctypes.util.find_library('magic')) +libmagic = None +# Let's try to find magic or magic1 +dll = ctypes.util.find_library('magic') or ctypes.util.find_library('magic1') + +# This is necessary because find_library returns None if it doesn't find the library +if dll: + libmagic = ctypes.CDLL(dll) + if not libmagic or not libmagic._name: import sys if sys.platform == "darwin": - # try mac ports location - libmagic = ctypes.CDLL('/opt/local/lib/libmagic.dylib') + try: + # try mac ports location + libmagic = ctypes.CDLL('/opt/local/lib/libmagic.dylib') + # Should we catch just OSError exceptions? + except: + pass elif sys.platform == "win32": - # try local magic1.dll - libmagic = ctypes.CDLL('magic1.dll') + try: + # try local magic1.dll + libmagic = ctypes.CDLL('magic1.dll') + # Should we catch just OSError exceptions? + except: + pass + if not libmagic or not libmagic._name: - raise Exception('failed to find libmagic. Check your installation') + # It is better to raise an ImportError since we are importing magic module + raise ImportError('failed to find libmagic. Check your installation') magic_t = ctypes.c_void_p |
