0.4.27-18-g7229954.fix-dont-raise-filenotfoundexception-on-symlinks.patch 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. Subject: Fix: Don't raise FileNotFoundException on symlinks
  2. Origin: upstream, commit 0.4.27-18-g7229954 <https://github.com/ahupp/python-magic/commit/0.4.27-18-g7229954>
  3. Author: Marten Ringwelski <git@maringuu.de>
  4. Date: Wed Aug 23 22:17:30 2023 +0200
  5. Forwarded: not-needed
  6. The builtin `open` will always follow symlinks.
  7. Using `os.stat` is the easiest solution imo.
  8. An alternative would be using `os.access` but that does not raise
  9. a FileNotFoundException so I chose `os.stat`.
  10. --- a/magic/__init__.py
  11. +++ b/magic/__init__.py
  12. @@ -17,6 +17,7 @@
  13. """
  14. import sys
  15. +import os
  16. import glob
  17. import ctypes
  18. import ctypes.util
  19. @@ -25,9 +26,6 @@
  20. from ctypes import c_char_p, c_int, c_size_t, c_void_p, byref, POINTER
  21. -# avoid shadowing the real open with the version from compat.py
  22. -_real_open = open
  23. -
  24. class MagicException(Exception):
  25. def __init__(self, message):
  26. @@ -109,8 +107,7 @@
  27. def from_file(self, filename):
  28. # raise FileNotFoundException or IOError if the file does not exist
  29. - with _real_open(filename):
  30. - pass
  31. + os.stat(filename, follow_symlinks=self.flags & MAGIC_SYMLINK)
  32. with self.lock:
  33. try: