Skip to content

Commit

Permalink
Fix FSEventObserver (add follow_symlink kwarg)
Browse files Browse the repository at this point in the history
* Improve link test in inotify_c.py
  • Loading branch information
Corentin-pro committed Nov 12, 2024
1 parent 8531fce commit 028caa4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
12 changes: 9 additions & 3 deletions src/watchdog/observers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ class ObservedWatch:
Optional collection of :class:`watchdog.events.FileSystemEvent` to watch
"""

def __init__(self, path: str | Path, *, recursive: bool, event_filter: list[type[FileSystemEvent]] | None = None,
follow_symlink: bool = False):
def __init__(
self,
path: str | Path,
*,
recursive: bool,
event_filter: list[type[FileSystemEvent]] | None = None,
follow_symlink: bool = False,
):
self._path = str(path) if isinstance(path, Path) else path
self._is_recursive = recursive
self._follow_symlink = follow_symlink
Expand Down Expand Up @@ -281,7 +287,7 @@ def schedule(
*,
recursive: bool = False,
event_filter: list[type[FileSystemEvent]] | None = None,
follow_symlink: bool = False
follow_symlink: bool = False,
) -> ObservedWatch:
"""Schedules watching a path and calls appropriate methods specified
in the given event handler in response to file system events.
Expand Down
4 changes: 3 additions & 1 deletion src/watchdog/observers/fsevents.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,13 @@ def schedule(
path: str,
*,
recursive: bool = False,
follow_symlink: bool = False,
event_filter: list[type[FileSystemEvent]] | None = None,
) -> ObservedWatch:
# Fix for issue #26: Trace/BPT error when given a unicode path
# string. https://github.com/gorakhargosh/watchdog/issues#issue/26
if isinstance(path, str):
path = unicodedata.normalize("NFC", path)

return super().schedule(event_handler, path, recursive=recursive, event_filter=event_filter)
return super().schedule(event_handler, path, recursive=recursive, follow_symlink=follow_symlink,
event_filter=event_filter)
5 changes: 3 additions & 2 deletions src/watchdog/observers/inotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ def __init__(
def on_thread_start(self) -> None:
path = os.fsencode(self.watch.path)
event_mask = self.get_event_mask_from_filter()
self._inotify = InotifyBuffer(path, recursive=self.watch.is_recursive, event_mask=event_mask,
follow_symlink=self.watch.follow_symlink)
self._inotify = InotifyBuffer(
path, recursive=self.watch.is_recursive, event_mask=event_mask, follow_symlink=self.watch.follow_symlink
)

def on_thread_stop(self) -> None:
if self._inotify:
Expand Down
5 changes: 3 additions & 2 deletions src/watchdog/observers/inotify_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class InotifyBuffer(BaseThread):

delay = 0.5

def __init__(self, path: bytes, *, recursive: bool = False, event_mask: int | None = None,
follow_symlink: bool = False) -> None:
def __init__(
self, path: bytes, *, recursive: bool = False, event_mask: int | None = None, follow_symlink: bool = False
) -> None:
super().__init__()
# XXX: Remove quotes after Python 3.9 drop
self._queue = DelayedQueue["InotifyEvent | tuple[InotifyEvent, InotifyEvent]"](self.delay)
Expand Down
7 changes: 4 additions & 3 deletions src/watchdog/observers/inotify_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ class Inotify:
``True`` if subdirectories should be monitored; ``False`` otherwise.
"""

def __init__(self, path: bytes, *, recursive: bool = False, event_mask: int | None = None,
follow_symlink: bool = False) -> None:
def __init__(
self, path: bytes, *, recursive: bool = False, event_mask: int | None = None, follow_symlink: bool = False
) -> None:
# The file descriptor associated with the inotify instance.
inotify_fd = inotify_init()
if inotify_fd == -1:
Expand Down Expand Up @@ -410,7 +411,7 @@ def _add_dir_watch(self, path: bytes, mask: int, *, recursive: bool) -> None:
for root, dirnames, _ in os.walk(path):
for dirname in dirnames:
full_path = os.path.join(root, dirname)
if os.path.islink(full_path) and not self._follow_symlink:
if not self._follow_symlink and os.path.islink(full_path):
continue
self._add_watch(full_path, mask)

Expand Down

0 comments on commit 028caa4

Please sign in to comment.