+ # overwrite target file: create a temp in the same directory
+ path=os.path.dirname(target) or '.'
+ fd, name = tempfile.mkstemp('','repl',path)
+ os.write(fd,new_contents)
+ os.close(fd)
+ if os.path.exists(target):
+ os.unlink(target)
+ shutil.move(name,target)