Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
fthiery opened this issue Jul 1, 2020 · 1 comment
Open

Comments

@fthiery
Copy link

fthiery commented Jul 1, 2020

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
Copy link
Author

fthiery commented Jul 2, 2020

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

fthiery added a commit to UbiCastTeam/pyinotify that referenced this issue Jul 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant