Skip to content

Commit

Permalink
Fix exception __cause__ propagation with tblib.pickling_support.insta…
Browse files Browse the repository at this point in the history
…ll() (#255)

Co-authored-by: Olivier Grisel <[email protected]>
Co-authored-by: Thomas Moreau <[email protected]>
Co-authored-by: Loïc Estève <[email protected]>
  • Loading branch information
3 people authored Jan 24, 2023
1 parent 1f21215 commit 81b6b9a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### 3.4.0 - in development

- TODO
- Fix exception `__cause__` not being propagated with
`tblib.pickling_support.install()` (#255).

### 3.3.0 - 2022-09-15

Expand Down
4 changes: 3 additions & 1 deletion loky/process_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ def __str__(self):
return self.tb


class _ExceptionWithTraceback(BaseException):
# Do not inherit from BaseException to mirror
# concurrent.futures.process._ExceptionWithTraceback
class _ExceptionWithTraceback:

def __init__(self, exc):
tb = getattr(exc, "__traceback__", None)
Expand Down
21 changes: 21 additions & 0 deletions tests/_test_process_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,26 @@ def check_viztracer_active_and_custom_init():
finally:
tracer.stop()

def test_exception_cause_with_tblib(self):
"Ensure tampering with exception pickling do not break __cause__ propagation"
tblib_pickling_support = pytest.importorskip('tblib.pickling_support')

error_message = "This is the error message"

def raise_value_error():
tblib_pickling_support.install()
raise ValueError(error_message)

executor = self.executor_type(max_workers=2)
f = executor.submit(raise_value_error)
try:
f.result()
except ValueError as e:
assert e.__cause__ is not None
assert error_message in str(e.__cause__)

executor.shutdown(wait=True)


def _custom_initializer():
"""_custom_initializer is module function to be picklable
Expand All @@ -1093,3 +1113,4 @@ def _custom_initializer():
use cloudpickle to pickle the initializer.
"""
loky._custom_global_var = 42

1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ deps =
coverage
viztracer ; python_version >= '3.8' and python_version <= '3.10'
numpy ; implementation_name == 'cpython'
tblib
whitelist_externals=
bash
setenv =
Expand Down

0 comments on commit 81b6b9a

Please sign in to comment.