[New-bugs-announce] [issue37019] using pathlib objects to create symlinks produces broken links

Shannon report at bugs.python.org
Thu May 23 03:09:02 EDT 2019


New submission from Shannon <shannonjkeough at googlemail.com>:

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.

----------
components: Library (Lib)
messages: 343277
nosy: skeo
priority: normal
severity: normal
status: open
title: using pathlib objects to create symlinks produces broken links
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37019>
_______________________________________


More information about the New-bugs-announce mailing list