<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def mock(self, obj=None, attr=None, **kwargs):
        '''
        Return a mock object.
        '''
        rval = Mock(**kwargs)
        if obj is not None and attr is not None:
            rval._object = obj
            rval._attr = attr

            if hasattr(obj, attr):
                orig = getattr(obj, attr)
                self._mocks.append((obj, attr, orig))
                setattr(obj, attr, rval)
            else:
                self._mocks.append((obj, attr))
                setattr(obj, attr, rval)
        return rval