sources for patch.py [rev. 38799]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
patched = {}
def patch(namespace, name, value):
    """ rebind the 'name' on the 'namespace'  to the 'value',
        possibly and remember the original value. Multiple
        invocations to the same namespace/name pair will
        remember a list of old values.
    """
    nref = (namespace, name)
    orig = getattr(namespace, name)
    patched.setdefault(nref, []).append(orig)
    setattr(namespace, name, value)
    return orig
def revert(namespace, name):
    """ revert to the orginal value the last patch modified.
        Raise ValueError if no such original value exists.
    """
    nref = (namespace, name)
    if nref not in patched or not patched[nref]:
        raise ValueError, "No original value stored for %s.%s" % nref
    current = getattr(namespace, name)
    orig = patched[nref].pop()
    setattr(namespace, name, orig)
    return current