Issue1423047

classification
Title: os.path.abspath does not normalize as in Python
Type: Severity: normal
Components: Library Versions:
Milestone:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: cgroves, leouserz, pekka.klarck
Priority: low Keywords:

Created on 2006-02-02.23:35:25 by pekka.klarck, last changed 2007-02-18.06:19:39 by cgroves.

Messages
msg1099 (view) Author: Pekka Klärck (pekka.klarck) Date: 2006-02-02.23:35:25
C:\temp>python
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32
bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for
more information.
>>> import os
>>> os.path.abspath('..')
'C:\\'
>>> ^Z


C:\temp>jython
Jython 2.2a1 on java1.5.0_04 (JIT: null)
Type "copyright", "credits" or "license" for more
information.
>>> import os
>>> os.path.abspath('..')
'C:\\temp\\..'
>>> os.path.normpath(os.path.abspath('..'))
'C:'
>>> ^Z
msg1100 (view) Author: Deleted User leouserz (leouserz) Date: 2007-01-12.21:12:42
the answer may be to change in javapath.py:
def abspath(path):

    path = _tostr(path, "abspath")

    return File(path).getAbsolutePath()


to

def abspath(path):

    path = _tostr(path, "abspath")

    return File(path).getCanonicalPath()

using getCanonicalFile on Windows gives me the same answer as the python example above.

The javadoc for getCanonicalPath states:
 A canonical pathname is both absolute and unique. The precise definition of canonical form is system-dependent. This method first converts this pathname to absolute form if necessary, as if by invoking the getAbsolutePath() method, and then maps it to its unique form in a system-dependent way. This typically involves removing redundant names such as "." and ".." from the pathname, resolving symbolic links (on UNIX platforms), and converting drive letters to a standard case (on Microsoft Windows platforms).
msg1101 (view) Author: Pekka Klärck (pekka.klarck) Date: 2007-01-12.21:38:09
Based on those javadocs and little playing on Jython interpreter getCanonicalPath really seems to be the answer. On top of things mentioned in javadocs it also fixes paths like 'c:\temp\\foo' -> 'c:\temp\foo'. Thanks for looking that up. 
msg1102 (view) Author: Charlie Groves (cgroves) Date: 2007-02-01.06:09:23
getCanonicalPath looks right to me too.  Committed in r3071.
msg1103 (view) Author: Pekka Klärck (pekka.klarck) Date: 2007-02-17.12:13:26
Unfortunately getCanonicalPath has two problems.

1) It raises an IOException in some cases (see e.g. bug [1])

2) It also eliminates symlinks which abspath doesn't do in CPython (there's realpath for that).

As I comment on [1] I have a patch solving both of these issues ready but need write some automated tests for it before submitting.

[1] http://jython.org/bugs/1661700
msg1104 (view) Author: Charlie Groves (cgroves) Date: 2007-02-18.06:19:39
I opened a new bug for laukpe's #2 at http://jython.org/bugs/1662689 rather than just reverting this change since I feel like getCanonical's behavior is still better than the old code's.
History
Date User Action Args
2006-02-02 23:35:25pekka.klarckcreate