1
0

0.4.27-39-ga3ed086.unbreak-various-things.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. Subject: Unbreak various things
  2. Origin: upstream, commit 0.4.27-39-ga3ed086 <https://github.com/ahupp/python-magic/commit/0.4.27-39-ga3ed086>
  3. Author: Adam Hupp <adam@hupp.org>
  4. Date: Tue Feb 18 10:55:05 2025 -0800
  5. * A merge to reduce error spam during loading broke .so
  6. loading in at least some (maybe all?) cases, where find_library
  7. doesn't return an absolute path.
  8. * Prematurely pushed some in-progress test changes that were super broken, all fixed now.
  9. --- a/magic/loader.py
  10. +++ b/magic/loader.py
  11. @@ -7,6 +7,7 @@
  12. logger = logging.getLogger(__name__)
  13. +
  14. def _lib_candidates_linux():
  15. """Yield possible libmagic library names on Linux.
  16. @@ -51,7 +52,7 @@
  17. "darwin": _lib_candidates_macos,
  18. "linux": _lib_candidates_linux,
  19. "win32": _lib_candidates_windows,
  20. - "sunos5": _lib_candidates_linux,
  21. + "sunos5": _lib_candidates_linux,
  22. }.get(sys.platform)
  23. if func is None:
  24. raise ImportError("python-magic: Unsupported platform: " + sys.platform)
  25. @@ -61,17 +62,20 @@
  26. def load_lib():
  27. + exc = []
  28. for lib in _lib_candidates():
  29. # find_library returns None when lib not found
  30. if lib is None:
  31. continue
  32. - if not os.path.exists(lib):
  33. - continue
  34. try:
  35. return ctypes.CDLL(lib)
  36. - except OSError:
  37. - logger.warning("Failed to load: " + lib, exc_info=True)
  38. + except OSError as e:
  39. + exc.append(e)
  40. +
  41. + msg = "\n".join([str(e) for e in exc])
  42. # It is better to raise an ImportError since we are importing magic module
  43. - raise ImportError("python-magic: failed to find libmagic. Check your installation")
  44. + raise ImportError(
  45. + "python-magic: failed to find libmagic. Check your installation: \n" + msg
  46. + )
  47. --- a/test/python_magic_test.py
  48. +++ b/test/python_magic_test.py
  49. @@ -5,6 +5,7 @@
  50. import shutil
  51. import sys
  52. import tempfile
  53. +from typing import List, Union
  54. import unittest
  55. import pytest
  56. @@ -19,140 +20,162 @@
  57. import magic
  58. +
  59. @dataclass
  60. class TestFile:
  61. file_name: str
  62. - mime_results: list[str]
  63. - text_results: list[str]
  64. - no_check_elf_results: list[str] | None
  65. + mime_results: List[str]
  66. + text_results: List[str]
  67. + no_check_elf_results: Union[List[str], None]
  68. buf_equals_file: bool = True
  69. +
  70. # magic_descriptor is broken (?) in centos 7, so don't run those tests
  71. SKIP_FROM_DESCRIPTOR = bool(os.environ.get("SKIP_FROM_DESCRIPTOR"))
  72. -COMMON_PLAIN = [
  73. - {},
  74. - {"check_soft": True},
  75. - {"check_soft": False},
  76. - {"check_json": True},
  77. - {"check_json": False},
  78. -]
  79. -
  80. -NO_SOFT = {"check_soft": False}
  81. -
  82. -COMMON_MIME = [{"mime": True, **k} for k in COMMON_PLAIN]
  83. +COMMON_PLAIN = [{}]
  84. +NO_SOFT = [{"check_soft": False}]
  85. +COMMON_MIME = [{"mime": True}]
  86. CASES = {
  87. - "magic._pyc_": [
  88. - (COMMON_MIME, [
  89. - "application/octet-stream",
  90. - "text/x-bytecode.python",
  91. - "application/x-bytecode.python",
  92. - ]),
  93. + b"magic._pyc_": [
  94. + (
  95. + COMMON_MIME,
  96. + [
  97. + "application/octet-stream",
  98. + "text/x-bytecode.python",
  99. + "application/x-bytecode.python",
  100. + ],
  101. + ),
  102. (COMMON_PLAIN, ["python 2.4 byte-compiled"]),
  103. (NO_SOFT, ["data"]),
  104. ],
  105. - "test.pdf": [
  106. + b"test.pdf": [
  107. (COMMON_MIME, ["application/pdf"]),
  108. - (COMMON_PLAIN, [
  109. - "PDF document, version 1.2",
  110. - "PDF document, version 1.2, 2 pages",
  111. - "PDF document, version 1.2, 2 page(s)",
  112. - ]),
  113. + (
  114. + COMMON_PLAIN,
  115. + [
  116. + "PDF document, version 1.2",
  117. + "PDF document, version 1.2, 2 pages",
  118. + "PDF document, version 1.2, 2 page(s)",
  119. + ],
  120. + ),
  121. (NO_SOFT, ["ASCII text"]),
  122. ],
  123. - "test.gz": [
  124. + b"test.gz": [
  125. (COMMON_MIME, ["application/gzip", "application/x-gzip"]),
  126. - (COMMON_PLAIN, [
  127. - 'gzip compressed data, was "test", from Unix, last modified: Sun Jun 29 01:32:52 2008',
  128. - 'gzip compressed data, was "test", last modified: Sun Jun 29 01:32:52 2008, from Unix',
  129. - 'gzip compressed data, was "test", last modified: Sun Jun 29 01:32:52 2008, from Unix, original size 15',
  130. - 'gzip compressed data, was "test", last modified: Sun Jun 29 01:32:52 2008, from Unix, original size modulo 2^32 15',
  131. - 'gzip compressed data, was "test", last modified: Sun Jun 29 01:32:52 2008, from Unix, truncated',
  132. - ]),
  133. - ({"extension": True}, [
  134. - # some versions return '' for the extensions of a gz file,
  135. - # including w/ the command line. Who knows...
  136. - "gz/tgz/tpz/zabw/svgz/adz/kmy/xcfgz",
  137. - "gz/tgz/tpz/zabw/svgz",
  138. - "",
  139. - "???",
  140. - ]),
  141. + (
  142. + COMMON_PLAIN,
  143. + [
  144. + 'gzip compressed data, was "test", from Unix, last modified: Sun Jun 29 01:32:52 2008',
  145. + 'gzip compressed data, was "test", last modified: Sun Jun 29 01:32:52 2008, from Unix',
  146. + 'gzip compressed data, was "test", last modified: Sun Jun 29 01:32:52 2008, from Unix, original size 15',
  147. + 'gzip compressed data, was "test", last modified: Sun Jun 29 01:32:52 2008, from Unix, original size modulo 2^32 15',
  148. + 'gzip compressed data, was "test", last modified: Sun Jun 29 01:32:52 2008, from Unix, truncated',
  149. + ],
  150. + ),
  151. + (
  152. + [{"extension": True}],
  153. + [
  154. + # some versions return '' for the extensions of a gz file,
  155. + # including w/ the command line. Who knows...
  156. + "gz/tgz/tpz/zabw/svgz/adz/kmy/xcfgz",
  157. + "gz/tgz/tpz/zabw/svgz",
  158. + "",
  159. + "???",
  160. + ],
  161. + ),
  162. (NO_SOFT, ["data"]),
  163. ],
  164. - "test.snappy.parquet": [
  165. + b"test.snappy.parquet": [
  166. (COMMON_MIME, ["application/octet-stream"]),
  167. (COMMON_PLAIN, ["Apache Parquet", "Par archive data"]),
  168. (NO_SOFT, ["data"]),
  169. ],
  170. - "test.json": [
  171. - # TODO: soft, no_json
  172. + b"test.json": [
  173. (COMMON_MIME, ["application/json"]),
  174. (COMMON_PLAIN, ["JSON text data"]),
  175. - ({"mime": True, "check_json": False}, [
  176. - "data",
  177. - ]),
  178. - (NO_SOFT, ["JSON text data"])
  179. + (
  180. + [{"mime": True, "check_json": False}],
  181. + [
  182. + "text/plain",
  183. + ],
  184. + ),
  185. + (NO_SOFT, ["JSON text data"]),
  186. ],
  187. - "elf-NetBSD-x86_64-echo": [
  188. + b"elf-NetBSD-x86_64-echo": [
  189. # TODO: soft, no elf
  190. - (COMMON_PLAIN, [
  191. - "ELF 64-bit LSB shared object, x86-64, version 1 (SYSV)",
  192. - "ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /libexec/ld.elf_so, for NetBSD 8.0, not stripped",
  193. - ]),
  194. - (COMMON_MIME, [
  195. - "application/x-pie-executable",
  196. - "application/x-sharedlib",
  197. - ]),
  198. - ({"check_elf": False}, [
  199. - "ELF 64-bit LSB shared object, x86-64, version 1 (SYSV)",
  200. - ]),
  201. + (
  202. + COMMON_PLAIN,
  203. + [
  204. + "ELF 64-bit LSB shared object, x86-64, version 1 (SYSV)",
  205. + "ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /libexec/ld.elf_so, for NetBSD 8.0, not stripped",
  206. + ],
  207. + ),
  208. + (
  209. + COMMON_MIME,
  210. + [
  211. + "application/x-pie-executable",
  212. + "application/x-sharedlib",
  213. + ],
  214. + ),
  215. + (
  216. + [{"check_elf": False}],
  217. + [
  218. + "ELF 64-bit LSB shared object, x86-64, version 1 (SYSV)",
  219. + ],
  220. + ),
  221. # TODO: sometimes
  222. # "ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /libexec/ld.elf_so, for NetBSD 8.0, not stripped",
  223. -
  224. (NO_SOFT, ["data"]),
  225. ],
  226. - "test.txt": [
  227. + b"text.txt": [
  228. (COMMON_MIME, ["text/plain"]),
  229. (COMMON_PLAIN, ["ASCII text"]),
  230. - ({"mime_encoding": True}, [
  231. - "us-ascii",
  232. - ]),
  233. + (
  234. + [{"mime_encoding": True}],
  235. + [
  236. + "us-ascii",
  237. + ],
  238. + ),
  239. (NO_SOFT, ["ASCII text"]),
  240. ],
  241. - "text-iso8859-1.txt": [
  242. - ({"mime_encoding": True}, [
  243. - "iso-8859-1",
  244. - ]),
  245. + b"text-iso8859-1.txt": [
  246. + (
  247. + [{"mime_encoding": True}],
  248. + [
  249. + "iso-8859-1",
  250. + ],
  251. + ),
  252. ],
  253. b"\xce\xbb": [
  254. (COMMON_MIME, ["text/plain"]),
  255. ],
  256. - "b\xce\xbb".decode("utf-8"): [
  257. - (COMMON_MIME, ["text/plain"]),
  258. + b"name_use.jpg": [
  259. + ([{"extension": True}], ["jpeg/jpg/jpe/jfif"]),
  260. + ],
  261. + b"keep-going.jpg": [
  262. + (COMMON_MIME, ["image/jpeg"]),
  263. + (
  264. + [{"mime": True, "keep_going": True}],
  265. + [
  266. + "image/jpeg\\012- application/octet-stream",
  267. + ],
  268. + ),
  269. + ],
  270. + b"../../magic/loader.py": [
  271. + (
  272. + COMMON_MIME,
  273. + [
  274. + "text/x-python",
  275. + "text/x-script.python",
  276. + ],
  277. + )
  278. ],
  279. - "name_use.jpg": [
  280. - ({"extension": True}, [
  281. - "jpeg/jpg/jpe/jfif"
  282. - ]),
  283. - ],
  284. - "keep-going.jpg": [
  285. - (COMMON_MIME, [
  286. - "image/jpeg"
  287. - ]),
  288. - ({"mime": True, "keep_going": True}, [
  289. - "image/jpeg\\012- application/octet-stream",
  290. - ])
  291. - ],
  292. - "test.py": [
  293. - (COMMON_MIME, [
  294. - "text/x-python",
  295. - "text/x-script.python",
  296. - ])
  297. - ]
  298. }
  299. +
  300. class MagicTest(unittest.TestCase):
  301. TESTDATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "testdata"))
  302. @@ -165,7 +188,6 @@
  303. def test_fs_encoding(self):
  304. self.assertEqual("utf-8", sys.getfilesystemencoding().lower())
  305. -
  306. def test_from_file_str_and_bytes(self):
  307. filename = os.path.join(self.TESTDATA_DIR, "test.pdf")
  308. @@ -174,7 +196,6 @@
  309. "application/pdf", magic.from_file(filename.encode("utf-8"), mime=True)
  310. )
  311. -
  312. def test_all_cases(self):
  313. # TODO:
  314. # * MAGIC_EXTENSION not supported
  315. @@ -184,21 +205,24 @@
  316. shutil.copyfile(os.path.join(MagicTest.TESTDATA_DIR, "lambda"), dest)
  317. os.environ["TZ"] = "UTC"
  318. try:
  319. - for file_name, cases in CASES:
  320. - filename = os.path.join(self.TESTDATA_DIR, file_name)
  321. - for flags, outputs in cases:
  322. - m = magic.Magic(**flags)
  323. - with open(filename) as f:
  324. - self.assertIn(m.from_descriptor(f.fileno()), outputs)
  325. -
  326. - self.assertIn(m.from_file(filename), outputs)
  327. -
  328. - fname_bytes = filename.encode("utf-8")
  329. - self.assertIn(m.from_file(fname_bytes), outputs)
  330. -
  331. - with open(file_name, "rb") as f:
  332. - buf_result = m.from_buffer(f.read(1024))
  333. - self.assertIn(buf_result, outputs)
  334. + for filename, cases in CASES.items():
  335. + filename = os.path.join(self.TESTDATA_DIR.encode("utf-8"), filename)
  336. + print("test case ", filename, file=sys.stderr)
  337. + for flag_variants, outputs in cases:
  338. + for flags in flag_variants:
  339. + print("flags", flags, file=sys.stderr)
  340. + m = magic.Magic(**flags)
  341. + with open(filename) as f:
  342. + self.assertIn(m.from_descriptor(f.fileno()), outputs)
  343. +
  344. + self.assertIn(m.from_file(filename), outputs)
  345. +
  346. + fname_str = filename.decode("utf-8")
  347. + self.assertIn(m.from_file(fname_str), outputs)
  348. +
  349. + with open(filename, "rb") as f:
  350. + buf_result = m.from_buffer(f.read(1024))
  351. + self.assertIn(buf_result, outputs)
  352. finally:
  353. del os.environ["TZ"]
  354. os.unlink(dest)
  355. @@ -222,7 +246,6 @@
  356. else:
  357. raise unittest.SkipTest("Magic file doesn't return expected type.")
  358. -
  359. def test_errors(self):
  360. m = magic.Magic()
  361. self.assertRaises(IOError, m.from_file, "nonexistent")
  362. @@ -233,7 +256,6 @@
  363. finally:
  364. del os.environ["MAGIC"]
  365. -
  366. def test_rethrow(self):
  367. old = magic.magic_buffer
  368. try: