PHP 8.2.30
Preview: utils.py Size: 2.74 KB
//opt/cloudlinux/venv/lib/python3.11/site-packages/testfixtures/utils.py

import sys
from functools import wraps
from textwrap import dedent

from inspect import getfullargspec
from typing import Callable, Sequence, Any

from . import singleton

DEFAULT = singleton('DEFAULT')
defaults = [DEFAULT]


try:
    from .mock import DEFAULT
except ImportError:  # pragma: no cover
    pass
else:
    defaults.append(DEFAULT)


def generator(*args):
    """
    A utility function for creating a generator that will yield the
    supplied arguments.
    """
    for i in args:
        yield i


class Wrapping:

    attribute_name = None
    new = DEFAULT

    def __init__(self, before: Callable[[], None], after: Callable[[], None]):
        self.before, self.after = before, after

    def __enter__(self):
        return self.before()

    def __exit__(self, exc_type=None, exc_val=None, exc_tb=None):
        if self.after is not None:
            self.after()


def wrap(before: Callable[[], Any], after: Callable[[], Any] = None):
    """
    A decorator that causes the supplied callables to be called before
    or after the wrapped callable, as appropriate.
    """

    wrapping = Wrapping(before, after)

    def wrapper(func):
        if hasattr(func, 'patchings'):
            func.patchings.append(wrapping)
            return func

        @wraps(func)
        def patched(*args, **keywargs):
            extra_args = []
            entered_patchers = []

            to_add = len(getfullargspec(func).args[len(args):])
            added = 0

            exc_info = (None, None, None)
            try:
                for patching in patched.patchings:
                    arg = patching.__enter__()
                    entered_patchers.append(patching)
                    if patching.attribute_name is not None:
                        keywargs.update(arg)
                    elif patching.new in defaults and added < to_add:
                        extra_args.append(arg)
                        added += 1

                args += tuple(extra_args)
                return func(*args, **keywargs)
            except:
                # Pass the exception to __exit__
                exc_info = sys.exc_info()
                # re-raise the exception
                raise
            finally:
                for patching in reversed(entered_patchers):
                    patching.__exit__(*exc_info)

        patched.patchings = [wrapping]
        return patched

    return wrapper


def extend_docstring(docstring: str, objs: Sequence):
    for obj in objs:
        obj.__doc__ = dedent(obj.__doc__) + docstring


def indent(text: str, indent_size: int = 2):
    indented = []
    for do_indent, line in enumerate(text.splitlines(True)):
        if do_indent:
            line = ' '*indent_size + line
        indented.append(line)
    return ''.join(indented)

Directory Contents

Dirs: 2 × Files: 20

Name Size Perms Modified Actions
tests DIR
- drwxr-xr-x 2026-02-06 08:01:08
Edit Download
- drwxr-xr-x 2026-02-06 08:01:08
Edit Download
39.14 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
224 B lrw-r--r-- 2026-01-20 13:01:47
Edit Download
1.31 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
24.42 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
2.88 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
10.75 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
1.21 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
4.69 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
9.89 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
12.15 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
2.05 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
2.52 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
3.58 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
2.21 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
2.28 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
12.89 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
4.80 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
2.74 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
6 B lrw-r--r-- 2026-01-20 13:01:47
Edit Download
1.19 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download

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