seb-m / pyinotify

Monitoring filesystems events with inotify on Linux.
http://github.com/seb-m/pyinotify/wiki
MIT License
2.29k stars 379 forks source link

Does nothing when passing a pathlib.PosixPath object to add_watch #187

Open fthiery opened 4 years ago

fthiery commented 4 years ago

Hi there,

Took me a while to figure out, because this is completely silent. I discovered that pyinotify will just not watch at all if the folder argument is a pathlib.Path object instead of a string. Therefore, you must str(path) for this to work.

Tested on Arch, v0.9.6, python 3.8.3 as well as debian 10 / python 3.7.3

Here is a testcase.

#!/usr/bin/env python3
import unittest
from pathlib import Path
import pyinotify
import os
import shutil

folder = Path("testfolder")
fname = folder / "testfile"

class Test(unittest.TestCase):

    def setUp(self):
        folder.mkdir()

    def tearDown(self):
        shutil.rmtree(folder)

    def on_event(self):
        pass

    def test_works(self):
        wm = pyinotify.WatchManager()
        notifier = pyinotify.Notifier(wm, default_proc_fun=self.on_event)
        wm.add_watch(str(folder), pyinotify.IN_CREATE | pyinotify.IN_CLOSE_WRITE | pyinotify.IN_MOVED_TO, rec=True, auto_add=True)
        os.system(f'sleep 0.2 && dd if=/dev/zero of={fname} count=1')
        self.assertTrue(notifier.check_events(1000))

    def test_fails(self):
        wm = pyinotify.WatchManager()
        notifier = pyinotify.Notifier(wm, default_proc_fun=self.on_event)
        wm.add_watch(folder, pyinotify.IN_CREATE | pyinotify.IN_CLOSE_WRITE | pyinotify.IN_MOVED_TO, rec=True, auto_add=True)
        os.system(f'sleep 0.2 && dd if=/dev/zero of={fname} count=1')
        self.assertTrue(notifier.check_events(1000))

if __name__ == '__main__':
    unittest.main()
fthiery commented 4 years ago

This seems to come down to this part:

https://github.com/seb-m/pyinotify/blob/master/python3/pyinotify.py#L1907

I'm not sure whether we should cast to string or emit an error