Skip to content

Commit

Permalink
lazy load libc to defer errors finding it until after import
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjbillington committed Jul 27, 2018
1 parent d3c7e02 commit f7e99db
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 8 additions & 2 deletions inotify_simple/inotify_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ class LongEnum(long, enum.Enum): pass

__all__ = ['flags', 'masks', 'parse_events', 'INotify', 'Event']

_libc = ctypes.cdll.LoadLibrary('libc.so.6')
_libc.__errno_location.restype = ctypes.POINTER(ctypes.c_int)
_libc = None

def _ensure_libc_loaded():
global _libc
if _libc is None:
_libc = ctypes.cdll.LoadLibrary('libc.so.6')
_libc.__errno_location.restype = ctypes.POINTER(ctypes.c_int)

def _libc_call(function, *args):
"""Wrapper which raises errors and retries on EINTR."""
Expand All @@ -78,6 +83,7 @@ def __init__(self):
#: The inotify file descriptor returned by ``inotify_init()``. You are
#: free to use it directly with ``os.read`` if you'd prefer not to call
#: :func:`~inotify_simple.INotify.read` for some reason.
_ensure_libc_loaded()
self.fd = _libc_call(_libc.inotify_init)
self._poller = select.poll()
self._poller.register(self.fd)
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
import os
from distutils.core import setup

__version__ = '1.1.7'
__version__ = '1.1.8'

DESCRIPTION = ("A simple wrapper around inotify. No fancy bells and whistles, " +
"just a literal wrapper with ctypes. Only 122 lines of code!")
"just a literal wrapper with ctypes. Only 127 lines of code!")

# Auto generate a __version__ package for the package to import
with open(os.path.join('inotify_simple', '__version__.py'), 'w') as f:
Expand Down

0 comments on commit f7e99db

Please sign in to comment.