You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

160 lines
6.6 KiB

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