PHP 8.3.31
Preview: errors.py Size: 2.62 KB
//proc/thread-self/root/opt/hc_python/lib/python3.12/site-packages/packaging/errors.py

from __future__ import annotations

import contextlib
import dataclasses
import sys
import typing

__all__ = ["ExceptionGroup"]


def __dir__() -> list[str]:
    return __all__


if sys.version_info >= (3, 11):  # pragma: no cover
    from builtins import ExceptionGroup
else:  # pragma: no cover

    class ExceptionGroup(Exception):
        """A minimal implementation of :external:exc:`ExceptionGroup` from Python 3.11.

        If :external:exc:`ExceptionGroup` is already defined by Python itself,
        that version is used instead.
        """

        message: str
        exceptions: list[Exception]

        def __init__(self, message: str, exceptions: list[Exception]) -> None:
            self.message = message
            self.exceptions = exceptions

        def __repr__(self) -> str:
            return f"{self.__class__.__name__}({self.message!r}, {self.exceptions!r})"


@dataclasses.dataclass
class _ErrorCollector:
    """
    Collect errors into ExceptionGroups.

    Used like this:

        collector = _ErrorCollector()
        # Add a single exception
        collector.error(ValueError("one"))

        # Supports nesting, including combining ExceptionGroups
        with collector.collect():
            raise ValueError("two")
        collector.finalize("Found some errors")

    Since making a collector and then calling finalize later is a common pattern,
    a convenience method ``on_exit`` is provided.
    """

    errors: list[Exception] = dataclasses.field(default_factory=list, init=False)

    def finalize(self, msg: str) -> None:
        """Raise a group exception if there are any errors."""
        if self.errors:
            raise ExceptionGroup(msg, self.errors)

    @contextlib.contextmanager
    def on_exit(self, msg: str) -> typing.Generator[_ErrorCollector, None, None]:
        """
        Calls finalize if no uncollected errors were present.

        Uncollected errors are raised normally.
        """
        yield self
        self.finalize(msg)

    @contextlib.contextmanager
    def collect(self, *err_cls: type[Exception]) -> typing.Generator[None, None, None]:
        """
        Context manager to collect errors into the error list.

        Must be inside loops, as only one error can be collected at a time.
        """
        error_classes = err_cls or (Exception,)
        try:
            yield
        except ExceptionGroup as error:
            self.errors.extend(error.exceptions)
        except error_classes as error:
            self.errors.append(error)

    def error(
        self,
        error: Exception,
    ) -> None:
        """Add an error to the list."""
        self.errors.append(error)

Directory Contents

Dirs: 2 × Files: 19

Name Size Perms Modified Actions
licenses DIR
- drwxr-xr-x 2026-06-11 06:30:31
Edit Download
- drwxr-xr-x 2026-06-11 06:30:31
Edit Download
9.98 KB lrw-r--r-- 2026-06-11 06:30:31
Edit Download
10.66 KB lrw-r--r-- 2026-06-11 06:30:31
Edit Download
2.62 KB lrw-r--r-- 2026-06-11 06:30:31
Edit Download
16.66 KB lrw-r--r-- 2026-06-11 06:30:31
Edit Download
37.86 KB lrw-r--r-- 2026-06-11 06:30:31
Edit Download
0 B lrw-r--r-- 2026-06-11 06:30:31
Edit Download
33.10 KB lrw-r--r-- 2026-06-11 06:30:31
Edit Download
4.28 KB lrw-r--r-- 2026-06-11 06:30:31
Edit Download
69.84 KB lrw-r--r-- 2026-06-11 06:30:31
Edit Download
33.42 KB lrw-r--r-- 2026-06-11 06:30:31
Edit Download
9.62 KB lrw-r--r-- 2026-06-11 06:30:31
Edit Download
37.47 KB lrw-r--r-- 2026-06-11 06:30:31
Edit Download
3.14 KB lrw-r--r-- 2026-06-11 06:30:31
Edit Download
9.33 KB lrw-r--r-- 2026-06-11 06:30:31
Edit Download
2.64 KB lrw-r--r-- 2026-06-11 06:30:31
Edit Download
11.42 KB lrw-r--r-- 2026-06-11 06:30:31
Edit Download
1.08 KB lrw-r--r-- 2026-06-11 06:30:31
Edit Download
5.26 KB lrw-r--r-- 2026-06-11 06:30:31
Edit Download
494 B lrw-r--r-- 2026-06-11 06:30:31
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).