Skip to content

Commit 1ee35f1

Browse files
committed
Fix a silent fail if path is a file #1034 (Windows only).
1 parent 6a4f1cf commit 1ee35f1

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/watchdog/observers/read_directory_changes.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,14 @@ def __init__(
4444
super().__init__(event_queue, watch, timeout=timeout, event_filter=event_filter)
4545
self._lock = threading.Lock()
4646
self._whandle: HANDLE | None = None
47+
self._watched_files: dict[str, str] = {}
4748

4849
def on_thread_start(self) -> None:
49-
self._whandle = get_directory_handle(self.watch.path)
50+
watch_path = self.watch.path
51+
if os.path.isfile(watch_path):
52+
watch_path, basename = os.path.split(watch_path)
53+
self._watched_files[self.watch.path] = basename
54+
self._whandle = get_directory_handle(watch_path)
5055

5156
if platform.python_implementation() == "PyPy":
5257

@@ -71,7 +76,13 @@ def queue_events(self, timeout: float) -> None:
7176
with self._lock:
7277
last_renamed_src_path = ""
7378
for winapi_event in winapi_events:
74-
src_path = os.path.join(self.watch.path, winapi_event.src_path)
79+
try:
80+
basename = self._watched_files[self.watch.path] # Is a file?
81+
if basename != winapi_event.src_path:
82+
continue
83+
src_path = self.watch.path
84+
except KeyError:
85+
src_path = os.path.join(self.watch.path, winapi_event.src_path)
7586

7687
if winapi_event.is_renamed_old:
7788
last_renamed_src_path = src_path

0 commit comments

Comments
 (0)