On Mon, May 13, 2019 at 04:38:08PM +0700, Tom Hale wrote:

> As suggested by Toshio Kuratomi at https://bugs.python.org/issue36656, I 
> am raising this here for inclusion in the shutil module.
> 
> Mimicking POSIX, os.symlink() will raise FileExistsError if the link 
> name to be created already exists.

Seems reasonable. I presume that's the current behaviour.


> A common use case is overwriting an existing file (often a symlink) with 
> a symlink. Naively, one would delete the file named link_name file if it 
> exists, then call symlink(). This "solution" is already 3 lines of code, 
> and without exception handling it introduces the race condition of a 
> file named link_name being created between unlink and symlink.
> 
> Depending on the functionality required, I suggest:
> 
> * os.symlink() - the new link name is expected to NOT exist
> * shutil.symlink() - the new symlink replaces an existing file

+1


> One issue I see with my suggested code is that the file at 
> temp_link_name could be changed before target is replaced with it. This 
> is mitigated by the randomness introduced by mktemp().

Arguing over the fine details of implementation should probably be done 
on the bug tracker, between the implementor and whoever is reviewing the 
code. As far as I'm concerned, the proposed interface is a good one:

- os.symlink for the low level function
- shutil.symlink for a high-level wrapper that handles the common case
  of wanting to overwrite an existing file.



-- 
Steven
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to