diff options
author | Georg Brandl <georg@python.org> | 2007-03-13 22:07:36 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2007-03-13 22:07:36 (GMT) |
commit | 03b90d8cfde0e74ec633f79201b53aada21bf42c (patch) | |
tree | f546d8e0e2a3f48a588ca1473df901562dc59da2 /Lib/ntpath.py | |
parent | b6ae6aa8ac8abb9350f2ae2186d609116676c661 (diff) | |
download | cpython-03b90d8cfde0e74ec633f79201b53aada21bf42c.zip cpython-03b90d8cfde0e74ec633f79201b53aada21bf42c.tar.gz cpython-03b90d8cfde0e74ec633f79201b53aada21bf42c.tar.bz2 |
Patch #957650: "%var%" environment variable references are now properly
expanded in ntpath.expandvars(), also "~user" home directory references
are recognized and handled on Windows.
Diffstat (limited to 'Lib/ntpath.py')
-rw-r--r-- | Lib/ntpath.py | 58 |
1 files changed, 42 insertions, 16 deletions
diff --git a/Lib/ntpath.py b/Lib/ntpath.py index 2ea3358..60c1fd0 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -278,36 +278,44 @@ def expanduser(path): i, n = 1, len(path) while i < n and path[i] not in '/\\': i = i + 1 - if i == 1: - if 'HOME' in os.environ: - userhome = os.environ['HOME'] - elif not 'HOMEPATH' in os.environ: - return path - else: - try: - drive = os.environ['HOMEDRIVE'] - except KeyError: - drive = '' - userhome = join(drive, os.environ['HOMEPATH']) - else: + + if 'HOME' in os.environ: + userhome = os.environ['HOME'] + elif 'USERPROFILE' in os.environ: + userhome = os.environ['USERPROFILE'] + elif not 'HOMEPATH' in os.environ: return path + else: + try: + drive = os.environ['HOMEDRIVE'] + except KeyError: + drive = '' + userhome = join(drive, os.environ['HOMEPATH']) + + if i != 1: #~user + userhome = join(dirname(userhome), path[1:i]) + return userhome + path[i:] # Expand paths containing shell variable substitutions. # The following rules apply: # - no expansion within single quotes -# - no escape character, except for '$$' which is translated into '$' +# - '$$' is translated into '$' +# - '%%' is translated into '%' if '%%' are not seen in %var1%%var2% # - ${varname} is accepted. -# - varnames can be made out of letters, digits and the character '_' +# - $varname is accepted. +# - %varname% is accepted. +# - varnames can be made out of letters, digits and the characters '_-' +# (though is not verifed in the ${varname} and %varname% cases) # XXX With COMMAND.COM you can use any characters in a variable name, # XXX except '^|<>='. def expandvars(path): - """Expand shell variables of form $var and ${var}. + """Expand shell variables of the forms $var, ${var} and %var%. Unknown variables are left unchanged.""" - if '$' not in path: + if '$' not in path and '%' not in path: return path import string varchars = string.ascii_letters + string.digits + '_-' @@ -325,6 +333,24 @@ def expandvars(path): except ValueError: res = res + path index = pathlen - 1 + elif c == '%': # variable or '%' + if path[index + 1:index + 2] == '%': + res = res + c + index = index + 1 + else: + path = path[index+1:] + pathlen = len(path) + try: + index = path.index('%') + except ValueError: + res = res + '%' + path + index = pathlen - 1 + else: + var = path[:index] + if var in os.environ: + res = res + os.environ[var] + else: + res = res + '%' + var + '%' elif c == '$': # variable or '$$' if path[index + 1:index + 2] == '$': res = res + c |