/usr/lib/python3/dist-packages/twisted/python
NameSizeModeActions
test/-0755rm
_pydoctortemplates/-0755rm
__pycache__/-0755rm
compat.py231940644editdlrm
components.py142950644editdlrm
constants.py5440644editdlrm
context.py40240644editdlrm
deprecate.py267750644editdlrm
failure.py266380644editdlrm
fakepwd.py61380644editdlrm
filepath.py588870644editdlrm
formmethod.py114600644editdlrm
htmlizer.py35410644editdlrm
lockfile.py77180644editdlrm
log.py224720644editdlrm
logfile.py100830644editdlrm
modules.py271410644editdlrm
monkey.py22270644editdlrm
procutils.py14190644editdlrm
randbytes.py39590644editdlrm
rebuild.py92680644editdlrm
reflect.py194770644editdlrm
release.py11920644editdlrm
roots.py74040644editdlrm
runtime.py62700644editdlrm
sendmsg.py34200644editdlrm
shortcut.py22530644editdlrm
syslog.py37300644editdlrm
systemd.py28340644editdlrm
text.py54820644editdlrm
threadable.py32970644editdlrm
threadpool.py98400644editdlrm
twisted-completion.zsh13710644editdlrm
url.py2440644editdlrm
urlpath.py90840644editdlrm
usage.py350100644editdlrm
util.py279310644editdlrm
versions.py3220644editdlrm
win32.py43170644editdlrm
zippath.py92370644editdlrm
zipstream.py97570644editdlrm
_appdirs.py7880644editdlrm
_inotify.py34550644editdlrm
_oldstyle.py25930644editdlrm
_release.py185480644editdlrm
_setup.py129530644editdlrm
_shellcomp.py248300644editdlrm
_textattributes.py90790644editdlrm
_tzhelper.py31920644editdlrm
_url.py2530644editdlrm
__init__.py6740644editdlrm
Edit: /usr/lib/python3/dist-packages/twisted/python/monkey.py (2227B)
# -*- test-case-name: twisted.test.test_monkey -*- # Copyright (c) Twisted Matrix Laboratories. # See LICENSE for details. from __future__ import division, absolute_import class MonkeyPatcher(object): """ 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) 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 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()