Skip to content

Commit 427aeab

Browse files
[REGRESSION] Possible fix for some misconfigured tests (- WIP #117 -)
1 parent 0abae5e commit 427aeab

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

multicast/__init__.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828
__all__ = [
2929
"""__package__""", """__module__""", """__name__""", """__version__""", """__prologue__""",
3030
"""__doc__""", """exceptions""", """exceptions.CommandExecutionError""",
31-
"""exceptions.get_exit_code_from_exception""",
32-
"""exceptions.get_exit_code_from_exception.__func__""",
33-
"""exceptions.exit_on_exception""", """exceptions.exit_on_exception.__func__""",
31+
"""exceptions.get_exit_code_from_exception""", """exceptions.exit_on_exception""",
3432
"""get_exit_code_from_exception""", """exit_on_exception""",
3533
"""skt""", """skt.__package__""", """skt.__module__""", """skt.__name__""",
3634
"""skt.__file__""", """skt.genSocket""", """skt.genSocket.__func__""", """genSocket""",
@@ -358,8 +356,6 @@
358356
raise ModuleNotFoundError("FAIL: we could not import Abstract base class. ABORT.") from None
359357

360358

361-
# global EXIT_CODES # skipcq: PYL-W0604
362-
# global EXCEPTION_EXIT_CODES # skipcq: PYL-W0604
363359
if 'multicast.exceptions' not in sys.modules:
364360
# pylint: disable=cyclic-import - skipcq: PYL-R0401, PYL-C0414
365361
from . import exceptions # pylint: disable=cyclic-import - skipcq: PYL-R0401, PYL-C0414
@@ -627,9 +623,11 @@ def doStep(self, *args): # pragma: no cover
627623

628624

629625
genSocket = skt.genSocket
626+
"""See multicast.skt.genSocket."""
630627

631628

632629
endSocket = skt.endSocket
630+
"""See multicast.skt.endSocket."""
633631

634632

635633
if 'multicast.recv' not in sys.modules:

multicast/exceptions.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,16 @@ def get_exit_code_from_exception(exc):
256256
Testcase 1: Exception with a mapped exit code.
257257
>>> exc = FileNotFoundError('No such file or directory')
258258
>>> get_exit_code_from_exception(exc)
259-
66 # Exit code for FileNotFoundError
259+
66
260260
261261
Testcase 2: Exception without a specific exit code.
262262
>>> exc = Exception('Generic error')
263263
>>> get_exit_code_from_exception(exc)
264-
70 # Default exit code for internal software error
264+
70
265265
266266
"""
267+
if type(exc) in EXCEPTION_EXIT_CODES:
268+
return EXCEPTION_EXIT_CODES[type(exc)]
267269
for exc_class in EXCEPTION_EXIT_CODES:
268270
if isinstance(exc, exc_class):
269271
return EXCEPTION_EXIT_CODES[exc_class]
@@ -274,7 +276,10 @@ def exit_on_exception(func):
274276
"""
275277
Decorator that wraps a function to handle exceptions and exit with appropriate exit codes.
276278
277-
This decorator captures exceptions raised by the wrapped function and handles them by mapping them to predefined exit codes specified in `EXIT_CODES`. It ensures that both `SystemExit` exceptions (which may be raised by modules like `argparse`) and other base exceptions result in the program exiting with meaningful exit codes and error messages.
279+
This decorator captures exceptions raised by the wrapped function and handles them by mapping
280+
them to predefined exit codes specified in `EXIT_CODES`. It ensures that both `SystemExit`
281+
exceptions (which may be raised by modules like `argparse`) and other base exceptions result
282+
in the program exiting with meaningful exit codes and error messages.
278283
279284
Parameters:
280285
func (callable): The function to be wrapped by the decorator.
@@ -303,7 +308,9 @@ def exit_on_exception(func):
303308
>>> @exit_on_exception
304309
... def system_exit_func():
305310
... raise SystemExit(64)
306-
>>> system_exit_func() # Exits with code 64
311+
>>> system_exit_func() #doctest: +IGNORE_EXCEPTION_DETAIL
312+
Traceback (most recent call last):
313+
SystemExit...64...
307314
308315
Testcase 3: Function raises a general exception.
309316
A. Define a function that raises a `ValueError`.
@@ -313,8 +320,9 @@ def exit_on_exception(func):
313320
>>> @exit_on_exception
314321
... def error_func():
315322
... raise ValueError("Invalid value")
316-
>>> error_func() # Exits with code 65
317-
323+
>>> error_func() #doctest: +IGNORE_EXCEPTION_DETAIL
324+
Traceback (most recent call last):
325+
SystemExit...65...
318326
"""
319327
def wrapper(*args, **kwargs):
320328
try:
@@ -327,7 +335,7 @@ def wrapper(*args, **kwargs):
327335
f"{EXIT_CODES.get(exit_code, (1, 'General Error'))[1]}: {exc}",
328336
file=sys.stderr
329337
)
330-
raise SystemExit(code=exit_code) from exc
338+
raise SystemExit(exit_code) from exc
331339
# otherwise sys.exit(exit_code)
332340
except BaseException as exc:
333341
exit_code = get_exit_code_from_exception(exc)
@@ -336,6 +344,6 @@ def wrapper(*args, **kwargs):
336344
f"{EXIT_CODES[exit_code][1]}: {exc}",
337345
file=sys.stderr
338346
)
339-
raise SystemExit(code=exit_code) from exc
347+
raise SystemExit(exit_code) from exc
340348
# otherwise sys.exit(exit_code)
341349
return wrapper

0 commit comments

Comments
 (0)