PHP 8.5.2
Preview: monkey.py Size: 2.23 KB
//lib/python3/dist-packages/twisted/python/monkey.py

# -*- test-case-name: twisted.test.test_monkey -*-

# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.


class MonkeyPatcher:
    """
    Cover up attributes with new objects. Neat for monkey-patching things for
    unit-testing purposes.
    """

    def __init__(self, *patches):
        # List of patches to apply in (obj, name, value).
        self._patchesToApply = []
        # List of the original values for things that have been patched.
        # (obj, name, value) format.
        self._originals = []
        for patch in patches:
            self.addPatch(*patch)

    def addPatch(self, obj, name, value):
        """
        Add a patch so that the attribute C{name} on C{obj} will be assigned to
        C{value} when C{patch} is called or during C{runWithPatches}.

        You can restore the original values with a call to restore().
        """
        self._patchesToApply.append((obj, name, value))

    def _alreadyPatched(self, obj, name):
        """
        Has the C{name} attribute of C{obj} already been patched by this
        patcher?
        """
        for o, n, v in self._originals:
            if (o, n) == (obj, name):
                return True
        return False

    def patch(self):
        """
        Apply all of the patches that have been specified with L{addPatch}.
        Reverse this operation using L{restore}.
        """
        for obj, name, value in self._patchesToApply:
            if not self._alreadyPatched(obj, name):
                self._originals.append((obj, name, getattr(obj, name)))
            setattr(obj, name, value)

    __enter__ = patch

    def restore(self):
        """
        Restore all original values to any patched objects.
        """
        while self._originals:
            obj, name, value = self._originals.pop()
            setattr(obj, name, value)

    def __exit__(self, excType=None, excValue=None, excTraceback=None):
        self.restore()

    def runWithPatches(self, f, *args, **kw):
        """
        Apply each patch already specified. Then run the function f with the
        given args and kwargs. Restore everything when done.
        """
        self.patch()
        try:
            return f(*args, **kw)
        finally:
            self.restore()

Directory Contents

Dirs: 3 × Files: 46

Name Size Perms Modified Actions
test DIR
- drwxr-xr-x 2026-01-08 12:56:22
Edit Download
- drwxr-xr-x 2026-01-08 12:55:41
Edit Download
- drwxr-xr-x 2026-01-08 12:56:23
Edit Download
16.42 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
13.87 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
460 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.96 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
27.49 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
27.43 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
6.88 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
58.92 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
11.82 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.54 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
7.79 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
21.89 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
9.88 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
26.21 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
2.23 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.34 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.43 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
6.96 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
20.02 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.08 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
7.01 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
5.79 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
2.62 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
2.23 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.57 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
5.45 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
5.28 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.25 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
10.65 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.34 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
244 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
8.25 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
33.79 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
26.86 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
273 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
4.66 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
11.99 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
9.45 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
828 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.42 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
8.57 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
24.68 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
8.88 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.12 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
228 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
598 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download

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