[Pkg-bitcoin-commits] [python-quamash] 91/269: Pass on exceptions caught in thread worker

Jonas Smedegaard dr at jones.dk
Fri Nov 24 11:26:19 UTC 2017


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository python-quamash.

commit 5a3d251c191e3b7eeadb22fd9367406561c224f4
Author: Arve Knudsen <arve.knudsen at gmail.com>
Date:   Tue Jul 15 14:57:52 2014 +0200

    Pass on exceptions caught in thread worker
---
 quamash/__init__.py      | 11 ++++++++---
 tests/test_qeventloop.py | 17 ++++++++++++-----
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/quamash/__init__.py b/quamash/__init__.py
index 854028c..3c2ec50 100644
--- a/quamash/__init__.py
+++ b/quamash/__init__.py
@@ -56,9 +56,14 @@ class _QThreadWorker(QtCore.QThread):
 			)
 			if future.set_running_or_notify_cancel():
 				self._logger.debug('Invoking callback')
-				r = callback(*args, **kwargs)
-				self._logger.debug('Setting Future result: {}'.format(r))
-				future.set_result(r)
+				try:
+					r = callback(*args, **kwargs)
+				except Exception as err:
+					self._logger.debug('Setting Future exception: {}'.format(err))
+					future.set_exception(err)
+				else:
+					self._logger.debug('Setting Future result: {}'.format(r))
+					future.set_result(r)
 			else:
 				self._logger.debug('Future was cancelled')
 
diff --git a/tests/test_qeventloop.py b/tests/test_qeventloop.py
index 61b1edf..f176913 100644
--- a/tests/test_qeventloop.py
+++ b/tests/test_qeventloop.py
@@ -73,16 +73,23 @@ def test_can_run_tasks_in_executor(loop, executor):
 		nonlocal was_invoked
 		was_invoked = True
 
-	@asyncio.coroutine
-	def blocking_task():
-		yield from loop.run_in_executor(None, blocking_func)
-
 	was_invoked = False
-	loop.run_until_complete(blocking_task())
+	loop.run_until_complete(loop.run_in_executor(None, blocking_func))
 
 	assert was_invoked
 
 
+def test_can_handle_exception_in_default_executor(loop):
+	"""Verify that exceptions from tasks run in default (threaded) executor are handled."""
+	def blocking_func():
+		raise Exception('Testing')
+
+	with pytest.raises(Exception) as excinfo:
+		loop.run_until_complete(loop.run_in_executor(None, blocking_func))
+
+	assert str(excinfo.value) == 'Testing'
+
+
 def test_can_execute_subprocess(loop):
 	"""Verify that a subprocess can be executed."""
 	transport, protocol = loop.run_until_complete(loop.subprocess_exec(

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-bitcoin/python-quamash.git



More information about the Pkg-bitcoin-commits mailing list