@@ -106,99 +106,100 @@ def from_buffer(buffer, mime=False):
106106
107107
108108
109-
110- libmagic = None
111- # Let's try to find magic or magic1
112- dll = ctypes .util .find_library ('magic' ) or ctypes .util .find_library ('magic1' )
113-
114- # This is necessary because find_library returns None if it doesn't find the library
115- if dll :
116- libmagic = ctypes .CDLL (dll )
117-
118- if not libmagic or not libmagic ._name :
119- import sys
120- platform_to_lib = {'darwin' : ['/opt/local/lib/libmagic.dylib' ,
121- '/usr/local/lib/libmagic.dylib' ,
122- '/usr/local/Cellar/libmagic/5.10/lib/libmagic.dylib' ],
123- 'win32' : ['magic1.dll' ]}
124- for dll in platform_to_lib .get (sys .platform , []):
125- try :
126- libmagic = ctypes .CDLL (dll )
127- except OSError :
128- pass
129-
130- if not libmagic or not libmagic ._name :
131- # It is better to raise an ImportError since we are importing magic module
132- raise ImportError ('failed to find libmagic. Check your installation' )
133-
134- magic_t = ctypes .c_void_p
135-
136- def errorcheck (result , func , args ):
137- err = magic_error (args [0 ])
138- if err is not None :
139- raise MagicException (err )
140- else :
141- return result
142-
143- def coerce_filename (filename ):
144- if filename is None :
145- return None
146- return filename .encode (sys .getfilesystemencoding ())
147-
148- magic_open = libmagic .magic_open
149- magic_open .restype = magic_t
150- magic_open .argtypes = [c_int ]
151-
152- magic_close = libmagic .magic_close
153- magic_close .restype = None
154- magic_close .argtypes = [magic_t ]
155-
156- magic_error = libmagic .magic_error
157- magic_error .restype = c_char_p
158- magic_error .argtypes = [magic_t ]
159-
160- magic_errno = libmagic .magic_errno
161- magic_errno .restype = c_int
162- magic_errno .argtypes = [magic_t ]
163-
164- _magic_file = libmagic .magic_file
165- _magic_file .restype = c_char_p
166- _magic_file .argtypes = [magic_t , c_char_p ]
167- _magic_file .errcheck = errorcheck
168-
169- def magic_file (cookie , filename ):
170- return _magic_file (cookie , coerce_filename (filename ))
171-
172- _magic_buffer = libmagic .magic_buffer
173- _magic_buffer .restype = c_char_p
174- _magic_buffer .argtypes = [magic_t , c_void_p , c_size_t ]
175- _magic_buffer .errcheck = errorcheck
176-
177-
178- def magic_buffer (cookie , buf ):
179- return _magic_buffer (cookie , buf , len (buf ))
180-
181-
182- _magic_load = libmagic .magic_load
183- _magic_load .restype = c_int
184- _magic_load .argtypes = [magic_t , c_char_p ]
185- _magic_load .errcheck = errorcheck
186-
187- def magic_load (cookie , filename ):
188- return _magic_load (cookie , coerce_filename (filename ))
189-
190- magic_setflags = libmagic .magic_setflags
191- magic_setflags .restype = c_int
192- magic_setflags .argtypes = [magic_t , c_int ]
193-
194- magic_check = libmagic .magic_check
195- magic_check .restype = c_int
196- magic_check .argtypes = [magic_t , c_char_p ]
197-
198- magic_compile = libmagic .magic_compile
199- magic_compile .restype = c_int
200- magic_compile .argtypes = [magic_t , c_char_p ]
201-
109+ try :
110+ libmagic = None
111+ # Let's try to find magic or magic1
112+ dll = ctypes .util .find_library ('magic' ) or ctypes .util .find_library ('magic1' )
113+
114+ # This is necessary because find_library returns None if it doesn't find the library
115+ if dll :
116+ libmagic = ctypes .CDLL (dll )
117+
118+ if not libmagic or not libmagic ._name :
119+ import sys
120+ platform_to_lib = {'darwin' : ['/opt/local/lib/libmagic.dylib' ,
121+ '/usr/local/lib/libmagic.dylib' ,
122+ '/usr/local/Cellar/libmagic/5.10/lib/libmagic.dylib' ],
123+ 'win32' : ['magic1.dll' ]}
124+ for dll in platform_to_lib .get (sys .platform , []):
125+ try :
126+ libmagic = ctypes .CDLL (dll )
127+ except OSError :
128+ pass
129+
130+ if not libmagic or not libmagic ._name :
131+ # It is better to raise an ImportError since we are importing magic module
132+ raise ImportError ('failed to find libmagic. Check your installation' )
133+
134+ magic_t = ctypes .c_void_p
135+
136+ def errorcheck (result , func , args ):
137+ err = magic_error (args [0 ])
138+ if err is not None :
139+ raise MagicException (err )
140+ else :
141+ return result
142+
143+ def coerce_filename (filename ):
144+ if filename is None :
145+ return None
146+ return filename .encode (sys .getfilesystemencoding ())
147+
148+ magic_open = libmagic .magic_open
149+ magic_open .restype = magic_t
150+ magic_open .argtypes = [c_int ]
151+
152+ magic_close = libmagic .magic_close
153+ magic_close .restype = None
154+ magic_close .argtypes = [magic_t ]
155+
156+ magic_error = libmagic .magic_error
157+ magic_error .restype = c_char_p
158+ magic_error .argtypes = [magic_t ]
159+
160+ magic_errno = libmagic .magic_errno
161+ magic_errno .restype = c_int
162+ magic_errno .argtypes = [magic_t ]
163+
164+ _magic_file = libmagic .magic_file
165+ _magic_file .restype = c_char_p
166+ _magic_file .argtypes = [magic_t , c_char_p ]
167+ _magic_file .errcheck = errorcheck
168+
169+ def magic_file (cookie , filename ):
170+ return _magic_file (cookie , coerce_filename (filename ))
171+
172+ _magic_buffer = libmagic .magic_buffer
173+ _magic_buffer .restype = c_char_p
174+ _magic_buffer .argtypes = [magic_t , c_void_p , c_size_t ]
175+ _magic_buffer .errcheck = errorcheck
176+
177+
178+ def magic_buffer (cookie , buf ):
179+ return _magic_buffer (cookie , buf , len (buf ))
180+
181+
182+ _magic_load = libmagic .magic_load
183+ _magic_load .restype = c_int
184+ _magic_load .argtypes = [magic_t , c_char_p ]
185+ _magic_load .errcheck = errorcheck
186+
187+ def magic_load (cookie , filename ):
188+ return _magic_load (cookie , coerce_filename (filename ))
189+
190+ magic_setflags = libmagic .magic_setflags
191+ magic_setflags .restype = c_int
192+ magic_setflags .argtypes = [magic_t , c_int ]
193+
194+ magic_check = libmagic .magic_check
195+ magic_check .restype = c_int
196+ magic_check .argtypes = [magic_t , c_char_p ]
197+
198+ magic_compile = libmagic .magic_compile
199+ magic_compile .restype = c_int
200+ magic_compile .argtypes = [magic_t , c_char_p ]
201+ except ImportError :
202+ pass
202203
203204
204205MAGIC_NONE = 0x000000 # No flags
0 commit comments