Skip to content

Commit d9375c6

Browse files
committed
Cache "is file" result as suggested.
1 parent e031c1c commit d9375c6

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/watchdog/observers/read_directory_changes.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ 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 = {}
4748

4849
def on_thread_start(self) -> None:
4950
watch_path = self.watch.path
5051
if os.path.isfile(watch_path):
51-
watch_path = os.path.dirname(watch_path)
52+
watch_path, basename = os.path.split(watch_path)
53+
self._watched_files[self.watch.path] = basename
5254
self._whandle = get_directory_handle(watch_path)
5355

5456
if platform.python_implementation() == "PyPy":
@@ -74,11 +76,12 @@ def queue_events(self, timeout: float) -> None:
7476
with self._lock:
7577
last_renamed_src_path = ""
7678
for winapi_event in winapi_events:
77-
if os.path.isfile(self.watch.path):
78-
if os.path.basename(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:
7982
continue
8083
src_path = self.watch.path
81-
else:
84+
except KeyError:
8285
src_path = os.path.join(self.watch.path, winapi_event.src_path)
8386

8487
if winapi_event.is_renamed_old:

0 commit comments

Comments
 (0)