python / python/cpython

os.path.relpath() assumes '//' root is equivalent to '/'

Open
#92,737 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Bug report

pathlib's Path.relative_to() and os.path.relpath(), the two have different behavior: On Linux os.path.relpath() understands double-slash in path is same as single slash, but Path.relative_to() does not.

For example,

import os
os.path.relpath('//foo/bar', '/foo')                 # results 'bar'

from pathlib import Path
pathlib.PosixPath('//foo/bar').relative_to('/foo')   # raises ValueError

pytest issue https://github.com/pytest-dev/pytest/issues/9894 relates to this.

Your environment

  • CPython versions tested on: 3.9.2
  • Operating system and architecture: Debian Bullseye
Linked PRs
  • gh-103798

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing os.path.relpath() and pathlib.PosixPath.relative_to() for the //foo/bar versus /foo example, then review pytest issue #9894 and linked PR gh-103798. Done means the intended behavior is resolved consistently and the example is covered by relevant tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.