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