Skip to content

Commit

Permalink
fix: polish #1070
Browse files Browse the repository at this point in the history
  • Loading branch information
BoboTiG committed Sep 27, 2024
1 parent 29393f4 commit 59650f8
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/watchdog/observers/inotify_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def close(self) -> None:
if self._waiting_to_read:
# inotify_rm_watch() should write data to _inotify_fd and wake
# the thread, but writing to the kill channel will gaurentee this
os.write(self._kill_w, b'!')
os.write(self._kill_w, b"!")
else:
self._close_resources()

Expand Down Expand Up @@ -365,7 +365,7 @@ def _recursive_simulate(src_path: bytes) -> list[InotifyEvent]:

return event_list

def _close_resources(self):
def _close_resources(self) -> None:
os.close(self._inotify_fd)
os.close(self._kill_r)
os.close(self._kill_w)
Expand Down
5 changes: 4 additions & 1 deletion src/watchdog/utils/bricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ def _init(self, maxsize: int) -> None:
super()._init(maxsize)
self._last_item = None

def put(self, item: Any, block: bool = True, timeout: float | None = None) -> None:
def put(self, item: Any, block: bool = True, timeout: float | None = None) -> None: # noqa: FBT001,FBT002
"""This method will be used by `eventlet`, when enabled, so we cannot use force proper keyword-only
arguments nor touch the signature. Also, the `timeout` argument will be ignored in that case.
"""
if self._last_item is None or item != self._last_item:
super().put(item, block, timeout)

Expand Down
Empty file added tests/isolated/__init__.py
Empty file.
7 changes: 4 additions & 3 deletions tests/isolated/eventlet_observer_stops.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if __name__ == '__main__':
if __name__ == "__main__":
import eventlet

eventlet.monkey_patch()
Expand All @@ -7,10 +7,11 @@
import sys
import tempfile

from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
from watchdog.observers import Observer

with tempfile.TemporaryDirectory() as temp_dir:

def run_observer():
event_handler = LoggingEventHandler()
observer = Observer()
Expand All @@ -20,7 +21,7 @@ def run_observer():
observer.stop()

def on_alarm(signum, frame):
print("Observer.stop() never finished!", file=sys.stderr)
print("Observer.stop() never finished!", file=sys.stderr) # noqa: T201
sys.exit(1)

signal.signal(signal.SIGALRM, on_alarm)
Expand Down
20 changes: 10 additions & 10 deletions tests/isolated/eventlet_skip_repeat_queue.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
if __name__ == '__main__':
if __name__ == "__main__":
import eventlet

eventlet.monkey_patch()

from watchdog.utils.bricks import SkipRepeatsQueue

q = SkipRepeatsQueue(10)
q.put('A')
q.put('A')
q.put('A')
q.put('A')
q.put('B')
q.put('A')
q.put("A")
q.put("A")
q.put("A")
q.put("A")
q.put("B")
q.put("A")

value = q.get()
assert value == 'A'
assert value == "A"
q.task_done()

assert q.unfinished_tasks == 2

value = q.get()
assert value == 'B'
assert value == "B"
q.task_done()

assert q.unfinished_tasks == 1

value = q.get()
assert value == 'A'
assert value == "A"
q.task_done()

assert q.empty()
Expand Down
11 changes: 6 additions & 5 deletions tests/test_isolated.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import importlib

import pytest

from watchdog.utils import platform

from .utils import run_isolated_test
Expand All @@ -10,15 +11,15 @@
# Current usage ReadDirectoryChangesW on Windows is blocking, though async may be possible
@pytest.mark.skipif(not platform.is_linux(), reason="Eventlet only supported in Linux")
def test_observer_stops_in_eventlet():
if not importlib.util.find_spec('eventlet'):
if not importlib.util.find_spec("eventlet"):
pytest.skip("eventlet not installed")

run_isolated_test('eventlet_observer_stops.py')
run_isolated_test("eventlet_observer_stops.py")


@pytest.mark.skipif(not platform.is_linux(), reason="Eventlet only supported in Linux")
def test_eventlet_skip_repeat_queue():
if not importlib.util.find_spec('eventlet'):
if not importlib.util.find_spec("eventlet"):
pytest.skip("eventlet not installed")

run_isolated_test('eventlet_skip_repeat_queue.py')
run_isolated_test("eventlet_skip_repeat_queue.py")
10 changes: 5 additions & 5 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ def close(self) -> None:


def run_isolated_test(path):
ISOALTED_TEST_PREFIX = os.path.join('tests', 'isolated')
path = os.path.abspath(os.path.join(ISOALTED_TEST_PREFIX, path))
isolated_test_prefix = os.path.join("tests", "isolated")
path = os.path.abspath(os.path.join(isolated_test_prefix, path))

src_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'src')
src_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "src")
new_env = os.environ.copy()
new_env['PYTHONPATH'] = os.pathsep.join(sys.path + [src_dir])
new_env["PYTHONPATH"] = os.pathsep.join([*sys.path, src_dir])

new_argv = [sys.executable, path]

Expand All @@ -122,6 +122,6 @@ def run_isolated_test(path):
p.communicate(timeout=timeout)
except subprocess.TimeoutExpired:
p.kill()
assert False, 'timed out'
raise

assert p.returncode == 0
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extras =
watchmedo
commands =
python -m ruff format docs/source/examples src tests
python -m ruff check --fix src docs/source/examples tests
python -m ruff check --fix --unsafe-fixes src docs/source/examples tests

[testenv:types]
usedevelop = true
Expand Down

0 comments on commit 59650f8

Please sign in to comment.