Title: Shouldn't shutil.copyfile replace link in dst with follow_symlinks set?
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Pavel Raiskup, r.david.murray, theger
Priority: normal Keywords:

Created on 2018-08-10 17:36 by theger, last changed 2020-09-15 06:23 by Pavel Raiskup.

Messages (3)
msg323373 - (view) Author: (theger) Date: 2018-08-10 17:36
This might not be an issue at all and maybe I'm just missing something but this confused me a bit and hence the question in title.

Python's shutil.copyfile() has a follow_symlinks argument but it only affects the source, not the destination - i.e. when src is a symlink, it makes a difference whether the flag is set (and then it will copy the destination of that src symlink) or not (and then it will just create a new symlink in dst pointing to the src symlink's destination). I hope it's clear so far.

When dst is a link though, the flag doesn't change anything and it will always copy that file to the dst symlink's destination.

My question is... If dst is a symlink and follow_symlinks flag is not set, wouldn't it be more logical if the dst symlink was just replaced by a copy of src instead of copying src to dst's destination?

Thanks and sorry if this is just noise.
msg323374 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-08-10 17:56
It is following the model of the posix cp command, whose equivalent to 'follow_symlinks' only affects the source.
msg376928 - (view) Author: Pavel Raiskup (Pavel Raiskup) Date: 2020-09-15 06:23
Check out this default behavior of /bin/cp though:

    $ mkdir a b
    $ echo content > a/file
    $ ln -s non-existing b/file
    $ cp a/file b
    cp: not writing through dangling symlink 'b/file'

Shouldn't shutil.copy*() refuse to write trough a dangling symlink to non-existent
file?  (ATM it seems it just silently creates the file, or fails if it can not be
Date User Action Args
2020-09-15 06:23:53Pavel Raiskupsetnosy: + Pavel Raiskup
messages: + msg376928
2018-08-10 17:56:20r.david.murraysetnosy: + r.david.murray
messages: + msg323374
2018-08-10 17:36:47thegercreate