Skip to content

Commit 4514350

Browse files
author
rmspeers
committed
Updated constructor to accept the raw flag and pass it to the underlying library. Helpful for cases where unicode chars exist in match results.
1 parent 3352868 commit 4514350

4 files changed

Lines changed: 35 additions & 4 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ bin/
44
deb_dist
55
htmlcov/
66
lib/
7+
__pycache__/
78
python_magic.egg-info
89
pip-selfcheck.json
910
pyvenv.cfg

magic.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
'PDF document, version 1.2'
1515
>>>
1616
17-
1817
"""
1918

2019
import sys
@@ -35,11 +34,10 @@ def __init__(self, message):
3534
class Magic:
3635
"""
3736
Magic is a wrapper around the libmagic C library.
38-
3937
"""
4038

4139
def __init__(self, mime=False, magic_file=None, mime_encoding=False,
42-
keep_going=False, uncompress=False):
40+
keep_going=False, uncompress=False, raw=False):
4341
"""
4442
Create a new libmagic wrapper.
4543
@@ -48,6 +46,7 @@ def __init__(self, mime=False, magic_file=None, mime_encoding=False,
4846
magic_file - use a mime database other than the system default
4947
keep_going - don't stop at the first match, keep going
5048
uncompress - Try to look inside compressed files.
49+
raw - Do not try to decode "non-printable" chars.
5150
"""
5251
self.flags = MAGIC_NONE
5352
if mime:
@@ -56,9 +55,10 @@ def __init__(self, mime=False, magic_file=None, mime_encoding=False,
5655
self.flags |= MAGIC_MIME_ENCODING
5756
if keep_going:
5857
self.flags |= MAGIC_CONTINUE
59-
6058
if uncompress:
6159
self.flags |= MAGIC_COMPRESS
60+
if raw:
61+
self.flags |= MAGIC_RAW
6262

6363
self.cookie = magic_open(self.flags)
6464
self.lock = threading.Lock()
@@ -190,13 +190,15 @@ def from_buffer(buffer, mime=False):
190190

191191
magic_t = ctypes.c_void_p
192192

193+
193194
def errorcheck_null(result, func, args):
194195
if result is None:
195196
err = magic_error(args[0])
196197
raise MagicException(err)
197198
else:
198199
return result
199200

201+
200202
def errorcheck_negative_one(result, func, args):
201203
if result is -1:
202204
err = magic_error(args[0])
@@ -213,6 +215,7 @@ def maybe_decode(s):
213215
else:
214216
return s.decode('utf-8')
215217

218+
216219
def coerce_filename(filename):
217220
if filename is None:
218221
return None
@@ -230,6 +233,7 @@ def coerce_filename(filename):
230233
else:
231234
return filename
232235

236+
233237
magic_open = libmagic.magic_open
234238
magic_open.restype = magic_t
235239
magic_open.argtypes = [c_int]
@@ -251,6 +255,7 @@ def coerce_filename(filename):
251255
_magic_file.argtypes = [magic_t, c_char_p]
252256
_magic_file.errcheck = errorcheck_null
253257

258+
254259
def magic_file(cookie, filename):
255260
return _magic_file(cookie, coerce_filename(filename))
256261

@@ -259,6 +264,7 @@ def magic_file(cookie, filename):
259264
_magic_buffer.argtypes = [magic_t, c_void_p, c_size_t]
260265
_magic_buffer.errcheck = errorcheck_null
261266

267+
262268
def magic_buffer(cookie, buf):
263269
return _magic_buffer(cookie, buf, len(buf))
264270

@@ -268,6 +274,7 @@ def magic_buffer(cookie, buf):
268274
_magic_load.argtypes = [magic_t, c_char_p]
269275
_magic_load.errcheck = errorcheck_negative_one
270276

277+
271278
def magic_load(cookie, filename):
272279
return _magic_load(cookie, coerce_filename(filename))
273280

@@ -288,6 +295,7 @@ def magic_load(cookie, filename):
288295
_magic_setparam.argtypes = [magic_t, c_int, POINTER(c_size_t)]
289296
_magic_setparam.errcheck = errorcheck_negative_one
290297

298+
291299
def magic_setparam(cookie, param, val):
292300
v = c_size_t(val)
293301
return _magic_setparam(cookie, param, byref(v))
@@ -297,11 +305,13 @@ def magic_setparam(cookie, param, val):
297305
_magic_getparam.argtypes = [magic_t, c_int, POINTER(c_size_t)]
298306
_magic_getparam.errcheck = errorcheck_negative_one
299307

308+
300309
def magic_getparam(cookie, param):
301310
val = c_size_t()
302311
_magic_getparam(cookie, param, byref(val))
303312
return val.value
304313

314+
305315
MAGIC_NONE = 0x000000 # No flags
306316
MAGIC_DEBUG = 0x000001 # Turn on debugging
307317
MAGIC_SYMLINK = 0x000002 # Follow symlinks

test/test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@ def test_descriptions(self):
8484
finally:
8585
del os.environ['TZ']
8686

87+
def test_unicode_result_nonraw(self):
88+
m = magic.Magic(raw=False)
89+
src = os.path.join(MagicTest.TESTDATA_DIR, 'pgpunicode')
90+
result = m.from_file(src)
91+
# NOTE: This check is added as otherwise some magic files don't identify the test case as a PGP key.
92+
if 'PGP' in result:
93+
assert r"PGP\011Secret Sub-key -" == result
94+
else:
95+
raise unittest.SkipTest("Magic file doesn't return expected type.")
96+
97+
def test_unicode_result_raw(self):
98+
m = magic.Magic(raw=True)
99+
src = os.path.join(MagicTest.TESTDATA_DIR, 'pgpunicode')
100+
result = m.from_file(src)
101+
if 'PGP' in result:
102+
assert b'PGP\tSecret Sub-key -' == result.encode('utf-8')
103+
else:
104+
raise unittest.SkipTest("Magic file doesn't return expected type.")
105+
87106
def test_mime_encodings(self):
88107
m = magic.Magic(mime_encoding=True)
89108
self.assert_values(m, {

test/testdata/pgpunicode

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
�qʐ

0 commit comments

Comments
 (0)