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

SourceForge.net noreply at sourceforge.net
Tue Sep 30 02:33:08 EDT 2003


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: Tim Peters (tim_one)
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: 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