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

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