1
0

0.4.27-41-g62bd3c6.format-with-ruff.patch 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. Subject: Format with ruff
  2. Origin: upstream, commit 0.4.27-41-g62bd3c6 <https://github.com/ahupp/python-magic/commit/0.4.27-41-g62bd3c6>
  3. Author: Adam Hupp <adam@hupp.org>
  4. Date: Sat Mar 1 17:10:13 2025 -0800
  5. Forwarded: not-needed
  6. --- a/magic/__init__.py
  7. +++ b/magic/__init__.py
  8. @@ -38,12 +38,27 @@
  9. Magic is a wrapper around the libmagic C library.
  10. """
  11. - def __init__(self, mime=False, magic_file=None, mime_encoding=False,
  12. - keep_going=False, uncompress=False, raw=False, extension=False,
  13. - follow_symlinks=False, check_tar=True, check_soft=True,
  14. - check_apptype=True, check_elf=True, check_text=True,
  15. - check_cdf=True, check_csv=True, check_encoding=True,
  16. - check_json=True, check_simh=True):
  17. + def __init__(
  18. + self,
  19. + mime=False,
  20. + magic_file=None,
  21. + mime_encoding=False,
  22. + keep_going=False,
  23. + uncompress=False,
  24. + raw=False,
  25. + extension=False,
  26. + follow_symlinks=False,
  27. + check_tar=True,
  28. + check_soft=True,
  29. + check_apptype=True,
  30. + check_elf=True,
  31. + check_text=True,
  32. + check_cdf=True,
  33. + check_csv=True,
  34. + check_encoding=True,
  35. + check_json=True,
  36. + check_simh=True,
  37. + ):
  38. """
  39. Create a new libmagic wrapper.
  40. @@ -101,7 +116,9 @@
  41. # MAGIC_EXTENSION was added in 523 or 524, so bail if
  42. # it doesn't appear to be available
  43. if extension and (not _has_version or version() < 524):
  44. - raise NotImplementedError('MAGIC_EXTENSION is not supported in this version of libmagic')
  45. + raise NotImplementedError(
  46. + "MAGIC_EXTENSION is not supported in this version of libmagic"
  47. + )
  48. # For https://github.com/ahupp/python-magic/issues/190
  49. # libmagic has fixed internal limits that some files exceed, causing
  50. @@ -128,7 +145,7 @@
  51. # which is not what libmagic expects
  52. # NEXTBREAK: only take bytes
  53. if type(buf) == str and str != bytes:
  54. - buf = buf.encode('utf-8', errors='replace')
  55. + buf = buf.encode("utf-8", errors="replace")
  56. return maybe_decode(magic_buffer(self.cookie, buf))
  57. except MagicException as e:
  58. return self._handle509Bug(e)
  59. @@ -176,7 +193,7 @@
  60. # incorrect fix for a threading problem, however I'm leaving
  61. # it in because it's harmless and I'm slightly afraid to
  62. # remove it.
  63. - if hasattr(self, 'cookie') and self.cookie and magic_close:
  64. + if hasattr(self, "cookie") and self.cookie and magic_close:
  65. magic_close(self.cookie)
  66. self.cookie = None
  67. @@ -192,7 +209,7 @@
  68. def from_file(filename, mime=False):
  69. - """"
  70. + """
  71. Accepts a filename and returns the detected filetype. Return
  72. value is the mimetype if mime=True, otherwise a human readable
  73. name.
  74. @@ -230,7 +247,9 @@
  75. m = _get_magic_type(mime)
  76. return m.from_descriptor(fd)
  77. +
  78. from . import loader
  79. +
  80. libmagic = loader.load_lib()
  81. magic_t = ctypes.c_void_p
  82. @@ -261,20 +280,23 @@
  83. else:
  84. # backslashreplace here because sometimes libmagic will return metadata in the charset
  85. # of the file, which is unknown to us (e.g the title of a Word doc)
  86. - return s.decode('utf-8', 'backslashreplace')
  87. + return s.decode("utf-8", "backslashreplace")
  88. try:
  89. from os import PathLike
  90. +
  91. def unpath(filename):
  92. if isinstance(filename, PathLike):
  93. return filename.__fspath__()
  94. else:
  95. return filename
  96. except ImportError:
  97. +
  98. def unpath(filename):
  99. return filename
  100. +
  101. def coerce_filename(filename):
  102. if filename is None:
  103. return None
  104. @@ -286,12 +308,11 @@
  105. # then you'll get inconsistent behavior (crashes) depending on the user's
  106. # LANG environment variable
  107. # NEXTBREAK: remove
  108. - is_unicode = (sys.version_info[0] <= 2 and
  109. - isinstance(filename, unicode)) or \
  110. - (sys.version_info[0] >= 3 and
  111. - isinstance(filename, str))
  112. + is_unicode = (sys.version_info[0] <= 2 and isinstance(filename, unicode)) or (
  113. + sys.version_info[0] >= 3 and isinstance(filename, str)
  114. + )
  115. if is_unicode:
  116. - return filename.encode('utf-8', 'surrogateescape')
  117. + return filename.encode("utf-8", "surrogateescape")
  118. else:
  119. return filename
  120. @@ -370,7 +391,7 @@
  121. magic_compile.argtypes = [magic_t, c_char_p]
  122. _has_param = False
  123. -if hasattr(libmagic, 'magic_setparam') and hasattr(libmagic, 'magic_getparam'):
  124. +if hasattr(libmagic, "magic_setparam") and hasattr(libmagic, "magic_getparam"):
  125. _has_param = True
  126. _magic_setparam = libmagic.magic_setparam
  127. _magic_setparam.restype = c_int
  128. @@ -443,8 +464,8 @@
  129. MAGIC_NO_CHECK_CDF = 0x0040000 # Don't check for CDF files
  130. MAGIC_NO_CHECK_CSV = 0x0080000 # Don't check for CSV files
  131. MAGIC_NO_CHECK_ENCODING = 0x0200000 # Don't check text encodings
  132. -MAGIC_NO_CHECK_JSON = 0x0400000 # Don't check for JSON files
  133. -MAGIC_NO_CHECK_SIMH = 0x0800000 # Don't check for SIMH tape files
  134. +MAGIC_NO_CHECK_JSON = 0x0400000 # Don't check for JSON files
  135. +MAGIC_NO_CHECK_SIMH = 0x0800000 # Don't check for SIMH tape files
  136. MAGIC_PARAM_INDIR_MAX = 0 # Recursion limit for indirect magic
  137. MAGIC_PARAM_NAME_MAX = 1 # Use count limit for name/use magic
  138. @@ -468,22 +489,20 @@
  139. warnings.warn(
  140. "Using compatibility mode with libmagic's python binding. "
  141. "See https://github.com/ahupp/python-magic/blob/master/COMPAT.md for details.",
  142. - PendingDeprecationWarning)
  143. + PendingDeprecationWarning,
  144. + )
  145. return fn(*args, **kwargs)
  146. return _
  147. - fn = ['detect_from_filename',
  148. - 'detect_from_content',
  149. - 'detect_from_fobj',
  150. - 'open']
  151. + fn = ["detect_from_filename", "detect_from_content", "detect_from_fobj", "open"]
  152. for fname in fn:
  153. to_module[fname] = deprecation_wrapper(compat.__dict__[fname])
  154. # copy constants over, ensuring there's no conflicts
  155. is_const_re = re.compile("^[A-Z_]+$")
  156. - allowed_inconsistent = set(['MAGIC_MIME'])
  157. + allowed_inconsistent = set(["MAGIC_MIME"])
  158. for name, value in compat.__dict__.items():
  159. if is_const_re.match(name):
  160. if name in to_module:
  161. --- a/magic/__init__.pyi
  162. +++ b/magic/__init__.pyi
  163. @@ -11,7 +11,25 @@
  164. flags: int = ...
  165. cookie: Any = ...
  166. lock: threading.Lock = ...
  167. - def __init__(self, mime: bool = ..., magic_file: Optional[Any] = ..., mime_encoding: bool = ..., keep_going: bool = ..., uncompress: bool = ..., raw: bool = ..., extension: bool = ..., follow_symlinks: bool = ..., check_tar: bool = ..., check_soft: bool = ..., check_apptype: bool = ..., check_elf: bool = ..., check_text: bool = ..., check_encoding: bool = ..., check_json: bool = ..., check_simh: bool = ...) -> None: ...
  168. + def __init__(
  169. + self,
  170. + mime: bool = ...,
  171. + magic_file: Optional[Any] = ...,
  172. + mime_encoding: bool = ...,
  173. + keep_going: bool = ...,
  174. + uncompress: bool = ...,
  175. + raw: bool = ...,
  176. + extension: bool = ...,
  177. + follow_symlinks: bool = ...,
  178. + check_tar: bool = ...,
  179. + check_soft: bool = ...,
  180. + check_apptype: bool = ...,
  181. + check_elf: bool = ...,
  182. + check_text: bool = ...,
  183. + check_encoding: bool = ...,
  184. + check_json: bool = ...,
  185. + check_simh: bool = ...,
  186. + ) -> None: ...
  187. def from_buffer(self, buf: Union[bytes, str]) -> Text: ...
  188. def from_file(self, filename: Union[bytes, str, PathLike]) -> Text: ...
  189. def from_descriptor(self, fd: int, mime: bool = ...) -> Text: ...
  190. --- /dev/null
  191. +++ b/ruff.toml
  192. @@ -0,0 +1,3 @@
  193. +exclude = ["magic/compat.py"]
  194. +
  195. +
  196. --- a/test/libmagic_test.py
  197. +++ b/test/libmagic_test.py
  198. @@ -6,16 +6,20 @@
  199. import os.path
  200. # magic_descriptor is broken (?) in centos 7, so don't run those tests
  201. -SKIP_FROM_DESCRIPTOR = bool(os.environ.get('SKIP_FROM_DESCRIPTOR'))
  202. +SKIP_FROM_DESCRIPTOR = bool(os.environ.get("SKIP_FROM_DESCRIPTOR"))
  203. -TESTDATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), 'testdata'))
  204. +TESTDATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "testdata"))
  205. class MagicTestCase(unittest.TestCase):
  206. - filename = os.path.join(TESTDATA_DIR, 'test.pdf')
  207. - expected_mime_type = 'application/pdf'
  208. - expected_encoding = 'us-ascii'
  209. - expected_name = ('PDF document, version 1.2', 'PDF document, version 1.2, 2 pages', 'PDF document, version 1.2, 2 page(s)')
  210. + filename = os.path.join(TESTDATA_DIR, "test.pdf")
  211. + expected_mime_type = "application/pdf"
  212. + expected_encoding = "us-ascii"
  213. + expected_name = (
  214. + "PDF document, version 1.2",
  215. + "PDF document, version 1.2, 2 pages",
  216. + "PDF document, version 1.2, 2 page(s)",
  217. + )
  218. def assert_result(self, result):
  219. self.assertEqual(result.mime_type, self.expected_mime_type)
  220. @@ -27,11 +31,9 @@
  221. self.assert_result(result)
  222. def test_detect_from_fobj(self):
  223. -
  224. if SKIP_FROM_DESCRIPTOR:
  225. self.skipTest("magic_descriptor is broken in this version of libmagic")
  226. -
  227. with open(self.filename) as fobj:
  228. result = magic.detect_from_fobj(fobj)
  229. self.assert_result(result)
  230. @@ -41,10 +43,10 @@
  231. # this avoids hitting a bug in python3+libfile bindings
  232. # see https://github.com/ahupp/python-magic/issues/152
  233. # for a similar issue
  234. - with open(self.filename, 'rb') as fobj:
  235. + with open(self.filename, "rb") as fobj:
  236. result = magic.detect_from_content(fobj.read(4096))
  237. self.assert_result(result)
  238. -if __name__ == '__main__':
  239. +if __name__ == "__main__":
  240. unittest.main()