@@ -98,68 +98,69 @@ def from_buffer(buffer, mime=False): |
98 | 98 |
|
99 | 99 |
|
100 | 100 |
|
| 101 | +try: |
| 102 | + libmagic = ctypes.CDLL(ctypes.util.find_library('magic')) |
101 | 103 |
|
102 | | -libmagic = ctypes.CDLL(ctypes.util.find_library('magic')) |
| 104 | + magic_t = ctypes.c_void_p |
103 | 105 |
|
104 | | -magic_t = ctypes.c_void_p |
| 106 | + def errorcheck(result, func, args): |
| 107 | + err = magic_error(args[0]) |
| 108 | + if err is not None: |
| 109 | + raise MagicException(err) |
| 110 | + else: |
| 111 | + return result |
105 | 112 |
|
106 | | -def errorcheck(result, func, args): |
107 | | - err = magic_error(args[0]) |
108 | | - if err is not None: |
109 | | - raise MagicException(err) |
110 | | - else: |
111 | | - return result |
112 | | - |
113 | | -magic_open = libmagic.magic_open |
114 | | -magic_open.restype = magic_t |
115 | | -magic_open.argtypes = [c_int] |
116 | | - |
117 | | -magic_close = libmagic.magic_close |
118 | | -magic_close.restype = None |
119 | | -magic_close.argtypes = [magic_t] |
120 | | -magic_close.errcheck = errorcheck |
| 113 | + magic_open = libmagic.magic_open |
| 114 | + magic_open.restype = magic_t |
| 115 | + magic_open.argtypes = [c_int] |
121 | 116 |
|
122 | | -magic_error = libmagic.magic_error |
123 | | -magic_error.restype = c_char_p |
124 | | -magic_error.argtypes = [magic_t] |
| 117 | + magic_close = libmagic.magic_close |
| 118 | + magic_close.restype = None |
| 119 | + magic_close.argtypes = [magic_t] |
| 120 | + magic_close.errcheck = errorcheck |
125 | 121 |
|
126 | | -magic_errno = libmagic.magic_errno |
127 | | -magic_errno.restype = c_int |
128 | | -magic_errno.argtypes = [magic_t] |
| 122 | + magic_error = libmagic.magic_error |
| 123 | + magic_error.restype = c_char_p |
| 124 | + magic_error.argtypes = [magic_t] |
129 | 125 |
|
130 | | -magic_file = libmagic.magic_file |
131 | | -magic_file.restype = c_char_p |
132 | | -magic_file.argtypes = [magic_t, c_char_p] |
133 | | -magic_file.errcheck = errorcheck |
| 126 | + magic_errno = libmagic.magic_errno |
| 127 | + magic_errno.restype = c_int |
| 128 | + magic_errno.argtypes = [magic_t] |
134 | 129 |
|
| 130 | + magic_file = libmagic.magic_file |
| 131 | + magic_file.restype = c_char_p |
| 132 | + magic_file.argtypes = [magic_t, c_char_p] |
| 133 | + magic_file.errcheck = errorcheck |
135 | 134 |
|
136 | | -_magic_buffer = libmagic.magic_buffer |
137 | | -_magic_buffer.restype = c_char_p |
138 | | -_magic_buffer.argtypes = [magic_t, c_void_p, c_size_t] |
139 | | -_magic_buffer.errcheck = errorcheck |
140 | 135 |
|
| 136 | + _magic_buffer = libmagic.magic_buffer |
| 137 | + _magic_buffer.restype = c_char_p |
| 138 | + _magic_buffer.argtypes = [magic_t, c_void_p, c_size_t] |
| 139 | + _magic_buffer.errcheck = errorcheck |
141 | 140 |
|
142 | | -def magic_buffer(cookie, buf): |
143 | | - return _magic_buffer(cookie, buf, len(buf)) |
144 | 141 |
|
| 142 | + def magic_buffer(cookie, buf): |
| 143 | + return _magic_buffer(cookie, buf, len(buf)) |
145 | 144 |
|
146 | | -magic_load = libmagic.magic_load |
147 | | -magic_load.restype = c_int |
148 | | -magic_load.argtypes = [magic_t, c_char_p] |
149 | | -magic_load.errcheck = errorcheck |
150 | 145 |
|
151 | | -magic_setflags = libmagic.magic_setflags |
152 | | -magic_setflags.restype = c_int |
153 | | -magic_setflags.argtypes = [magic_t, c_int] |
| 146 | + magic_load = libmagic.magic_load |
| 147 | + magic_load.restype = c_int |
| 148 | + magic_load.argtypes = [magic_t, c_char_p] |
| 149 | + magic_load.errcheck = errorcheck |
154 | 150 |
|
155 | | -magic_check = libmagic.magic_check |
156 | | -magic_check.restype = c_int |
157 | | -magic_check.argtypes = [magic_t, c_char_p] |
| 151 | + magic_setflags = libmagic.magic_setflags |
| 152 | + magic_setflags.restype = c_int |
| 153 | + magic_setflags.argtypes = [magic_t, c_int] |
158 | 154 |
|
159 | | -magic_compile = libmagic.magic_compile |
160 | | -magic_compile.restype = c_int |
161 | | -magic_compile.argtypes = [magic_t, c_char_p] |
| 155 | + magic_check = libmagic.magic_check |
| 156 | + magic_check.restype = c_int |
| 157 | + magic_check.argtypes = [magic_t, c_char_p] |
162 | 158 |
|
| 159 | + magic_compile = libmagic.magic_compile |
| 160 | + magic_compile.restype = c_int |
| 161 | + magic_compile.argtypes = [magic_t, c_char_p] |
| 162 | +except: |
| 163 | + pass |
163 | 164 |
|
164 | 165 |
|
165 | 166 | MAGIC_NONE = 0x000000 # No flags |
|