Message8246

Author jeff.allen
Recipients jeff.allen
Date 2014-03-05.22:44:02
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1394059444.77.0.400460032292.issue2117@psf.upfronthosting.co.za>
In-reply-to
Content
I discovered this while working on an apparently simple test failure in test_chdir. The problem seems to be mainly with our conversion to absolute paths, used everywhere in the os module, and some places in sys. We have a confusing mixture of Python and Java code that's just a bit too complicated to put right as part of the chdir test, so I'm parking it here.

java.io.File gets this right, and provides equivalents to abspath and realpath I think we could take advantage of directly. Or one could, once he had a really clear idea of the intended behaviour.

Examples for comparison follow. The Unix "current working directory" is complicated on Windows, which keeps a CWD for each drive letter. In a clean command window (in D:\hg\chdirtest) these initial cd commands set CWD on the drives mentioned, but don't make then the actual CWD for the window:

>cd c:\Users\Jeff\Documents
>cd e:\test
>python
Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, os, os.path
>>> os.getcwd()
'D:\\hg\\chdirtest'
>>> os.path.abspath('aa')
'D:\\hg\\chdirtest\\aa'
>>> os.path.abspath('d:aa')
'D:\\hg\\chdirtest\\aa'
>>> os.path.abspath('c:aa')
'c:\\Users\\Jeff\\Documents\\aa'
>>> os.path.abspath('e:aa')
'e:\\test\\aa'
>>> os.path.abspath('c:\\aa')
'c:\\aa'
>>> os.chdir('e:')
>>> os.getcwd()
'e:\\test'
>>> os.chdir('d:\\hg')
>>> os.getcwd()
'd:\\hg'
>>> os.chdir('e:')
>>> os.getcwd()
'e:\\test'
>>> os.path.abspath('d:aa')
'd:\\hg\\aa'
>>> os.path.abspath('aa')
'e:\\test\\aa'
>>> os.chdir('d:\\hg')
>>> os.getcwd()
'd:\\hg'
>>> os.path.abspath('aa')
'd:\\hg\\aa'
>>>

The CPython code mirrors what the OS does in tracking the per drive CWD. Jython can't get the paths right at all when a drive letter is involved and doesn't understand the per drive CWD:

>cd c:\Users\Jeff\Documents
>cd e:\test
>..\jython-int\dist\bin\jython
Jython 2.7b1+ (default:78efc2f8c832, Mar 2 2014, 20:03:25)
[Java HotSpot(TM) 64-Bit Server VM (Sun Microsystems Inc.)] on java1.6.0_45
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, os, os.path
>>> os.getcwd()
'D:\\hg\\chdirtest'
>>> os.path.abspath('aa')
'D:\\hg\\chdirtest\\aa'
>>> os.path.abspath('d:aa')
'D:\\hg\\chdirtest\\d:aa'
>>> os.path.abspath('c:aa')
'D:\\hg\\chdirtest\\c:aa'
>>> os.path.abspath('e:aa')
'D:\\hg\\chdirtest\\e:aa'
>>> os.path.abspath('c:\\aa')
'c:\\aa'
>>> os.chdir('e:')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 20047] Unknown error: 20047: 'D:\\hg\\chdirtest\\e:'
>>> os.getcwd()
'D:\\hg\\chdirtest'
>>> os.chdir('d:\\hg')
>>> os.getcwd()
'd:\\hg'
>>> os.chdir('e:')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 20047] Unknown error: 20047: 'd:\\hg\\e:'
>>> os.getcwd()
'd:\\hg'
>>> os.chdir('c:\\Users\\Jeff\\Documents')
>>> os.path.abspath('d:aa')
'c:\\Users\\Jeff\\Documents\\d:aa'
>>> os.path.abspath('..')
'c:\\Users\\Jeff'

However, java.io.File makes a pretty good start:
>>> import java.io
>>> java.io.File('d:aa').getAbsolutePath()
u'd:\\hg\\chdirtest\\aa'
>>> java.io.File('e:bb').getAbsolutePath()
u'e:\\test\\bb'
>>>
History
Date User Action Args
2014-03-05 22:44:04jeff.allensetrecipients: + jeff.allen
2014-03-05 22:44:04jeff.allensetmessageid: <1394059444.77.0.400460032292.issue2117@psf.upfronthosting.co.za>
2014-03-05 22:44:04jeff.allenlinkissue2117 messages
2014-03-05 22:44:02jeff.allencreate