summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Hupp <adam@hupp.org>2010-11-21 23:13:16 -0800
committerAdam Hupp <adam@hupp.org>2010-11-21 23:13:16 -0800
commit2f5d66706d1ea0d20445baab3a66d353a5e5edbf (patch)
tree1caed2cdb764e21dd44df03f1898a4bdc2f8ba91
parentf082313f250b9b8b1d393ad0740aef09afd86244 (diff)
parent61fd4ee20345018283562045498feb1db048feed (diff)
downloadpython-magic-2f5d66706d1ea0d20445baab3a66d353a5e5edbf.tar.gz
Merge branch 'master' of https://github.com/FlaPer87/python-magic into FlaPer87-master
-rw-r--r--magic.py35
1 files changed, 26 insertions, 9 deletions
diff --git a/magic.py b/magic.py
index 7d7b872..8f03889 100644
--- a/magic.py
+++ b/magic.py
@@ -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