METADATA 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. Metadata-Version: 2.1
  2. Name: exceptiongroup
  3. Version: 1.1.3
  4. Summary: Backport of PEP 654 (exception groups)
  5. Author-email: Alex Grönholm <alex.gronholm@nextday.fi>
  6. Requires-Python: >=3.7
  7. Description-Content-Type: text/x-rst
  8. Classifier: Development Status :: 5 - Production/Stable
  9. Classifier: Intended Audience :: Developers
  10. Classifier: License :: OSI Approved :: MIT License
  11. Classifier: Programming Language :: Python
  12. Classifier: Programming Language :: Python :: 3 :: Only
  13. Classifier: Typing :: Typed
  14. Requires-Dist: pytest >= 6 ; extra == "test"
  15. Project-URL: Changelog, https://github.com/agronholm/exceptiongroup/blob/main/CHANGES.rst
  16. Project-URL: Issue Tracker, https://github.com/agronholm/exceptiongroup/issues
  17. Project-URL: Source code, https://github.com/agronholm/exceptiongroup
  18. Provides-Extra: test
  19. .. image:: https://github.com/agronholm/exceptiongroup/actions/workflows/test.yml/badge.svg
  20. :target: https://github.com/agronholm/exceptiongroup/actions/workflows/test.yml
  21. :alt: Build Status
  22. .. image:: https://coveralls.io/repos/github/agronholm/exceptiongroup/badge.svg?branch=main
  23. :target: https://coveralls.io/github/agronholm/exceptiongroup?branch=main
  24. :alt: Code Coverage
  25. This is a backport of the ``BaseExceptionGroup`` and ``ExceptionGroup`` classes from
  26. Python 3.11.
  27. It contains the following:
  28. * The ``exceptiongroup.BaseExceptionGroup`` and ``exceptiongroup.ExceptionGroup``
  29. classes
  30. * A utility function (``exceptiongroup.catch()``) for catching exceptions possibly
  31. nested in an exception group
  32. * Patches to the ``TracebackException`` class that properly formats exception groups
  33. (installed on import)
  34. * An exception hook that handles formatting of exception groups through
  35. ``TracebackException`` (installed on import)
  36. * Special versions of some of the functions from the ``traceback`` module, modified to
  37. correctly handle exception groups even when monkey patching is disabled, or blocked by
  38. another custom exception hook:
  39. * ``traceback.format_exception()``
  40. * ``traceback.format_exception_only()``
  41. * ``traceback.print_exception()``
  42. * ``traceback.print_exc()``
  43. If this package is imported on Python 3.11 or later, the built-in implementations of the
  44. exception group classes are used instead, ``TracebackException`` is not monkey patched
  45. and the exception hook won't be installed.
  46. See the `standard library documentation`_ for more information on exception groups.
  47. .. _standard library documentation: https://docs.python.org/3/library/exceptions.html
  48. Catching exceptions
  49. ===================
  50. Due to the lack of the ``except*`` syntax introduced by `PEP 654`_ in earlier Python
  51. versions, you need to use ``exceptiongroup.catch()`` to catch exceptions that are
  52. potentially nested inside an exception group. This function returns a context manager
  53. that calls the given handler for any exceptions matching the sole argument.
  54. The argument to ``catch()`` must be a dict (or any ``Mapping``) where each key is either
  55. an exception class or an iterable of exception classes. Each value must be a callable
  56. that takes a single positional argument. The handler will be called at most once, with
  57. an exception group as an argument which will contain all the exceptions that are any
  58. of the given types, or their subclasses. The exception group may contain nested groups
  59. containing more matching exceptions.
  60. Thus, the following Python 3.11+ code:
  61. .. code-block:: python3
  62. try:
  63. ...
  64. except* (ValueError, KeyError) as excgroup:
  65. for exc in excgroup.exceptions:
  66. print('Caught exception:', type(exc))
  67. except* RuntimeError:
  68. print('Caught runtime error')
  69. would be written with this backport like this:
  70. .. code-block:: python3
  71. from exceptiongroup import ExceptionGroup, catch
  72. def value_key_err_handler(excgroup: ExceptionGroup) -> None:
  73. for exc in excgroup.exceptions:
  74. print('Caught exception:', type(exc))
  75. def runtime_err_handler(exc: ExceptionGroup) -> None:
  76. print('Caught runtime error')
  77. with catch({
  78. (ValueError, KeyError): value_key_err_handler,
  79. RuntimeError: runtime_err_handler
  80. }):
  81. ...
  82. **NOTE**: Just like with ``except*``, you cannot handle ``BaseExceptionGroup`` or
  83. ``ExceptionGroup`` with ``catch()``.
  84. Notes on monkey patching
  85. ========================
  86. To make exception groups render properly when an unhandled exception group is being
  87. printed out, this package does two things when it is imported on any Python version
  88. earlier than 3.11:
  89. #. The ``traceback.TracebackException`` class is monkey patched to store extra
  90. information about exception groups (in ``__init__()``) and properly format them (in
  91. ``format()``)
  92. #. An exception hook is installed at ``sys.excepthook``, provided that no other hook is
  93. already present. This hook causes the exception to be formatted using
  94. ``traceback.TracebackException`` rather than the built-in rendered.
  95. If ``sys.exceptionhook`` is found to be set to something else than the default when
  96. ``exceptiongroup`` is imported, no monkeypatching is done at all.
  97. To prevent the exception hook and patches from being installed, set the environment
  98. variable ``EXCEPTIONGROUP_NO_PATCH`` to ``1``.
  99. Formatting exception groups
  100. ---------------------------
  101. Normally, the monkey patching applied by this library on import will cause exception
  102. groups to be printed properly in tracebacks. But in cases when the monkey patching is
  103. blocked by a third party exception hook, or monkey patching is explicitly disabled,
  104. you can still manually format exceptions using the special versions of the ``traceback``
  105. functions, like ``format_exception()``, listed at the top of this page. They work just
  106. like their counterparts in the ``traceback`` module, except that they use a separately
  107. patched subclass of ``TracebackException`` to perform the rendering.
  108. Particularly in cases where a library installs its own exception hook, it is recommended
  109. to use these special versions to do the actual formatting of exceptions/tracebacks.
  110. .. _PEP 654: https://www.python.org/dev/peps/pep-0654/