1
0

0.4.27-38-g36ecbf9.update-magic-compat-py.patch 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. Subject: Update magic/compat.py
  2. Origin: upstream, commit 0.4.27-38-g36ecbf9 <https://github.com/ahupp/python-magic/commit/0.4.27-38-g36ecbf9>
  3. Author: Adam Hupp <adam@hupp.org>
  4. Date: Mon Aug 5 09:24:16 2024 -0700
  5. This pulls changes from https://github.com/file/file, commit
  6. 512840337ead1076519332d24fefcaa8fac36e06
  7. --- a/.gitignore
  8. +++ b/.gitignore
  9. @@ -11,3 +11,4 @@
  10. *.pyc
  11. *~
  12. dist/
  13. +.vscode/
  14. --- a/magic/compat.py
  15. +++ b/magic/compat.py
  16. @@ -4,14 +4,12 @@
  17. Python bindings for libmagic
  18. '''
  19. -import ctypes
  20. -
  21. +import threading
  22. from collections import namedtuple
  23. from ctypes import *
  24. from ctypes.util import find_library
  25. -
  26. from . import loader
  27. _libraries = {}
  28. @@ -45,13 +43,19 @@
  29. MAGIC_NO_CHECK_BUILTIN = NO_CHECK_BUILTIN = 4173824
  30. +MAGIC_PARAM_INDIR_MAX = PARAM_INDIR_MAX = 0
  31. +MAGIC_PARAM_NAME_MAX = PARAM_NAME_MAX = 1
  32. +MAGIC_PARAM_ELF_PHNUM_MAX = PARAM_ELF_PHNUM_MAX = 2
  33. +MAGIC_PARAM_ELF_SHNUM_MAX = PARAM_ELF_SHNUM_MAX = 3
  34. +MAGIC_PARAM_ELF_NOTES_MAX = PARAM_ELF_NOTES_MAX = 4
  35. +MAGIC_PARAM_REGEX_MAX = PARAM_REGEX_MAX = 5
  36. +MAGIC_PARAM_BYTES_MAX = PARAM_BYTES_MAX = 6
  37. +
  38. FileMagic = namedtuple('FileMagic', ('mime_type', 'encoding', 'name'))
  39. class magic_set(Structure):
  40. pass
  41. -
  42. -
  43. magic_set._fields_ = []
  44. magic_t = POINTER(magic_set)
  45. @@ -103,6 +107,14 @@
  46. _errno.restype = c_int
  47. _errno.argtypes = [magic_t]
  48. +_getparam = _libraries['magic'].magic_getparam
  49. +_getparam.restype = c_int
  50. +_getparam.argtypes = [magic_t, c_int, c_void_p]
  51. +
  52. +_setparam = _libraries['magic'].magic_setparam
  53. +_setparam.restype = c_int
  54. +_setparam.argtypes = [magic_t, c_int, c_void_p]
  55. +
  56. class Magic(object):
  57. def __init__(self, ms):
  58. @@ -228,29 +240,81 @@
  59. """
  60. return _errno(self._magic_t)
  61. + def getparam(self, param):
  62. + """
  63. + Returns the param value if successful and -1 if the parameter
  64. + was unknown.
  65. + """
  66. + v = c_int()
  67. + i = _getparam(self._magic_t, param, byref(v))
  68. + if i == -1:
  69. + return -1
  70. + return v.value
  71. +
  72. + def setparam(self, param, value):
  73. + """
  74. + Returns 0 if successful and -1 if the parameter was unknown.
  75. + """
  76. + v = c_int(value)
  77. + return _setparam(self._magic_t, param, byref(v))
  78. +
  79. def open(flags):
  80. """
  81. Returns a magic object on success and None on failure.
  82. Flags argument as for setflags.
  83. """
  84. - return Magic(_open(flags))
  85. + magic_t = _open(flags)
  86. + if magic_t is None:
  87. + return None
  88. + return Magic(magic_t)
  89. # Objects used by `detect_from_` functions
  90. -mime_magic = Magic(_open(MAGIC_MIME))
  91. -mime_magic.load()
  92. -none_magic = Magic(_open(MAGIC_NONE))
  93. -none_magic.load()
  94. +class error(Exception):
  95. + pass
  96. +class MagicDetect(object):
  97. + def __init__(self):
  98. + self.mime_magic = open(MAGIC_MIME)
  99. + if self.mime_magic is None:
  100. + raise error
  101. + if self.mime_magic.load() == -1:
  102. + self.mime_magic.close()
  103. + self.mime_magic = None
  104. + raise error
  105. + self.none_magic = open(MAGIC_NONE)
  106. + if self.none_magic is None:
  107. + self.mime_magic.close()
  108. + self.mime_magic = None
  109. + raise error
  110. + if self.none_magic.load() == -1:
  111. + self.none_magic.close()
  112. + self.none_magic = None
  113. + self.mime_magic.close()
  114. + self.mime_magic = None
  115. + raise error
  116. +
  117. + def __del__(self):
  118. + if self.mime_magic is not None:
  119. + self.mime_magic.close()
  120. + if self.none_magic is not None:
  121. + self.none_magic.close()
  122. +
  123. +threadlocal = threading.local()
  124. +
  125. +def _detect_make():
  126. + v = getattr(threadlocal, "magic_instance", None)
  127. + if v is None:
  128. + v = MagicDetect()
  129. + setattr(threadlocal, "magic_instance", v)
  130. + return v
  131. def _create_filemagic(mime_detected, type_detected):
  132. - splat = mime_detected.split('; ')
  133. - mime_type = splat[0]
  134. - if len(splat) == 2:
  135. - mime_encoding = splat[1]
  136. - else:
  137. - mime_encoding = ''
  138. + try:
  139. + mime_type, mime_encoding = mime_detected.split('; ')
  140. + except ValueError:
  141. + raise ValueError(mime_detected)
  142. return FileMagic(name=type_detected, mime_type=mime_type,
  143. encoding=mime_encoding.replace('charset=', ''))
  144. @@ -261,9 +325,9 @@
  145. Returns a `FileMagic` namedtuple.
  146. '''
  147. -
  148. - return _create_filemagic(mime_magic.file(filename),
  149. - none_magic.file(filename))
  150. + x = _detect_make()
  151. + return _create_filemagic(x.mime_magic.file(filename),
  152. + x.none_magic.file(filename))
  153. def detect_from_fobj(fobj):
  154. @@ -273,8 +337,9 @@
  155. '''
  156. file_descriptor = fobj.fileno()
  157. - return _create_filemagic(mime_magic.descriptor(file_descriptor),
  158. - none_magic.descriptor(file_descriptor))
  159. + x = _detect_make()
  160. + return _create_filemagic(x.mime_magic.descriptor(file_descriptor),
  161. + x.none_magic.descriptor(file_descriptor))
  162. def detect_from_content(byte_content):
  163. @@ -283,5 +348,6 @@
  164. Returns a `FileMagic` namedtuple.
  165. '''
  166. - return _create_filemagic(mime_magic.buffer(byte_content),
  167. - none_magic.buffer(byte_content))
  168. + x = _detect_make()
  169. + return _create_filemagic(x.mime_magic.buffer(byte_content),
  170. + x.none_magic.buffer(byte_content))