1
0

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

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