Skip to content

pathlib.PurePath.relative_to(walk_up=True) mishandles '..' components #105002

Closed
@barneygale

Description

@barneygale

pathlib.PurePath.relative_to(other, walk_up=True) doesn't handle '..' segments in its other argument correctly:

>>> from pathlib import PurePath, Path
>>> Path.cwd()
PosixPath('/home/barney/projects/cpython')
>>> PurePath('a/b').relative_to('a/..', walk_up=True)
PurePosixPath('../b')
# expected: ValueError (ideal world: 'a/b')
>>> PurePath('a/b').relative_to('a/../..', walk_up=True)
PurePosixPath('../../b')
# expected: ValueError (ideal world: 'cpython/a/b')

PurePath objects do not know the current working directory, nor can they safely eliminate .. segments without resolving symlinks, so I think raising ValueError is the only reasonable thing to do.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13bugs and security fixestopic-pathlibtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions