'Decorator that ensures that function calls leave the pos
in the underlying file unchanged'

def safe(func):

    @wraps(func)
    def restore_pos(*args, **kwargs):
        obj = args[0]
        pos = obj._file.tell()
        res = func(*args, **kwargs)
        obj._file.seek(0, 2)
        if (obj._file.tell() >= pos):
            obj._file.seek(pos)
        return res
    return restore_pos
