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

Fix missing errno for library calls #60

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions inotify/calls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import ctypes
import os

import inotify.library

Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion inotify/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
if _FILEPATH is None:
_FILEPATH = 'libc.so.6'

instance = ctypes.cdll.LoadLibrary(_FILEPATH)
instance = ctypes.CDLL(_FILEPATH, use_errno=True)
12 changes: 12 additions & 0 deletions tests/test_inotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import os
import unittest
import errno

import inotify.constants
import inotify.adapters
import inotify.calls
import inotify.test_support

try:
Expand Down Expand Up @@ -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):
Expand Down