diff --git a/asyncio_glib/glib_selector.py b/asyncio_glib/glib_selector.py index 430d076..49283c1 100644 --- a/asyncio_glib/glib_selector.py +++ b/asyncio_glib/glib_selector.py @@ -8,20 +8,16 @@ ) -# The override for GLib.MainLoop installs a signal wakeup fd, -# which interferes with asyncio signal handlers. Try to get the -# direct version. - -MainLoop = GLib.MainLoop -RawMainLoop = MainLoop.__bases__[0] -RawMainLoop = RawMainLoop if hasattr(RawMainLoop, 'run') else MainLoop - - -class BasicMainLoop(RawMainLoop): - - # Backwards compatible constructor API - def __new__(cls, context=None): - return GLib.MainLoop.new(context, False) +def _get_main_loop_runner(loop, handle_sigint): + # The override for GLib.MainLoop installs a signal wakeup fd, + # which interferes with asyncio signal handlers. Try to get the + # direct version. + if handle_sigint: + return loop.run + try: + return super(GLib.MainLoop, loop).run + except AttributeError: + return loop.run class _SelectorSource(GLib.Source): @@ -43,7 +39,10 @@ def dispatch(self, callback, args): for (fd, tag) in self._fd_to_tag.items(): condition = self.query_unix_fd(tag) events = self._fd_to_events.setdefault(fd, 0) - if condition & GLib.IOCondition.IN: + if ( + condition & GLib.IOCondition.IN + or condition & GLib.IOCondition.HUP + ): events |= selectors.EVENT_READ if condition & GLib.IOCondition.OUT: events |= selectors.EVENT_WRITE @@ -77,8 +76,10 @@ class GLibSelector(selectors._BaseSelectorImpl): def __init__(self, context, handle_sigint=False): super().__init__() self._context = context - self._main_loop = MainLoop(self._context) \ - if handle_sigint else BasicMainLoop(self._context) + self._main_loop = GLib.MainLoop.new(self._context, False) + self._main_loop_runner = _get_main_loop_runner( + self._main_loop, handle_sigint + ) self._source = _SelectorSource(self._main_loop) self._source.attach(self._context) @@ -108,7 +109,7 @@ def select(self, timeout=None): self._source.clear() if may_block: - self._main_loop.run() + self._main_loop_runner() else: self._context.iteration(False)