This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Fix for bugs relating to ntpath.expanduser()
Type: Stage:
Components: Windows Versions: Python 3.0, Python 2.6
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 2022-04-11 14:56 by admin. This issue is now closed.

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) * (Python triager) 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) * (Python triager) Date: 2004-05-20 20:36
Logged In: YES 

#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')
>>> ntpath.expanduser('~billy\\bob')
>>> ntpath.expanduser('~\\bob')
'C:\\Documents and Settings\\jcarlson\\bob'

#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'
'C:Documents and
msg46033 - (view) Author: Josiah Carlson (josiahcarlson) * (Python triager) Date: 2004-05-20 20:38
Logged In: YES 

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 

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

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 

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

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

I've just attached an updated version of the patch to handle
expandusers (without recursive environment variable
expansion), as well as a variant of 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) * (Python triager) 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

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.
Date User Action Args
2022-04-11 14:56:04adminsetgithub: 40279
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