0.4.27-54-g4043553.revert-move-lock-to-global-scope.patch 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. Subject: Revert "Move lock to global scope"
  2. Origin: upstream, commit 0.4.27-54-g4043553 <https://github.com/ahupp/python-magic/commit/0.4.27-54-g4043553>
  3. Author: ddelange <14880945+ddelange@users.noreply.github.com>
  4. Date: Fri Oct 17 20:04:26 2025 +0300
  5. Bug-Debian: https://bugs.debian.org/4043553
  6. This reverts commit f2ac98d8aa7464165984068de9e484d0321cd4f3.
  7. --- a/magic/__init__.py
  8. +++ b/magic/__init__.py
  9. @@ -105,6 +105,7 @@
  10. self.flags |= MAGIC_NO_CHECK_SIMH
  11. self.cookie = magic_open(self.flags)
  12. + self.lock = threading.Lock()
  13. magic_load(self.cookie, magic_file)
  14. @@ -133,31 +134,34 @@
  15. """
  16. Identify the contents of `buf`
  17. """
  18. - try:
  19. - # if we're on python3, convert buf to bytes
  20. - # otherwise this string is passed as wchar*
  21. - # which is not what libmagic expects
  22. - # NEXTBREAK: only take bytes
  23. - if type(buf) == str and str != bytes:
  24. - buf = buf.encode("utf-8", errors="replace")
  25. - return maybe_decode(magic_buffer(self.cookie, buf))
  26. - except MagicException as e:
  27. - return self._handle509Bug(e)
  28. + with self.lock:
  29. + try:
  30. + # if we're on python3, convert buf to bytes
  31. + # otherwise this string is passed as wchar*
  32. + # which is not what libmagic expects
  33. + # NEXTBREAK: only take bytes
  34. + if type(buf) == str and str != bytes:
  35. + buf = buf.encode("utf-8", errors="replace")
  36. + return maybe_decode(magic_buffer(self.cookie, buf))
  37. + except MagicException as e:
  38. + return self._handle509Bug(e)
  39. def from_file(self, filename):
  40. # raise FileNotFoundException or IOError if the file does not exist
  41. os.stat(filename, follow_symlinks=self.flags & MAGIC_SYMLINK)
  42. - try:
  43. - return maybe_decode(magic_file(self.cookie, filename))
  44. - except MagicException as e:
  45. - return self._handle509Bug(e)
  46. + with self.lock:
  47. + try:
  48. + return maybe_decode(magic_file(self.cookie, filename))
  49. + except MagicException as e:
  50. + return self._handle509Bug(e)
  51. def from_descriptor(self, fd):
  52. - try:
  53. - return maybe_decode(magic_descriptor(self.cookie, fd))
  54. - except MagicException as e:
  55. - return self._handle509Bug(e)
  56. + with self.lock:
  57. + try:
  58. + return maybe_decode(magic_descriptor(self.cookie, fd))
  59. + except MagicException as e:
  60. + return self._handle509Bug(e)
  61. def _handle509Bug(self, e):
  62. # libmagic 5.09 has a bug where it might fail to identify the
  63. @@ -309,9 +313,6 @@
  64. return filename
  65. -# libmagic is not thread-safe: guard for concurrent calls on a global scope
  66. -LOCK = threading.Lock()
  67. -
  68. magic_open = libmagic.magic_open
  69. magic_open.restype = magic_t
  70. magic_open.argtypes = [c_int]
  71. @@ -335,8 +336,7 @@
  72. def magic_file(cookie, filename):
  73. - with LOCK:
  74. - return _magic_file(cookie, coerce_filename(filename))
  75. + return _magic_file(cookie, coerce_filename(filename))
  76. _magic_buffer = libmagic.magic_buffer
  77. @@ -346,8 +346,7 @@
  78. def magic_buffer(cookie, buf):
  79. - with LOCK:
  80. - return _magic_buffer(cookie, buf, len(buf))
  81. + return _magic_buffer(cookie, buf, len(buf))
  82. magic_descriptor = libmagic.magic_descriptor
  83. @@ -362,8 +361,7 @@
  84. def magic_descriptor(cookie, fd):
  85. - with LOCK:
  86. - return _magic_descriptor(cookie, fd)
  87. + return _magic_descriptor(cookie, fd)
  88. _magic_load = libmagic.magic_load
  89. @@ -373,8 +371,7 @@
  90. def magic_load(cookie, filename):
  91. - with LOCK:
  92. - return _magic_load(cookie, coerce_filename(filename))
  93. + return _magic_load(cookie, coerce_filename(filename))
  94. magic_setflags = libmagic.magic_setflags
  95. @@ -407,16 +404,14 @@
  96. if not _has_param:
  97. raise NotImplementedError("magic_setparam not implemented")
  98. v = c_size_t(val)
  99. - with LOCK:
  100. - return _magic_setparam(cookie, param, byref(v))
  101. + return _magic_setparam(cookie, param, byref(v))
  102. def magic_getparam(cookie, param):
  103. if not _has_param:
  104. raise NotImplementedError("magic_getparam not implemented")
  105. val = c_size_t()
  106. - with LOCK:
  107. - _magic_getparam(cookie, param, byref(val))
  108. + _magic_getparam(cookie, param, byref(val))
  109. return val.value
  110. @@ -431,8 +426,7 @@
  111. def version():
  112. if not _has_version:
  113. raise NotImplementedError("magic_version not implemented")
  114. - with LOCK:
  115. - return magic_version()
  116. + return magic_version()
  117. MAGIC_NONE = 0x000000 # No flags