Skip to content

Commit

Permalink
Install excepthook for top-level errfile
Browse files Browse the repository at this point in the history
  • Loading branch information
danijar committed Sep 9, 2024
1 parent 8c2b5a1 commit 459b63d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
python -m pip install pytest
- name: Run tests
run: |
python -m pytest tests
python -m pytest tests -vv -s
- name: Package installation
run: |
python -m pip install portal
2 changes: 1 addition & 1 deletion portal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '3.1.11'
__version__ = '3.1.12'

import multiprocessing as mp
try:
Expand Down
11 changes: 11 additions & 0 deletions portal/contextlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def setup(
assert hasattr(errfile, 'exists') and hasattr(errfile, 'write_text')
self.errfile = errfile
self._check_errfile()
self._install_excepthook()

if interval:
assert isinstance(interval, (int, float))
Expand Down Expand Up @@ -148,6 +149,16 @@ def _watcher(self):
while not self.done.wait(self.interval):
self._check_errfile()

def _install_excepthook(self):
existing = sys.excepthook
def patched(typ, val, tb):
if self.errfile:
message = ''.join(traceback.format_exception(typ, val, tb)).strip('\n')
self.errfile.write_text(message)
print(f'Wrote errorfile: {self.errfile}', file=sys.stderr)
return existing(typ, val, tb)
sys.excepthook = patched

def _check_errfile(self):
if self.errfile and self.errfile.exists():
message = f'Shutting down due to error file: {self.errfile}'
Expand Down

0 comments on commit 459b63d

Please sign in to comment.