summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorXin LI <delphij@FreeBSD.org>2022-01-03 01:44:41 +0000
committerXin LI <delphij@FreeBSD.org>2022-01-03 01:44:41 +0000
commit048bd4094ff308722af59e857c54819c9313875a (patch)
treeb9e408b4bcc6134b4111262b2c80c2fc96cc84cc /python
parent2d24dbcf45a851fa5c7960160b7b4a28ff373558 (diff)
Diffstat (limited to 'python')
-rw-r--r--python/CHANGELOG.md5
-rw-r--r--python/Makefile.am2
-rw-r--r--python/Makefile.in5
-rw-r--r--python/file_magic/__init__.py1
-rw-r--r--python/magic.py73
5 files changed, 72 insertions, 14 deletions
diff --git a/python/CHANGELOG.md b/python/CHANGELOG.md
index ac3c0c0e9455..2634986b9234 100644
--- a/python/CHANGELOG.md
+++ b/python/CHANGELOG.md
@@ -1,5 +1,10 @@
# Python `file-magic` Log of Changes
+## `0.4.1`
+
+- Create threadlocal objects so that the `detect_from_*` methods work properly
+
+
## `0.4.0`
- Sync with current version of file:
diff --git a/python/Makefile.am b/python/Makefile.am
index f2d3412b8658..9588f085df0f 100644
--- a/python/Makefile.am
+++ b/python/Makefile.am
@@ -1,4 +1,4 @@
EXTRA_DIST = LICENSE CHANGELOG.md README.md example.py magic.py setup.py \
- tests.py
+ tests.py file_magic/__init__.py
diff --git a/python/Makefile.in b/python/Makefile.in
index 9a9c880b7ba6..759bd2b648c9 100644
--- a/python/Makefile.in
+++ b/python/Makefile.in
@@ -91,7 +91,8 @@ ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
am__aclocal_m4_deps = $(top_srcdir)/m4/libtool.m4 \
$(top_srcdir)/m4/ltoptions.m4 $(top_srcdir)/m4/ltsugar.m4 \
$(top_srcdir)/m4/ltversion.m4 $(top_srcdir)/m4/lt~obsolete.m4 \
- $(top_srcdir)/acinclude.m4 $(top_srcdir)/configure.ac
+ $(top_srcdir)/m4/visibility.m4 $(top_srcdir)/acinclude.m4 \
+ $(top_srcdir)/configure.ac
am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \
$(ACLOCAL_M4)
DIST_COMMON = $(srcdir)/Makefile.am $(am__DIST_COMMON)
@@ -243,7 +244,7 @@ top_build_prefix = @top_build_prefix@
top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
EXTRA_DIST = LICENSE CHANGELOG.md README.md example.py magic.py setup.py \
- tests.py
+ tests.py file_magic/__init__.py
all: all-am
diff --git a/python/file_magic/__init__.py b/python/file_magic/__init__.py
new file mode 100644
index 000000000000..c71f1242950a
--- /dev/null
+++ b/python/file_magic/__init__.py
@@ -0,0 +1 @@
+name = 'file_magic'
diff --git a/python/magic.py b/python/magic.py
index 4c1ff7a7de7a..4b074f31c6ca 100644
--- a/python/magic.py
+++ b/python/magic.py
@@ -5,6 +5,7 @@ Python bindings for libmagic
'''
import ctypes
+import threading
from collections import namedtuple
@@ -50,6 +51,14 @@ MAGIC_NO_CHECK_ENCODING = NO_CHECK_ENCODING = 2097152
MAGIC_NO_CHECK_BUILTIN = NO_CHECK_BUILTIN = 4173824
+MAGIC_PARAM_INDIR_MAX = PARAM_INDIR_MAX = 0
+MAGIC_PARAM_NAME_MAX = PARAM_NAME_MAX = 1
+MAGIC_PARAM_ELF_PHNUM_MAX = PARAM_ELF_PHNUM_MAX = 2
+MAGIC_PARAM_ELF_SHNUM_MAX = PARAM_ELF_SHNUM_MAX = 3
+MAGIC_PARAM_ELF_NOTES_MAX = PARAM_ELF_NOTES_MAX = 4
+MAGIC_PARAM_REGEX_MAX = PARAM_REGEX_MAX = 5
+MAGIC_PARAM_BYTES_MAX = PARAM_BYTES_MAX = 6
+
FileMagic = namedtuple('FileMagic', ('mime_type', 'encoding', 'name'))
@@ -106,6 +115,14 @@ _errno = _libraries['magic'].magic_errno
_errno.restype = c_int
_errno.argtypes = [magic_t]
+_getparam = _libraries['magic'].magic_getparam
+_getparam.restype = c_int
+_getparam.argtypes = [magic_t, c_int, c_void_p]
+
+_setparam = _libraries['magic'].magic_setparam
+_setparam.restype = c_int
+_setparam.argtypes = [magic_t, c_int, c_void_p]
+
class Magic(object):
def __init__(self, ms):
@@ -231,6 +248,24 @@ class Magic(object):
"""
return _errno(self._magic_t)
+ def getparam(self, param):
+ """
+ Returns the param value if successful and -1 if the parameter
+ was unknown.
+ """
+ v = c_int()
+ i = _getparam(self._magic_t, param, byref(v))
+ if i == -1:
+ return -1
+ return v.value
+
+ def setparam(self, param, value):
+ """
+ Returns 0 if successful and -1 if the parameter was unknown.
+ """
+ v = c_int(value)
+ return _setparam(self._magic_t, param, byref(v))
+
def open(flags):
"""
@@ -241,11 +276,25 @@ def open(flags):
# Objects used by `detect_from_` functions
-mime_magic = Magic(_open(MAGIC_MIME))
-mime_magic.load()
-none_magic = Magic(_open(MAGIC_NONE))
-none_magic.load()
+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()
+ def __del__(self):
+ self.mime_magic.close()
+ self.none_magic.close()
+
+threadlocal = threading.local()
+
+def _detect_make():
+ v = getattr(threadlocal, "magic_instance", None)
+ if v is None:
+ v = MagicDetect()
+ setattr(threadlocal, "magic_instance", v)
+ return v
def _create_filemagic(mime_detected, type_detected):
try:
@@ -262,9 +311,9 @@ def detect_from_filename(filename):
Returns a `FileMagic` namedtuple.
'''
-
- return _create_filemagic(mime_magic.file(filename),
- none_magic.file(filename))
+ x = _detect_make()
+ return _create_filemagic(x.mime_magic.file(filename),
+ x.none_magic.file(filename))
def detect_from_fobj(fobj):
@@ -274,8 +323,9 @@ def detect_from_fobj(fobj):
'''
file_descriptor = fobj.fileno()
- return _create_filemagic(mime_magic.descriptor(file_descriptor),
- none_magic.descriptor(file_descriptor))
+ x = _detect_make()
+ return _create_filemagic(x.mime_magic.descriptor(file_descriptor),
+ x.none_magic.descriptor(file_descriptor))
def detect_from_content(byte_content):
@@ -284,5 +334,6 @@ def detect_from_content(byte_content):
Returns a `FileMagic` namedtuple.
'''
- return _create_filemagic(mime_magic.buffer(byte_content),
- none_magic.buffer(byte_content))
+ x = _detect_make()
+ return _create_filemagic(x.mime_magic.buffer(byte_content),
+ x.none_magic.buffer(byte_content))