| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- Subject: Update magic/compat.py
- Origin: upstream, commit 0.4.27-38-g36ecbf9 <https://github.com/ahupp/python-magic/commit/0.4.27-38-g36ecbf9>
- Author: Adam Hupp <adam@hupp.org>
- Date: Mon Aug 5 09:24:16 2024 -0700
- This pulls changes from https://github.com/file/file, commit
- 512840337ead1076519332d24fefcaa8fac36e06
- --- a/.gitignore
- +++ b/.gitignore
- @@ -11,3 +11,4 @@
- *.pyc
- *~
- dist/
- +.vscode/
- --- a/magic/compat.py
- +++ b/magic/compat.py
- @@ -4,14 +4,12 @@
- Python bindings for libmagic
- '''
-
- -import ctypes
- -
- +import threading
- from collections import namedtuple
-
- from ctypes import *
- from ctypes.util import find_library
-
- -
- from . import loader
-
- _libraries = {}
- @@ -45,13 +43,19 @@
-
- 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'))
-
-
- class magic_set(Structure):
- pass
- -
- -
- magic_set._fields_ = []
- magic_t = POINTER(magic_set)
-
- @@ -103,6 +107,14 @@
- _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):
- @@ -228,29 +240,81 @@
- """
- 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):
- """
- 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
- -mime_magic = Magic(_open(MAGIC_MIME))
- -mime_magic.load()
- -none_magic = Magic(_open(MAGIC_NONE))
- -none_magic.load()
- +class error(Exception):
- + pass
-
- +class MagicDetect(object):
- + def __init__(self):
- + 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):
- + 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()
- +
- +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):
- - splat = mime_detected.split('; ')
- - mime_type = splat[0]
- - if len(splat) == 2:
- - mime_encoding = splat[1]
- - else:
- - mime_encoding = ''
- + try:
- + mime_type, mime_encoding = mime_detected.split('; ')
- + except ValueError:
- + raise ValueError(mime_detected)
-
- return FileMagic(name=type_detected, mime_type=mime_type,
- encoding=mime_encoding.replace('charset=', ''))
- @@ -261,9 +325,9 @@
-
- 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):
- @@ -273,8 +337,9 @@
- '''
-
- 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):
- @@ -283,5 +348,6 @@
- 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))
|