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 skeo
Recipients skeo
Date 2019-05-23.07:09:02
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1558595342.46.0.940919584419.issue37019@roundup.psfhosted.org>
In-reply-to
Content
when using pathlib objects to define src and dst for os.symlink (also relevant for Path(dst).symlink_to(src)), if the src path is not absolute, or relative to the directory the link is being created in, a broken link will be created.

example/

src = pathlib.Path('dir1/file')
dst = pathlib.Path('dir2/file')
os.symlink(src, dst) # or dst.symlink_to(src)

this will create a broken link in dir2, attempting to link to dir1/file, relative to dir2.

It seems to me, if src given is a pathlib object (relative to cwd), the linking process should be smart enough to point the link the created symlink to the right place.

os.symlink(src.absolute(), dst) works, but creates an absolute symlink which may not be desired behaviour.

My current workaround is:
os.symlink(os.path.relpath(src, dst.parent), dst)
which creates a working relative symlink as desired. I would suggest this should be the default behaviour of both os.symlink and pathlib.Path().symlink_to when a non-absolute path object is given as src.

Interestingly, src.relative_to(dst.parent) will raise a ValueError while os.path.relpath(src, dst.parent) correctly returns '../dir1/file'.
I also think Path().relative_to should be changed to match the behaviour of os.path.relpath here, but perhaps that is a separate issue.
History
Date User Action Args
2019-05-23 07:09:02skeosetrecipients: + skeo
2019-05-23 07:09:02skeosetmessageid: <1558595342.46.0.940919584419.issue37019@roundup.psfhosted.org>
2019-05-23 07:09:02skeolinkissue37019 messages
2019-05-23 07:09:02skeocreate