Skip to content

Commit 727cff8

Browse files
author
Tony Crisci
committed
raise handler exceptions from Conection.main()
fixes #125
1 parent acb1eb5 commit 727cff8

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

i3ipc/connection.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,14 +470,19 @@ def _event_socket_poll(self):
470470
# we have not implemented this event
471471
return
472472

473-
self._pubsub.emit(event_name, event)
473+
try:
474+
self._pubsub.emit(event_name, event)
475+
except Exception as e:
476+
print(e)
477+
raise e
474478

475479
def main(self, timeout: float = 0.0):
476480
"""Starts the main loop for this connection to start handling events.
477481
478482
:param timeout: If given, quit the main loop after ``timeout`` seconds.
479483
:type timeout: float
480484
"""
485+
loop_exception = None
481486
self._quitting = False
482487
while True:
483488
try:
@@ -491,14 +496,16 @@ def main(self, timeout: float = 0.0):
491496

492497
while not self._event_socket_poll():
493498
pass
494-
499+
except Exception as e:
500+
loop_exception = e
501+
finally:
495502
if timer:
496503
timer.cancel()
497-
finally:
504+
498505
self._event_socket_teardown()
499506

500507
if self._quitting or not self._restarting or not self.auto_reconnect:
501-
return
508+
break
502509

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

517+
if loop_exception:
518+
raise loop_exception
519+
510520
def main_quit(self):
511521
"""Quits the running main loop for this connection."""
512522
self._quitting = True

test/aio/test_event_exceptions.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ def exception_throwing_handler(self, i3, e):
1616
async def test_event_exceptions(self, i3):
1717
i3.on('tick', self.exception_throwing_handler)
1818

19-
def send_tick():
20-
asyncio.ensure_future(self.send_tick())
21-
22-
i3._loop.call_later(0.1, send_tick)
19+
asyncio.ensure_future(i3.send_tick())
2320

2421
with pytest.raises(HandlerException):
2522
await i3.main()

test/aio/test_window.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77

88
class TestWindow(IpcTest):
9-
'''
109
@pytest.mark.asyncio
1110
async def test_window_event(self, i3):
1211
event = None
@@ -26,7 +25,6 @@ def on_window(i3, e):
2625
assert event
2726

2827
i3.off(on_window)
29-
'''
3028

3129
@pytest.mark.asyncio
3230
async def test_detailed_window_event(self, i3):

test/test_event_exceptions.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from ipctest import IpcTest
2+
3+
from threading import Timer
4+
import pytest
5+
6+
7+
class HandlerException(Exception):
8+
pass
9+
10+
11+
class TestEventExceptions(IpcTest):
12+
def exception_throwing_handler(self, i3, e):
13+
raise HandlerException()
14+
15+
def test_event_exceptions(self, i3):
16+
i3.on('tick', self.exception_throwing_handler)
17+
18+
Timer(0.001, i3.send_tick).start()
19+
20+
with pytest.raises(HandlerException):
21+
i3.main()

0 commit comments

Comments
 (0)