classification
Title: Fix for bugs relating to ntpath.expanduser()
Type: Stage:
Components: Windows Versions: Python 3.0, Python 2.6
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: Nosy List: chomo, christian.heimes, georg.brandl, gjb1002, josiahcarlson, loewis, mwh
Priority: normal Keywords: patch

Created on 2004-05-20 20:35 by josiahcarlson, last changed 2009-04-30 14:56 by gjb1002. This issue is now closed.

Files
File name Uploaded Description Edit
ntpath.patch josiahcarlson, 2004-05-20 20:35 Patch for ntpath that fixes bugs.
ntpath_test.txt josiahcarlson, 2004-05-20 20:37 Testing output on unpatched and patched ntpath
ntpath_updated.patch josiahcarlson, 2006-10-22 21:23 Updated patch for expanduser and expandvars
ntpath_full.patch josiahcarlson, 2007-03-13 20:49 expanduser, expandvars, doc, and tests
Messages (16)
msg46031 - (view) Author: Josiah Carlson (josiahcarlson) * Date: 2004-05-20 20:35
Attached is a patch for sf bug #796219 that fixes
ntpath.expanduser() for paths that embed other
environment variables, and also includes functionality
that mirrors *nix-style ~user\extra expansions.

I will comment with output from both the unpatched and
patched version of ntpath.
msg46032 - (view) Author: Josiah Carlson (josiahcarlson) * Date: 2004-05-20 20:36
Logged In: YES 
user_id=341410

#test setup:
>>> import os
>>> os.environ['TESTING'] = '%TESTING1%'
>>> os.environ['TESTING1'] = '%TESTING2%'
>>> os.environ['TESTING2'] = 'Final\\Path'

#test standard ntpath
>>> import ntpath
>>> ntpath.expanduser('~')
'C:\\Documents and Settings\\jcarlson'
>>> ntpath.expanduser('~billy')
'~billy'
>>> ntpath.expanduser('~billy\\bob')
'~billy\\bob'
>>> ntpath.expanduser('~\\bob')
'C:\\Documents and Settings\\jcarlson\\bob'
>>>
ntpath.expanduser('~billy\\%TESTING%\\%TESTING1%\\%TESTING2%')
'~billy\\%TESTING%\\%TESTING1%\\%TESTING2%'

#test patched ntpath
>>> import ntpath_patched
>>> ntpath_patched.expanduser('~')
'C:Documents and Settings\\jcarlson'
>>> ntpath_patched.expanduser('~billy')
'C:Documents and Settings\\billy'
>>> ntpath_patched.expanduser('~billy\\bob')
'C:Documents and Settings\\billy\\bob'
>>> ntpath_patched.expanduser('~\\bob')
'C:Documents and Settings\\jcarlson\\bob'
>>>
ntpath_patched.expanduser('~billy\\%TESTING%\\%TESTING1%\\%TESTING2%')
'C:Documents and
Settings\\billy\\Final\\Path\\Final\\Path\\Final\\Path'
msg46033 - (view) Author: Josiah Carlson (josiahcarlson) * Date: 2004-05-20 20:38
Logged In: YES 
user_id=341410

I uploaded the testing as text to alleviate text wrapping
issues that could confuse.
msg46034 - (view) Author: alan johnson (chomo) Date: 2004-06-14 20:44
Logged In: YES 
user_id=943591

this problem maybe is whats
needed
msg46035 - (view) Author: Josiah Carlson (josiahcarlson) * Date: 2004-06-15 01:11
Logged In: YES 
user_id=341410

What problem is needed?

Perhaps you mean "this solution is desireable".
msg46036 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2004-06-15 10:45
Logged In: YES 
user_id=6656

This looks much better to me!
msg46037 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2004-06-15 10:46
Logged In: YES 
user_id=6656

bugger, wrong report :-/
msg46038 - (view) Author: Josiah Carlson (josiahcarlson) * Date: 2006-10-22 21:25
Logged In: YES 
user_id=341410

I've just attached an updated version of the patch to handle
expandusers (without recursive environment variable
expansion), as well as a variant of
http://python.org/sf/1574252 to handle %VAR% style
expansions on Windows.
msg46039 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-03-13 08:39
The patch looks good to me; documentation updates and new tests will be needed though.
msg46040 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-03-13 08:41
This fixes #796219.
msg46041 - (view) Author: Josiah Carlson (josiahcarlson) * Date: 2007-03-13 20:49
I have just attached an updated patch (against trunk) for ntpath expandusers and expandvars that includes documentation and test updates.
File Added: ntpath_full.patch
msg46042 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-03-13 22:08
Thanks! Committed as rev. 54364.
msg74158 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-10-02 11:56
I just saw this bug entry in the whatsnew list. The os.path.expanduser()
function handles ~user the wrong way. The (naive) implementation expects
that all user directories are inside a common base path. This is only
true for standalone Windows computers.

There is no way to get the path to a user's home directory w/o her
login+password or the login of a domain admin. The feature should be
removed in 2.6.1.

Martin, do you concur?
msg74191 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2008-10-02 19:34
> Martin, do you concur?

Not really. On my system, which is part of a domain, expanduser("~")
gives 'C:\\Documents and Settings\\martin.vonloewis', which is
indeed the directory where my profile lives. HOMEDRIVE is Y:,
and HOMESHARE is some system in our network, but those aren't
considered for determining ~ - although I do think
%HOMEDRIVE%\%HOMEPATH% might have been a better choice.
However, given the choice that was made, I think the assumption
is correct that somebody else's profile will live in the same
folder. Is it possible to configure a domain profile so that it's
not in Documents and Settings?

Even if this doesn't work for all systems, I don't think we can
revert it for 2.6.1 (as it does give the correct answer at least
in some cases). Such a change will have to wait for 2.7.
msg86842 - (view) Author: Geoffrey Bache (gjb1002) Date: 2009-04-30 12:56
Just ran into this myself, and would agree with Christian's comments. On
my system, my home directory is a mounted network drive, hence "H:\". It
was a bit of a surprise when os.path.expanduser("~fred") returned
"H:\\fred"...

This seems broken to me. It's surely better to have reliable functions
that either work or return the path unchanged, than ones that guess and
are wrong some of the time. At least in the above case it should be
possible to add a special case.

Will this be considered for Python 2.7 now? I'd suggest opening a new
bug or reopening this one if so.
msg86843 - (view) Author: Geoffrey Bache (gjb1002) Date: 2009-04-30 14:56
In fact, wouldn't a very simple fix be to not return paths that don't
exist? That would probably catch 90% of the cases.
History
Date User Action Args
2009-04-30 14:56:22gjb1002setmessages: + msg86843
2009-04-30 12:56:58gjb1002setnosy: + gjb1002
messages: + msg86842
2008-10-15 06:15:08loewissetstatus: open -> closed
assignee: loewis ->
resolution: accepted
priority: high -> normal
2008-10-02 19:34:20loewissetmessages: + msg74191
2008-10-02 11:56:48christian.heimessetstatus: closed -> open
priority: normal -> high
assignee: georg.brandl -> loewis
components: + Windows, - None
versions: + Python 2.6, Python 3.0
nosy: + christian.heimes, loewis
messages: + msg74158
resolution: accepted -> (no value)
2004-05-20 20:35:05josiahcarlsoncreate