[New-bugs-announce] [issue42464] Pathlib resolve() resolves non-existent ".." components with strict=False

John Engelke report at bugs.python.org
Wed Nov 25 11:07:29 EST 2020


New submission from John Engelke <john.engelke at gmail.com>:

The below snippet results in the symlink "/home" resolving as expected. However, documentation at https://docs.python.org/3.7/library/pathlib.html#pathlib.Path.resolve suggests, "If strict is False, the path is resolved as far as possible and any remainder is appended without checking whether it exists."

>>> from pathlib import Path
>>> host_path_str = "/home/somewhere/there/../nowhere"
>>> host_path = Path(host_path_str)
>>> host_path
PosixPath('/home/somewhere/there/../nowhere')
>>> host_path.resolve()
PosixPath('/System/Volumes/Data/home/somewhere/nowhere')

Expected results (based on the wording above): 

>>> host_path.resolve()
PosixPath('/System/Volumes/Data/home/somewhere/there/../nowhere')

Right now the ".." pieces are universally removed. I'm not exactly sure exactly how symlinks resolve, but this might create unexpected results. 

No part of the path component "somewhere/there/../nowhere" exists. When strict=True this would yield an error. So when strict=False, and it doesn't resolve, documentation implies that would be re-added to the resolved section.

----------
components: Library (Lib)
messages: 381844
nosy: john.engelke
priority: normal
severity: normal
status: open
title: Pathlib resolve() resolves non-existent ".." components with strict=False
type: behavior
versions: Python 3.10, Python 3.7, Python 3.8, Python 3.9

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


More information about the New-bugs-announce mailing list