From 84051284ef4d2b18bf899a50c633f2b51a3288b9 Mon Sep 17 00:00:00 2001 From: Elias481 <38512369+Elias481@users.noreply.github.com> Date: Wed, 19 Sep 2018 16:55:23 +0200 Subject: [PATCH 1/3] Utilize different library load function to enable fetching of errno --- inotify/library.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inotify/library.py b/inotify/library.py index bb718d3..260e720 100644 --- a/inotify/library.py +++ b/inotify/library.py @@ -5,4 +5,4 @@ if _FILEPATH is None: _FILEPATH = 'libc.so.6' -instance = ctypes.cdll.LoadLibrary(_FILEPATH) +instance = ctypes.CDLL(_FILEPATH, use_errno=True) From 448a58c7f443bef8d9f806a4d27a6a795a6f2b3b Mon Sep 17 00:00:00 2001 From: Elias481 <38512369+Elias481@users.noreply.github.com> Date: Wed, 19 Sep 2018 16:57:59 +0200 Subject: [PATCH 2/3] Add message belonging to (now working) errno to Exception --- inotify/calls.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/inotify/calls.py b/inotify/calls.py index 99de66a..9a1a669 100644 --- a/inotify/calls.py +++ b/inotify/calls.py @@ -1,5 +1,6 @@ import logging import ctypes +import os import inotify.library @@ -10,8 +11,11 @@ class InotifyError(Exception): def __init__(self, message, *args, **kwargs): - self.errno = ctypes.get_errno() - message += " ERRNO=(%d)" % (self.errno,) + errnum = ctypes.get_errno() + self.errno = errnum + try: errmsg = os.strerror(errnum) + except ValueError as ex: errmsg = '' + message += " ERRNO=%d %s" % (errnum,errmsg) super(InotifyError, self).__init__(message, *args, **kwargs) From 3fc523146c305947e4be45e3a1a8d9194502c29a Mon Sep 17 00:00:00 2001 From: Elias Ohm Date: Wed, 19 Sep 2018 19:34:58 +0200 Subject: [PATCH 3/3] add test-case for errno returned in InotifyError, make coverage report happier (it still does not like all the dead code..) --- tests/test_inotify.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_inotify.py b/tests/test_inotify.py index 0f87524..e5a7fd1 100644 --- a/tests/test_inotify.py +++ b/tests/test_inotify.py @@ -2,9 +2,11 @@ import os import unittest +import errno import inotify.constants import inotify.adapters +import inotify.calls import inotify.test_support try: @@ -145,6 +147,16 @@ def test__get_event_names(self): self.assertEquals(names, all_names) + def test__exception_errno(self): + with inotify.test_support.temp_path() as path: + i = inotify.adapters.Inotify() + errnum = None + path1 = os.path.join(path, 'abc') + try: + i.add_watch(path1) + except inotify.calls.InotifyError as ex: + errnum = ex.errno + self.assertEquals(errnum, errno.ENOENT) class TestInotifyTree(unittest.TestCase): def __init__(self, *args, **kwargs):