Coverage for gwcelery/util/cmdline.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-23 18:01 +0000

1"""Utilities for calling command-line tools as Python functions.""" 

2import contextlib 

3 

4__all__ = ('handling_system_exit',) 

5 

6 

7@contextlib.contextmanager 

8def handling_system_exit(): 

9 """Catch any :obj:`SystemExit` and re-raise it as :obj:`RuntimeError`. 

10 

11 Some Celery tasks in this package call main functions of command-line tools 

12 from other packages. Those main functions may try to exit the Python 

13 interpreter (if, for example, the command-line arguments are not 

14 understood). 

15 

16 Catch any :obj:`SystemExit` exception. If the exit code is zero (signifying 

17 a normal exit status), then ignore the exception. If the exit code is 

18 nonzero (signifying an error exit status), then re-raise it as a 

19 :obj:`RuntimeError` so that the error is reported but the Celery worker is 

20 not killed. 

21 """ 

22 try: 

23 yield 

24 except SystemExit as e: 

25 if e.code != 0: 

26 raise RuntimeError( 

27 'Command-line tool tried to exit with nonzero status') from e