'Set attribute value on target, memorizing the old value.
By default raise AttributeError if the attribute did not exist.
For convenience you can specify a string as ``target`` which
will be interpreted as a dotted import path, with the last part
being the attribute name.  Example:
``monkeypatch.setattr("os.getcwd", lambda x: "/")``
would set the ``getcwd`` function of the ``os`` module.
The ``raising`` value determines if the setattr should fail
if the attribute is not already present (defaults to True
which means it will raise).'

def setattr(self, target, name, value=notset, raising=True):

    __tracebackhide__ = True
    import inspect
    if (value is notset):
        if (not isinstance(target, _basestring)):
            raise TypeError('use    setattr(target,    name,    value)    or    setattr(target,    value)    with    target    being    a    dotted    import    string')
        value = name
        (name, target) = derive_importpath(target, raising)
    oldval = getattr(target, name, notset)
    if (raising and (oldval is notset)):
        raise AttributeError(('%r    has    no    attribute    %r' % (target, name)))
    if inspect.isclass(target):
        oldval = target.__dict__.get(name, notset)
    self._setattr.append((target, name, oldval))
    setattr(target, name, value)
