1
0

0.4.27-31-gcf21065.clean-up-loader-py.patch 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. Subject: Clean up loader.py
  2. Origin: upstream, commit 0.4.27-31-gcf21065 <https://github.com/ahupp/python-magic/commit/0.4.27-31-gcf21065>
  3. Author: Christian Clauss <cclauss@me.com>
  4. Date: Wed May 22 16:15:04 2024 +0200
  5. Forwarded: not-needed
  6. --- a/magic/loader.py
  7. +++ b/magic/loader.py
  8. @@ -4,47 +4,65 @@
  9. import glob
  10. import os.path
  11. -def _lib_candidates():
  12. - yield find_library('magic')
  13. +def _lib_candidates_linux():
  14. + """Yield possible libmagic library names on Linux.
  15. +
  16. + This is necessary because alpine is bad
  17. + """
  18. + yield "libmagic.so.1"
  19. - if sys.platform == 'darwin':
  20. +def _lib_candidates_macos():
  21. + """Yield possible libmagic library names on macOS."""
  22. paths = [
  23. - '/opt/local/lib',
  24. - '/usr/local/lib',
  25. - '/opt/homebrew/lib',
  26. - ] + glob.glob('/usr/local/Cellar/libmagic/*/lib')
  27. -
  28. - for i in paths:
  29. - yield os.path.join(i, 'libmagic.dylib')
  30. -
  31. - elif sys.platform in ('win32', 'cygwin'):
  32. -
  33. - prefixes = ['libmagic', 'magic1', 'magic-1', 'cygmagic-1', 'libmagic-1', 'msys-magic-1']
  34. -
  35. - for i in prefixes:
  36. - # find_library searches in %PATH% but not the current directory,
  37. - # so look for both
  38. - yield './%s.dll' % (i,)
  39. - yield find_library(i)
  40. -
  41. - elif sys.platform == 'linux':
  42. - # This is necessary because alpine is bad
  43. - yield 'libmagic.so.1'
  44. + "/opt/homebrew/lib",
  45. + "/opt/local/lib",
  46. + "/usr/local/lib",
  47. + ] + glob.glob("/usr/local/Cellar/libmagic/*/lib")
  48. + for path in paths:
  49. + yield os.path.join(path, "libmagic.dylib")
  50. +
  51. +
  52. +def _lib_candidates_windows():
  53. + """Yield possible libmagic library names on Windows."""
  54. + prefixes = (
  55. + "libmagic",
  56. + "magic1",
  57. + "magic-1",
  58. + "cygmagic-1",
  59. + "libmagic-1",
  60. + "msys-magic-1",
  61. + )
  62. + for prefix in prefixes:
  63. + # find_library searches in %PATH% but not the current directory,
  64. + # so look for both
  65. + yield "./%s.dll" % (prefix,)
  66. + yield find_library(prefix)
  67. +
  68. +
  69. +def _lib_candidates():
  70. + yield find_library("magic")
  71. +
  72. + func = {
  73. + "cygwin": _lib_candidates_windows,
  74. + "darwin": _lib_candidates_macos,
  75. + "linux": _lib_candidates_linux,
  76. + "win32": _lib_candidates_windows,
  77. + }[sys.platform]
  78. + # When we drop legacy Python, we can just `yield from func()`
  79. + for path in func():
  80. + yield path
  81. def load_lib():
  82. + for lib in _lib_candidates():
  83. + # find_library returns None when lib not found
  84. + if lib:
  85. + try:
  86. + return ctypes.CDLL(lib)
  87. + except OSError:
  88. + pass
  89. - for lib in _lib_candidates():
  90. - # find_library returns None when lib not found
  91. - if lib is None:
  92. - continue
  93. - try:
  94. - return ctypes.CDLL(lib)
  95. - except OSError:
  96. - pass
  97. - else:
  98. # It is better to raise an ImportError since we are importing magic module
  99. - raise ImportError('failed to find libmagic. Check your installation')
  100. -
  101. + raise ImportError("failed to find libmagic. Check your installation")