Skip to content

Commit

Permalink
raise handler exceptions from Conection.main()
Browse files Browse the repository at this point in the history
fixes #125
  • Loading branch information
Tony Crisci committed Sep 2, 2019
1 parent acb1eb5 commit 727cff8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
18 changes: 14 additions & 4 deletions i3ipc/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,14 +470,19 @@ def _event_socket_poll(self):
# we have not implemented this event
return

self._pubsub.emit(event_name, event)
try:
self._pubsub.emit(event_name, event)
except Exception as e:
print(e)
raise e

def main(self, timeout: float = 0.0):
"""Starts the main loop for this connection to start handling events.
:param timeout: If given, quit the main loop after ``timeout`` seconds.
:type timeout: float
"""
loop_exception = None
self._quitting = False
while True:
try:
Expand All @@ -491,14 +496,16 @@ def main(self, timeout: float = 0.0):

while not self._event_socket_poll():
pass

except Exception as e:
loop_exception = e
finally:
if timer:
timer.cancel()
finally:

self._event_socket_teardown()

if self._quitting or not self._restarting or not self.auto_reconnect:
return
break

self._restarting = False
# The ipc told us it's restarting and the user wants to survive
Expand All @@ -507,6 +514,9 @@ def main(self, timeout: float = 0.0):
if not self._wait_for_socket():
break

if loop_exception:
raise loop_exception

def main_quit(self):
"""Quits the running main loop for this connection."""
self._quitting = True
Expand Down
5 changes: 1 addition & 4 deletions test/aio/test_event_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ def exception_throwing_handler(self, i3, e):
async def test_event_exceptions(self, i3):
i3.on('tick', self.exception_throwing_handler)

def send_tick():
asyncio.ensure_future(self.send_tick())

i3._loop.call_later(0.1, send_tick)
asyncio.ensure_future(i3.send_tick())

with pytest.raises(HandlerException):
await i3.main()
2 changes: 0 additions & 2 deletions test/aio/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class TestWindow(IpcTest):
'''
@pytest.mark.asyncio
async def test_window_event(self, i3):
event = None
Expand All @@ -26,7 +25,6 @@ def on_window(i3, e):
assert event

i3.off(on_window)
'''

@pytest.mark.asyncio
async def test_detailed_window_event(self, i3):
Expand Down
21 changes: 21 additions & 0 deletions test/test_event_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from ipctest import IpcTest

from threading import Timer
import pytest


class HandlerException(Exception):
pass


class TestEventExceptions(IpcTest):
def exception_throwing_handler(self, i3, e):
raise HandlerException()

def test_event_exceptions(self, i3):
i3.on('tick', self.exception_throwing_handler)

Timer(0.001, i3.send_tick).start()

with pytest.raises(HandlerException):
i3.main()

0 comments on commit 727cff8

Please sign in to comment.