Codebase list python-magic-ahupp / 4514350
Updated constructor to accept the raw flag and pass it to the underlying library. Helpful for cases where unicode chars exist in match results. rmspeers 5 years ago
4 changed file(s) with 35 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
33 deb_dist
44 htmlcov/
55 lib/
6 __pycache__/
67 python_magic.egg-info
78 pip-selfcheck.json
89 pyvenv.cfg
1313 'PDF document, version 1.2'
1414 >>>
1515
16
1716 """
1817
1918 import sys
3433 class Magic:
3534 """
3635 Magic is a wrapper around the libmagic C library.
37
3836 """
3937
4038 def __init__(self, mime=False, magic_file=None, mime_encoding=False,
41 keep_going=False, uncompress=False):
39 keep_going=False, uncompress=False, raw=False):
4240 """
4341 Create a new libmagic wrapper.
4442
4745 magic_file - use a mime database other than the system default
4846 keep_going - don't stop at the first match, keep going
4947 uncompress - Try to look inside compressed files.
48 raw - Do not try to decode "non-printable" chars.
5049 """
5150 self.flags = MAGIC_NONE
5251 if mime:
5554 self.flags |= MAGIC_MIME_ENCODING
5655 if keep_going:
5756 self.flags |= MAGIC_CONTINUE
58
5957 if uncompress:
6058 self.flags |= MAGIC_COMPRESS
59 if raw:
60 self.flags |= MAGIC_RAW
6161
6262 self.cookie = magic_open(self.flags)
6363 self.lock = threading.Lock()
189189
190190 magic_t = ctypes.c_void_p
191191
192
192193 def errorcheck_null(result, func, args):
193194 if result is None:
194195 err = magic_error(args[0])
195196 raise MagicException(err)
196197 else:
197198 return result
199
198200
199201 def errorcheck_negative_one(result, func, args):
200202 if result is -1:
211213 return s
212214 else:
213215 return s.decode('utf-8')
216
214217
215218 def coerce_filename(filename):
216219 if filename is None:
229232 else:
230233 return filename
231234
235
232236 magic_open = libmagic.magic_open
233237 magic_open.restype = magic_t
234238 magic_open.argtypes = [c_int]
250254 _magic_file.argtypes = [magic_t, c_char_p]
251255 _magic_file.errcheck = errorcheck_null
252256
257
253258 def magic_file(cookie, filename):
254259 return _magic_file(cookie, coerce_filename(filename))
255260
258263 _magic_buffer.argtypes = [magic_t, c_void_p, c_size_t]
259264 _magic_buffer.errcheck = errorcheck_null
260265
266
261267 def magic_buffer(cookie, buf):
262268 return _magic_buffer(cookie, buf, len(buf))
263269
267273 _magic_load.argtypes = [magic_t, c_char_p]
268274 _magic_load.errcheck = errorcheck_negative_one
269275
276
270277 def magic_load(cookie, filename):
271278 return _magic_load(cookie, coerce_filename(filename))
272279
287294 _magic_setparam.argtypes = [magic_t, c_int, POINTER(c_size_t)]
288295 _magic_setparam.errcheck = errorcheck_negative_one
289296
297
290298 def magic_setparam(cookie, param, val):
291299 v = c_size_t(val)
292300 return _magic_setparam(cookie, param, byref(v))
296304 _magic_getparam.argtypes = [magic_t, c_int, POINTER(c_size_t)]
297305 _magic_getparam.errcheck = errorcheck_negative_one
298306
307
299308 def magic_getparam(cookie, param):
300309 val = c_size_t()
301310 _magic_getparam(cookie, param, byref(val))
302311 return val.value
312
303313
304314 MAGIC_NONE = 0x000000 # No flags
305315 MAGIC_DEBUG = 0x000001 # Turn on debugging
8383 finally:
8484 del os.environ['TZ']
8585
86 def test_unicode_result_nonraw(self):
87 m = magic.Magic(raw=False)
88 src = os.path.join(MagicTest.TESTDATA_DIR, 'pgpunicode')
89 result = m.from_file(src)
90 # NOTE: This check is added as otherwise some magic files don't identify the test case as a PGP key.
91 if 'PGP' in result:
92 assert r"PGP\011Secret Sub-key -" == result
93 else:
94 raise unittest.SkipTest("Magic file doesn't return expected type.")
95
96 def test_unicode_result_raw(self):
97 m = magic.Magic(raw=True)
98 src = os.path.join(MagicTest.TESTDATA_DIR, 'pgpunicode')
99 result = m.from_file(src)
100 if 'PGP' in result:
101 assert b'PGP\tSecret Sub-key -' == result.encode('utf-8')
102 else:
103 raise unittest.SkipTest("Magic file doesn't return expected type.")
104
86105 def test_mime_encodings(self):
87106 m = magic.Magic(mime_encoding=True)
88107 self.assert_values(m, {