classification
Title: os.path.relpath problem with root directory
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: 5799 Superseder:
Assigned To: Nosy List: BreamoreBoy, eckhardt, eli.bendersky, georg.brandl, ixokai, janssen, jimb, ocean-city
Priority: normal Keywords: patch

Created on 2009-01-31 07:25 by eli.bendersky, last changed 2010-10-19 01:59 by ocean-city. This issue is now closed.

Files
File name Uploaded Description Edit
fix_relpath_v3.patch ocean-city, 2009-01-31 17:16 review
py3k_fix_relpath.patch ocean-city, 2010-10-02 14:10 review
py27_fix_relpath.patch ocean-city, 2010-10-02 19:55 review
Messages (19)
msg80860 - (view) Author: Eli Bendersky (eli.bendersky) * (Python committer) Date: 2009-01-31 07:25
This is correct:

relpath(r'd:\abc\jho', r'd:\abc')
=> 'jho'

But this isn't:
relpath(r'd:\jho', r'd:\\')
=> '..\jho'

Neither is this:
relpath(r'd:\jho', r'd:')
=> '..\..\..\jho'
msg80861 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2009-01-31 08:34
About this,

>But this isn't:
>relpath(r'd:\jho', r'd:\\')
>=> '..\jho'

Same happens on posixpath.

from posixpath import relpath
print relpath(r'/abc', r'/') #=> ../abc

I'll look at the code.
msg80862 - (view) Author: Eli Bendersky (eli.bendersky) * (Python committer) Date: 2009-01-31 08:46
The problem is with these lines:

    start_list = abspath(start).split(sep)
    path_list = abspath(path).split(sep)

In case of 'd:\', the split returns two elements, the second empty.
msg80863 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2009-01-31 09:20
I hope attached patch will fix this bug.
msg80877 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2009-01-31 16:21
It seems that there is still problem.

>>> ntpath.relpath("//whiterab-c2znlh/foo/", "//whiterab-c2znlh/bar/")
'..\\foo'

This should raise ValueError because "//whiterab-c2znlh/foo" is UNC root
prefix (like "e:" for normal path) not "//whiterab-c2znlh" AFAIK.

I'll look into more deeper...
msg80880 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2009-01-31 17:16
I hope this works.
msg83146 - (view) Author: Jim Blandy (jimb) Date: 2009-03-04 22:01
In case the behavior requested here is controversial, here's an example
of where it would be nice to have relpath(x, '/') return a path for x
that is relative to the root directory:

The 'oprofile' system profiler for Linux profiles everything running on
the system at once.  Its profile count database takes the form of a
directory tree of counter files that mirrors the whole filesystem:
/var/lib/oprofile/samples/current/{root}/bin/ls holds the samples for
/bin/ls, and so on.  Also, the 'opannotate' command annotates source
trees with profile counts: if /bin/ls were built from /src/ls.c, then
'opannotate -o ~/ann-tree' would put a count-annotated copy of ls's
source '~/ann-tree/src/ls.c'.

At the moment, I wish I could simply say:

src=path to source file
real_src = os.path.realpath(src)
rel_real_src = os.path.relpath(real_src, '/')
annotated_source = os.path.join(annotation_dir, rel_real_src)

but this bug gets in the way.
msg90061 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2009-07-03 14:25
Maybe this patch should be updated because os.path functions changed
their behaviors for UNC in py3k. (#5799)
msg110994 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-07-21 02:59
msg90061 refers to #5799 which is closed, fixed.  Will the attached patch need reworking as a result of the #5799 change?
msg117876 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2010-10-02 14:10
In py3k, ntpath is almost fixed. but posixpath is not fixed yet.
ntpath has another problem about case sensitivity. I'll attach
the patch to fix ntpath's case issue and posixpath.

In Py27, ntpath has whole issue still there.
msg117877 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2010-10-02 14:11
I'll create the patch for it.
msg117894 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2010-10-02 19:55
Well, I said msg80877 past, and I think so too, but
os.path module of python2.x seems not to support UNC
correctly, and I'm not sure if the behavior change is
allowed here.

Probably UNC support is new feature, so I'll attach
the patch to solve only this problem as is.
msg119018 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2010-10-18 13:57
Committed fixes in r85689(py3k), r85693(release31-maint),
r85694(release27-maint). (This should work because I 
simply ported already working fix in ntpath.relpath)
msg119024 - (view) Author: Stephen Hansen (ixokai) Date: 2010-10-18 14:41
FYI, this fix broke some buildbots: http://www.python.org/dev/buildbot/all/builders/x86%20Snow%20Leopard%202.7/builds/50 for instance. Gentoo too.
msg119068 - (view) Author: Bill Janssen (janssen) * (Python committer) Date: 2010-10-18 19:22
Broke bunches of 2.7 buildbots.  But why are we running test_ntpath on OS X, anyway?  Shouldn't this be skipped everywhere except win32 platforms?
msg119069 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-10-18 19:23
No - the posixpath/ntpath routines are meant to be usable for path manipulation for posix/NT paths on any platform.
msg119070 - (view) Author: Bill Janssen (janssen) * (Python committer) Date: 2010-10-18 19:25
Then we need to revert this patch and find one that works.
msg119100 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2010-10-19 01:25
Sorry, I've commit the fix in r85717(release27-maint).
I'll sit and watch buildbot.
msg119101 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2010-10-19 01:59
I confirmed test runs fine on x86 Snow Leopard 2.7.
So I'm closing this issue....
History
Date User Action Args
2010-10-19 01:59:36ocean-citysetstatus: open -> closed
resolution: fixed
messages: + msg119101
2010-10-19 01:25:54ocean-citysetmessages: + msg119100
2010-10-18 19:25:02janssensetresolution: fixed -> (no value)
messages: + msg119070
2010-10-18 19:23:59georg.brandlsetnosy: + georg.brandl
messages: + msg119069
2010-10-18 19:22:09janssensetnosy: + janssen
messages: + msg119068
2010-10-18 18:34:41georg.brandlsetstatus: closed -> open
2010-10-18 14:41:49ixokaisetnosy: + ixokai
messages: + msg119024
2010-10-18 13:57:56ocean-citysetstatus: open -> closed
resolution: fixed
messages: + msg119018

stage: commit review -> resolved
2010-10-08 13:16:15ocean-citysetstage: patch review -> commit review
2010-10-02 19:55:31ocean-citysetfiles: + py27_fix_relpath.patch

messages: + msg117894
2010-10-02 14:11:50ocean-citysetmessages: + msg117877
versions: - Python 2.6
2010-10-02 14:10:40ocean-citysetfiles: + py3k_fix_relpath.patch

messages: + msg117876
2010-09-29 08:51:50eckhardtsetnosy: + eckhardt
2010-07-21 02:59:05BreamoreBoysetnosy: + BreamoreBoy
messages: + msg110994
2009-07-03 14:28:59ocean-citysetversions: + Python 3.2, - Python 3.0
2009-07-03 14:25:30ocean-citysetdependencies: + Change ntpath functions to implicitly support UNC paths
messages: + msg90061
2009-04-22 14:39:08ajaksu2setpriority: normal
stage: patch review
2009-03-04 22:01:21jimbsetnosy: + jimb
messages: + msg83146
2009-02-01 10:56:43ocean-citysettitle: os.path.relpath problem with root drive directory on windows -> os.path.relpath problem with root directory
2009-02-01 07:41:43ocean-citysetversions: + Python 3.0, Python 3.1, Python 2.7
2009-01-31 17:17:00ocean-citysetmessages: - msg80864
2009-01-31 17:16:47ocean-citysetfiles: - fix_relpath_v2.patch
2009-01-31 17:16:35ocean-citysetfiles: - fix_relpath.patch
2009-01-31 17:16:22ocean-citysetfiles: + fix_relpath_v3.patch
messages: + msg80880
2009-01-31 16:21:54ocean-citysetmessages: + msg80877
2009-01-31 10:17:41ocean-citysetfiles: + fix_relpath_v2.patch
2009-01-31 10:17:24ocean-citysetfiles: - fix_relpath_v2.patch
2009-01-31 10:01:57ocean-citysetfiles: + fix_relpath_v2.patch
messages: + msg80864
2009-01-31 09:20:20ocean-citysetfiles: + fix_relpath.patch
keywords: + patch
messages: + msg80863
2009-01-31 08:46:39eli.benderskysetmessages: + msg80862
2009-01-31 08:34:59ocean-citysetnosy: + ocean-city
messages: + msg80861
2009-01-31 07:25:51eli.benderskycreate