| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- Subject: Revert "Move lock to global scope"
- Origin: upstream, commit 0.4.27-54-g4043553 <https://github.com/ahupp/python-magic/commit/0.4.27-54-g4043553>
- Author: ddelange <14880945+ddelange@users.noreply.github.com>
- Date: Fri Oct 17 20:04:26 2025 +0300
- Bug-Debian: https://bugs.debian.org/4043553
- This reverts commit f2ac98d8aa7464165984068de9e484d0321cd4f3.
- --- a/magic/__init__.py
- +++ b/magic/__init__.py
- @@ -105,6 +105,7 @@
- self.flags |= MAGIC_NO_CHECK_SIMH
-
- self.cookie = magic_open(self.flags)
- + self.lock = threading.Lock()
-
- magic_load(self.cookie, magic_file)
-
- @@ -133,31 +134,34 @@
- """
- Identify the contents of `buf`
- """
- - try:
- - # if we're on python3, convert buf to bytes
- - # otherwise this string is passed as wchar*
- - # which is not what libmagic expects
- - # NEXTBREAK: only take bytes
- - if type(buf) == str and str != bytes:
- - buf = buf.encode("utf-8", errors="replace")
- - return maybe_decode(magic_buffer(self.cookie, buf))
- - except MagicException as e:
- - return self._handle509Bug(e)
- + with self.lock:
- + try:
- + # if we're on python3, convert buf to bytes
- + # otherwise this string is passed as wchar*
- + # which is not what libmagic expects
- + # NEXTBREAK: only take bytes
- + if type(buf) == str and str != bytes:
- + buf = buf.encode("utf-8", errors="replace")
- + return maybe_decode(magic_buffer(self.cookie, buf))
- + except MagicException as e:
- + return self._handle509Bug(e)
-
- def from_file(self, filename):
- # raise FileNotFoundException or IOError if the file does not exist
- os.stat(filename, follow_symlinks=self.flags & MAGIC_SYMLINK)
-
- - try:
- - return maybe_decode(magic_file(self.cookie, filename))
- - except MagicException as e:
- - return self._handle509Bug(e)
- + with self.lock:
- + try:
- + return maybe_decode(magic_file(self.cookie, filename))
- + except MagicException as e:
- + return self._handle509Bug(e)
-
- def from_descriptor(self, fd):
- - try:
- - return maybe_decode(magic_descriptor(self.cookie, fd))
- - except MagicException as e:
- - return self._handle509Bug(e)
- + with self.lock:
- + try:
- + return maybe_decode(magic_descriptor(self.cookie, fd))
- + except MagicException as e:
- + return self._handle509Bug(e)
-
- def _handle509Bug(self, e):
- # libmagic 5.09 has a bug where it might fail to identify the
- @@ -309,9 +313,6 @@
- return filename
-
-
- -# libmagic is not thread-safe: guard for concurrent calls on a global scope
- -LOCK = threading.Lock()
- -
- magic_open = libmagic.magic_open
- magic_open.restype = magic_t
- magic_open.argtypes = [c_int]
- @@ -335,8 +336,7 @@
-
-
- def magic_file(cookie, filename):
- - with LOCK:
- - return _magic_file(cookie, coerce_filename(filename))
- + return _magic_file(cookie, coerce_filename(filename))
-
-
- _magic_buffer = libmagic.magic_buffer
- @@ -346,8 +346,7 @@
-
-
- def magic_buffer(cookie, buf):
- - with LOCK:
- - return _magic_buffer(cookie, buf, len(buf))
- + return _magic_buffer(cookie, buf, len(buf))
-
-
- magic_descriptor = libmagic.magic_descriptor
- @@ -362,8 +361,7 @@
-
-
- def magic_descriptor(cookie, fd):
- - with LOCK:
- - return _magic_descriptor(cookie, fd)
- + return _magic_descriptor(cookie, fd)
-
-
- _magic_load = libmagic.magic_load
- @@ -373,8 +371,7 @@
-
-
- def magic_load(cookie, filename):
- - with LOCK:
- - return _magic_load(cookie, coerce_filename(filename))
- + return _magic_load(cookie, coerce_filename(filename))
-
-
- magic_setflags = libmagic.magic_setflags
- @@ -407,16 +404,14 @@
- if not _has_param:
- raise NotImplementedError("magic_setparam not implemented")
- v = c_size_t(val)
- - with LOCK:
- - return _magic_setparam(cookie, param, byref(v))
- + return _magic_setparam(cookie, param, byref(v))
-
-
- def magic_getparam(cookie, param):
- if not _has_param:
- raise NotImplementedError("magic_getparam not implemented")
- val = c_size_t()
- - with LOCK:
- - _magic_getparam(cookie, param, byref(val))
- + _magic_getparam(cookie, param, byref(val))
- return val.value
-
-
- @@ -431,8 +426,7 @@
- def version():
- if not _has_version:
- raise NotImplementedError("magic_version not implemented")
- - with LOCK:
- - return magic_version()
- + return magic_version()
-
-
- MAGIC_NONE = 0x000000 # No flags
|