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

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