Skip to content

Commit 9dac67c

Browse files
committed
Improve decoding of path on errors
Also: - move file_type() towards the top - make libmagic_version a function to avoid unnecessary calls at import time Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent c38f683 commit 9dac67c

2 files changed

Lines changed: 33 additions & 28 deletions

File tree

src/typecode/magic2.py

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
import ctypes
3232
import glob
3333
import os
34+
import warnings
3435

3536
from commoncode import command
3637
from commoncode.system import on_windows
37-
import warnings
3838

3939
"""
4040
magic2 is minimal and specialized wrapper around a vendored libmagic file
@@ -85,6 +85,24 @@ def logger_debug(*args):
8585
TYPECODE_LIBMAGIC_PATH_ENVVAR = 'TYPECODE_LIBMAGIC_PATH'
8686
TYPECODE_LIBMAGIC_DB_PATH_ENVVAR = 'TYPECODE_LIBMAGIC_DB_PATH'
8787

88+
if TRACE:
89+
90+
def file_type(location):
91+
return _detect(location, DETECT_TYPE)
92+
93+
else:
94+
95+
def file_type(location):
96+
""""
97+
Return the detected filetype for file at `location` or an empty string if
98+
nothing found or an error occurred.
99+
"""
100+
try:
101+
return _detect(location, DETECT_TYPE)
102+
except:
103+
# TODO: log errors
104+
return ''
105+
88106

89107
class NoMagicLibError(Exception):
90108
"""
@@ -150,6 +168,7 @@ def load_lib():
150168
Return the libmagic shared library object loaded from either:
151169
- an environment variable ``TYPECODE_LIBMAGIC_PATH``
152170
- a plugin-provided path,
171+
- well known system names and locations,
153172
- the system PATH.
154173
Raise an NoMagicLibError if no libmagic can be found.
155174
"""
@@ -259,25 +278,6 @@ def get_magicdb_location(_cache=[]):
259278
return magicdb_loc
260279

261280

262-
if TRACE:
263-
264-
def file_type(location):
265-
return _detect(location, DETECT_TYPE)
266-
267-
else:
268-
269-
def file_type(location):
270-
""""
271-
Return the detected filetype for file at `location` or an empty string if
272-
nothing found or an error occurred.
273-
"""
274-
try:
275-
return _detect(location, DETECT_TYPE)
276-
except:
277-
# TODO: log errors
278-
return ''
279-
280-
281281
def mime_type(location):
282282
""""
283283
Return the detected mimetype for file at `location` or an empty string if
@@ -377,6 +377,10 @@ def __del__(self):
377377
libmagic = load_lib()
378378

379379

380+
def libmagic_version():
381+
return _magic_version()
382+
383+
380384
def check_error(result, func, args): # NOQA
381385
"""
382386
ctypes error handler/checker: Check for errors and raise an exception or
@@ -385,11 +389,14 @@ def check_error(result, func, args): # NOQA
385389
is_int = isinstance(result, int)
386390
is_bytes = isinstance(result, bytes)
387391
is_text = isinstance(result, str)
388-
389-
if (result is None
390-
or (is_int and result < 0)
391-
or (is_bytes and str(result, encoding='utf-8').startswith('cannot open'))
392-
or (is_text and result.startswith('cannot open'))):
392+
if (
393+
result is None
394+
or (is_int and result < 0)
395+
or (
396+
is_bytes
397+
and str(result, encoding='utf-8', errors='ignore').startswith('cannot open'))
398+
or (is_text and result.startswith('cannot open'))
399+
):
393400
err = _magic_error(args[0])
394401
raise MagicException(err)
395402
else:
@@ -427,5 +434,3 @@ def check_error(result, func, args): # NOQA
427434
_magic_version = libmagic.magic_version
428435
_magic_version.restype = ctypes.c_int
429436
_magic_version.argtypes = []
430-
431-
libmagic_version = _magic_version()

tests/test_magic2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111

1212

1313
def test_load_lib():
14-
assert libmagic_version > 0
14+
assert float(libmagic_version()) > 5

0 commit comments

Comments
 (0)