This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Tom Hale
Recipients Tom Hale
Date 2019-04-19.07:40:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1555659627.62.0.0676845139921.issue36656@roundup.psfhosted.org>
In-reply-to
Content
The most correct work-around I believe exists is:

(updates at: https://stackoverflow.com/a/55742015/5353461)

    def symlink_force(target, link_name):
        '''
        Create a symbolic link pointing to target named link_name.
        Overwrite target if it exists.
        '''
    
        # os.replace may fail if files are on different filesystems.
        # Therefore, use the directory of target
        link_dir = os.path.dirname(target)
    
        # os.symlink requires that the target does NOT exist.
        # Avoid race condition of file creation between mktemp and symlink:
        while True:
            temp_pathname = tempfile.mktemp(suffix='.tmp', \
                            prefix='symlink_force_tmp-', dir=link_dir)
            try:
                os.symlink(target, temp_pathname)
                break  # Success, exit loop
            except FileExistsError:
                time.sleep(0.001)  # Prevent high load in pathological conditions
            except:
                raise
        os.replace(temp_pathname, link_name)

An unlikely race condition still remains: the symlink created at the randomly-named `temp_path` could be modified between creation and rename/replacing the specified link name.

Suggestions for improvement welcome.
History
Date User Action Args
2019-04-19 07:40:27Tom Halesetrecipients: + Tom Hale
2019-04-19 07:40:27Tom Halesetmessageid: <1555659627.62.0.0676845139921.issue36656@roundup.psfhosted.org>
2019-04-19 07:40:27Tom Halelinkissue36656 messages
2019-04-19 07:40:27Tom Halecreate