Skip to content

Commit

Permalink
Fix marshal_exception not to use global state (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdickinson committed Jul 12, 2021
1 parent a311235 commit 28cb7e9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
26 changes: 21 additions & 5 deletions traits_futures/exception_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,28 @@
import traceback


def marshal_exception(e):
def marshal_exception(exception):
"""
Turn exception details into something that can be safely
transmitted across thread / process boundaries.
Parameters
----------
exception : BaseException
The exception instance to be marshalled
Returns
-------
exception_type, exception_value, exception_traceback : str
Strings representing the exception type, value and
formatted traceback.
"""
exc_type = str(type(e))
exc_value = str(e)
formatted_traceback = str(traceback.format_exc())
return exc_type, exc_value, formatted_traceback
return (
str(type(exception)),
str(exception),
"".join(
traceback.format_exception(
type(exception), exception, exception.__traceback__
)
),
)
18 changes: 18 additions & 0 deletions traits_futures/tests/test_exception_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,21 @@ def test_marshal_exception_with_unicode_message(self):
self.assertEqual(exc_type, str(ValueError))
self.assertIn(message, exc_value)
self.assertIn("test_marshal_exception", exc_traceback)

def test_marshal_exception_works_outside_except(self):
try:
raise RuntimeError("something went wrong")
except BaseException as exception:
stored_exception = exception

exc_type, exc_value, exc_traceback = marshal_exception(
stored_exception
)

self.assertIsInstance(exc_type, str)
self.assertIsInstance(exc_value, str)
self.assertIsInstance(exc_traceback, str)

self.assertEqual(exc_type, str(RuntimeError))
self.assertIn("something went wrong", exc_value)
self.assertIn("test_marshal_exception", exc_traceback)

0 comments on commit 28cb7e9

Please sign in to comment.