'Decorator that ensures that read or writing a property leaves
the position in the underlying file unchanged'

def safe_property(func):

    def decorator(f):
        def restore_pos(*args, **kwargs):
            obj = args[0]
            pos = obj._file.tell()
            res = f(*args, **kwargs)
            obj._file.seek(0, 2)
            if (obj._file.tell() >= pos):
                obj._file.seek(pos)
            return res
        return restore_pos
    locals_ = func()
    if locals_.has_key('fget'):
        locals_['fget'] = decorator(locals_['fget'])
    if locals_.has_key('fset'):
        locals_['fset'] = decorator(locals_['fset'])
    return property(**locals_)
