Skip to content

Commit 7229954

Browse files
maringuuahupp
authored andcommitted
fix: Don't raise FileNotFoundException on symlinks
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`.
1 parent 0cc3cf8 commit 7229954

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

magic/__init__.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"""
1818

1919
import sys
20+
import os
2021
import glob
2122
import ctypes
2223
import ctypes.util
@@ -25,9 +26,6 @@
2526

2627
from ctypes import c_char_p, c_int, c_size_t, c_void_p, byref, POINTER
2728

28-
# avoid shadowing the real open with the version from compat.py
29-
_real_open = open
30-
3129

3230
class MagicException(Exception):
3331
def __init__(self, message):
@@ -109,8 +107,7 @@ def from_buffer(self, buf):
109107

110108
def from_file(self, filename):
111109
# raise FileNotFoundException or IOError if the file does not exist
112-
with _real_open(filename):
113-
pass
110+
os.stat(filename, follow_symlinks=self.flags & MAGIC_SYMLINK)
114111

115112
with self.lock:
116113
try:

0 commit comments

Comments
 (0)