diff options
| author | Xin LI <delphij@FreeBSD.org> | 2022-09-24 04:20:34 +0000 |
|---|---|---|
| committer | Xin LI <delphij@FreeBSD.org> | 2022-09-24 04:20:34 +0000 |
| commit | ac33800609941e32486585331e2d4af9ecdd554b (patch) | |
| tree | d643ef47f63c6734de36c5d0ee5fa380d35502da /python | |
| parent | 5457a3f25818dc2a14ec8a58c6f27dc42a50cdd4 (diff) | |
Diffstat (limited to 'python')
| -rw-r--r-- | python/Makefile.in | 12 | ||||
| -rw-r--r-- | python/magic.py | 36 | ||||
| -rw-r--r-- | python/tests.py | 8 |
3 files changed, 40 insertions, 16 deletions
diff --git a/python/Makefile.in b/python/Makefile.in index 759bd2b648c9..b3053e695bbd 100644 --- a/python/Makefile.in +++ b/python/Makefile.in @@ -1,7 +1,7 @@ -# Makefile.in generated by automake 1.16.1 from Makefile.am. +# Makefile.in generated by automake 1.16.5 from Makefile.am. # @configure_input@ -# Copyright (C) 1994-2018 Free Software Foundation, Inc. +# Copyright (C) 1994-2021 Free Software Foundation, Inc. # This Makefile.in is free software; the Free Software Foundation # gives unlimited permission to copy and/or distribute it, @@ -120,7 +120,7 @@ am__can_run_installinfo = \ *) (install-info --version) >/dev/null 2>&1;; \ esac am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP) -am__DIST_COMMON = $(srcdir)/Makefile.in +am__DIST_COMMON = $(srcdir)/Makefile.in README.md DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) pkgdatadir = @pkgdatadir@ ACLOCAL = @ACLOCAL@ @@ -135,8 +135,9 @@ CC = @CC@ CCDEPMODE = @CCDEPMODE@ CFLAGS = @CFLAGS@ CFLAG_VISIBILITY = @CFLAG_VISIBILITY@ -CPP = @CPP@ CPPFLAGS = @CPPFLAGS@ +CSCOPE = @CSCOPE@ +CTAGS = @CTAGS@ CYGPATH_W = @CYGPATH_W@ DEFS = @DEFS@ DEPDIR = @DEPDIR@ @@ -147,6 +148,7 @@ ECHO_C = @ECHO_C@ ECHO_N = @ECHO_N@ ECHO_T = @ECHO_T@ EGREP = @EGREP@ +ETAGS = @ETAGS@ EXEEXT = @EXEEXT@ FGREP = @FGREP@ GREP = @GREP@ @@ -235,6 +237,7 @@ pdfdir = @pdfdir@ prefix = @prefix@ program_transform_name = @program_transform_name@ psdir = @psdir@ +runstatedir = @runstatedir@ sbindir = @sbindir@ sharedstatedir = @sharedstatedir@ srcdir = @srcdir@ @@ -290,7 +293,6 @@ ctags CTAGS: cscope cscopelist: - distdir: $(BUILT_SOURCES) $(MAKE) $(AM_MAKEFLAGS) distdir-am diff --git a/python/magic.py b/python/magic.py index 4b074f31c6ca..4300ee0719a9 100644 --- a/python/magic.py +++ b/python/magic.py @@ -272,20 +272,42 @@ def open(flags): Returns a magic object on success and None on failure. Flags argument as for setflags. """ - return Magic(_open(flags)) + magic_t = _open(flags) + if magic_t is None: + return None + return Magic(magic_t) # Objects used by `detect_from_` functions +class error(Exception): + pass + class MagicDetect(object): def __init__(self): - self.mime_magic = Magic(_open(MAGIC_MIME)) - self.mime_magic.load() - self.none_magic = Magic(_open(MAGIC_NONE)) - self.none_magic.load() + self.mime_magic = open(MAGIC_MIME) + if self.mime_magic is None: + raise error + if self.mime_magic.load() == -1: + self.mime_magic.close() + self.mime_magic = None + raise error + self.none_magic = open(MAGIC_NONE) + if self.none_magic is None: + self.mime_magic.close() + self.mime_magic = None + raise error + if self.none_magic.load() == -1: + self.none_magic.close() + self.none_magic = None + self.mime_magic.close() + self.mime_magic = None + raise error def __del__(self): - self.mime_magic.close() - self.none_magic.close() + if self.mime_magic is not None: + self.mime_magic.close() + if self.none_magic is not None: + self.none_magic.close() threadlocal = threading.local() diff --git a/python/tests.py b/python/tests.py index 197a8fc4b519..3fc73c641be7 100644 --- a/python/tests.py +++ b/python/tests.py @@ -8,7 +8,7 @@ import magic class MagicTestCase(unittest.TestCase): filename = 'magic.py' - expected_mime_type = 'text/x-python' + expected_mime_type = 'text/x-script.python' expected_encoding = 'us-ascii' expected_name = 'Python script, ASCII text executable' @@ -22,11 +22,11 @@ class MagicTestCase(unittest.TestCase): self.assert_result(result) def test_detect_from_fobj(self): - with open(self.filename) as fobj: + with open(self.filename, "rb") as fobj: result = magic.detect_from_fobj(fobj) self.assert_result(result) def test_detect_from_content(self): - with open(self.filename) as fobj: - result = magic.detect_from_content(fobj.read(4096)) + with open(self.filename, "rb") as fobj: + result = magic.detect_from_content(fobj.read(8192)) self.assert_result(result) |
