[ python-Bugs-796219 ] ntpath.expanduser() is still wrong

SourceForge.net noreply at sourceforge.net
Thu May 20 16:45:16 EDT 2004


Bugs item #796219, was opened at 2003-08-27 13:11
Message generated for change (Comment added) made by josiahcarlson
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=796219&group_id=5470

Category: Windows
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Guido van Rossum (gvanrossum)
Assigned to: Guido van Rossum (gvanrossum)
Summary: ntpath.expanduser() is still wrong

Initial Comment:
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.

----------------------------------------------------------------------

Comment By: Josiah Carlson (josiahcarlson)
Date: 2004-05-20 13:45

Message:
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.

----------------------------------------------------------------------

Comment By: Josiah Carlson (josiahcarlson)
Date: 2004-03-26 16:53

Message:
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.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2004-03-20 14:49

Message:
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?

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2003-09-30 07:00

Message:
Logged In: YES 
user_id=31435

Assigned back to Guido.

----------------------------------------------------------------------

Comment By: Josiah Carlson (josiahcarlson)
Date: 2003-09-29 23:33

Message:
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)


----------------------------------------------------------------------

Comment By: Christos Georgiou (tzot)
Date: 2003-08-30 02:55

Message:
Logged In: YES 
user_id=539787

I stand corrected; multiple backslashes inside a path are not 
merged into one on Windows.  Thank you.

----------------------------------------------------------------------

Comment By: Josiah Carlson (josiahcarlson)
Date: 2003-08-29 11:48

Message:
Logged In: YES 
user_id=341410

Sourceforge ate my double-backslashes.
All '\' should be '\\'.

----------------------------------------------------------------------

Comment By: Josiah Carlson (josiahcarlson)
Date: 2003-08-29 11:20

Message:
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.

----------------------------------------------------------------------

Comment By: Christos Georgiou (tzot)
Date: 2003-08-29 01:38

Message:
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.

----------------------------------------------------------------------

Comment By: Jarek Zgoda (zgoda)
Date: 2003-08-28 00:47

Message:
Logged In: YES 
user_id=92222

This is very common setting on Windows2000 Professional.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=796219&group_id=5470



More information about the Python-bugs-list mailing list