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.

classification
Title: ntpath.expanduser() is still wrong
Type: Stage:
Components: Windows Versions: Python 2.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: georg.brandl, gvanrossum, josiahcarlson, tim.peters, tzot, zgoda
Priority: normal Keywords:

Created on 2003-08-27 20:11 by gvanrossum, last changed 2022-04-10 16:10 by admin. This issue is now closed.

Messages (12)
msg17990 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2003-08-27 20:11
I found a system with the following setup:

- os.getenv("HOME") returns "%USERPROFILE%"
- os.getenv("USERPROFILE") returns the home directory

Currently, ntpath.py doesn't expand ~ correctly in this
case.
The fix is pretty simple, I'll try to submit it if I
have time.
msg17991 - (view) Author: Jarek Zgoda (zgoda) Date: 2003-08-28 07:47
Logged In: YES 
user_id=92222

This is very common setting on Windows2000 Professional.
msg17992 - (view) Author: Χρήστος Γεωργίου (Christos Georgiou) (tzot) * Date: 2003-08-29 08:38
Logged In: YES 
user_id=539787

If expandvars worked for nt variable syntax too, then just 
before the expanduser final return, the following code would 
suffice, right?

max_recursion = 3
while '%' in userhome and max_recursion > 0:
    userhome = expandvars(userhome)
    max_recursion -= 1

ignoring the fact that path[1:] could contain variables too.

Shouldn't expandvars be made to work with %var% format 
too?  If yes, I'll offer code.
msg17993 - (view) Author: Josiah Carlson (josiahcarlson) * (Python triager) Date: 2003-08-29 18:20
Logged In: YES 
user_id=341410

The code you offered won't work correctly for all
environment variable returns.  An example that would kill
your code:
%SYSTEMROOT%\System32

def expandfull(var, rem=3):
    if not rem:
        return expandvars(var)
    a = expandvars(var)
    b = a.split('\\')
    c = []
    for i in b:
        if '%' in i:
            c.append(expandfull(i), rem-1)
        else:
            c.append(i)
    return '\\'.join(c)

The above would work properly for all environment variables.
msg17994 - (view) Author: Josiah Carlson (josiahcarlson) * (Python triager) Date: 2003-08-29 18:48
Logged In: YES 
user_id=341410

Sourceforge ate my double-backslashes.
All '\\' should be '\\\\'.
msg17995 - (view) Author: Χρήστος Γεωργίου (Christos Georgiou) (tzot) * Date: 2003-08-30 09:55
Logged In: YES 
user_id=539787

I stand corrected; multiple backslashes inside a path are not 
merged into one on Windows.  Thank you.
msg17996 - (view) Author: Josiah Carlson (josiahcarlson) * (Python triager) Date: 2003-09-30 06:33
Logged In: YES 
user_id=341410

I just noticed that I've got some forward slashes in various
paths on my windows machines...here's some updated code:

    def expandfull(var, rem=3):
        if not rem:
            return os.path.expandvars(var)
        a = os.path.expandvars(var)
        b = []
        d = [b.extend(i.split('\\')) for i in a.split('/')]
        c = []
        for i in b:
            if '%' in i:
                c.append(expandfull(i), rem-1)
            else:
                c.append(i)
        return '\\'.join(c)
msg17997 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2003-09-30 14:00
Logged In: YES 
user_id=31435

Assigned back to Guido.
msg17998 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2004-03-20 22:49
Logged In: YES 
user_id=6380

Unclear what I'm asked to do here. Josiah, could you produce
an actual patch against CVS rather than random example code?

If you have forward slashes, you should use
os.path.normpath(). Why doesn't that work?
msg17999 - (view) Author: Josiah Carlson (josiahcarlson) * (Python triager) Date: 2004-03-27 00:53
Logged In: YES 
user_id=341410

After doing some more playing around, I discovered a few
other examples that would kill either method described below:
HOME=%USERDRIVE%%USERPATH%
HOME=C:\%userprofiles\%USERNAME%

The real trick is that % can exist in a path on windows, so
even c:\%HOME% is a vaild path:
 Directory of D:\test

03/26/2004  03:58p      <DIR>          .
03/26/2004  03:58p      <DIR>          ..
03/26/2004  03:59p      <DIR>          %HOME%
               0 File(s)              0 bytes
               3 Dir(s)   5,355,511,808 bytes free

D:\test>

I suppose the question remains as to what cases do we want
to cover.  If we assume that there aren't any % symbols in a
path, then the code is straightforward, and I can have a
patch for you in a few minutes.  If % can be in a path, then
the problem is a pain, and a miniature parser needs to be
written to deal with it.
msg18000 - (view) Author: Josiah Carlson (josiahcarlson) * (Python triager) Date: 2004-05-20 20:45
Logged In: YES 
user_id=341410

I have submitted sf patch #957650 to fix the bug listed.

There is still an issue when there actually exists folders
with names that mirror environment variables, but regardless
of whether we stat the filesystem, the meaning of such a
thing is ambiguous.

The patch also includes ~user\subpath functionality, which
has been missing in ntpath.
msg18001 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-03-13 22:08
This is finally fixed with patch #957650.
History
Date User Action Args
2022-04-10 16:10:53adminsetgithub: 39140
2003-08-27 20:11:14gvanrossumcreate