Skip to content

Commit

Permalink
Cache "is file" result as suggested.
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasRzepka committed Oct 4, 2024
1 parent e031c1c commit d9375c6
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/watchdog/observers/read_directory_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ def __init__(
super().__init__(event_queue, watch, timeout=timeout, event_filter=event_filter)
self._lock = threading.Lock()
self._whandle: HANDLE | None = None
self._watched_files = {}

def on_thread_start(self) -> None:
watch_path = self.watch.path
if os.path.isfile(watch_path):
watch_path = os.path.dirname(watch_path)
watch_path, basename = os.path.split(watch_path)
self._watched_files[self.watch.path] = basename
self._whandle = get_directory_handle(watch_path)

if platform.python_implementation() == "PyPy":
Expand All @@ -74,11 +76,12 @@ def queue_events(self, timeout: float) -> None:
with self._lock:
last_renamed_src_path = ""
for winapi_event in winapi_events:
if os.path.isfile(self.watch.path):
if os.path.basename(self.watch.path) != winapi_event.src_path:
try:
basename = self._watched_files[self.watch.path] # Is a file?
if basename != winapi_event.src_path:
continue
src_path = self.watch.path
else:
except KeyError:
src_path = os.path.join(self.watch.path, winapi_event.src_path)

if winapi_event.is_renamed_old:
Expand Down

0 comments on commit d9375c6

Please sign in to comment.