mirror of
https://github.com/python/cpython.git
synced 2025-07-29 22:24:49 +00:00
Patch #957650: "%var%" environment variable references are now properly
expanded in ntpath.expandvars(), also "~user" home directory references are recognized and handled on Windows.
This commit is contained in:
parent
b6ae6aa8ac
commit
03b90d8cfd
4 changed files with 64 additions and 22 deletions
|
@ -58,18 +58,20 @@ Equivalent to \function{exists()} on platforms lacking
|
||||||
\end{funcdesc}
|
\end{funcdesc}
|
||||||
|
|
||||||
\begin{funcdesc}{expanduser}{path}
|
\begin{funcdesc}{expanduser}{path}
|
||||||
On \UNIX, return the argument with an initial component of \samp{\~} or
|
On \UNIX and Windows, return the argument with an initial component of
|
||||||
\samp{\~\var{user}} replaced by that \var{user}'s home directory.
|
\samp{\~} or \samp{\~\var{user}} replaced by that \var{user}'s home directory.
|
||||||
An initial \samp{\~} is replaced by the environment variable
|
|
||||||
|
On \UNIX, an initial \samp{\~} is replaced by the environment variable
|
||||||
\envvar{HOME} if it is set; otherwise the current user's home directory
|
\envvar{HOME} if it is set; otherwise the current user's home directory
|
||||||
is looked up in the password directory through the built-in module
|
is looked up in the password directory through the built-in module
|
||||||
\refmodule{pwd}\refbimodindex{pwd}.
|
\refmodule{pwd}\refbimodindex{pwd}.
|
||||||
An initial \samp{\~\var{user}} is looked up directly in the
|
An initial \samp{\~\var{user}} is looked up directly in the
|
||||||
password directory.
|
password directory.
|
||||||
|
|
||||||
On Windows, only \samp{\~} is supported; it is replaced by the
|
On Windows, \envvar{HOME} and \envvar{USERPROFILE} will be used if set,
|
||||||
environment variable \envvar{HOME} or by a combination of
|
otherwise a combination of \envvar{HOMEPATH} and \envvar{HOMEDRIVE} will be
|
||||||
\envvar{HOMEDRIVE} and \envvar{HOMEPATH}.
|
used. An initial \samp{\~\var{user}} is handled by stripping the last
|
||||||
|
directory component from the created user path derived above.
|
||||||
|
|
||||||
If the expansion fails or if the
|
If the expansion fails or if the
|
||||||
path does not begin with a tilde, the path is returned unchanged.
|
path does not begin with a tilde, the path is returned unchanged.
|
||||||
|
@ -81,6 +83,9 @@ of the form \samp{\$\var{name}} or \samp{\$\{\var{name}\}} are
|
||||||
replaced by the value of environment variable \var{name}. Malformed
|
replaced by the value of environment variable \var{name}. Malformed
|
||||||
variable names and references to non-existing variables are left
|
variable names and references to non-existing variables are left
|
||||||
unchanged.
|
unchanged.
|
||||||
|
|
||||||
|
On Windows, \samp{\%\var{name}\%} expansions are supported in addition to
|
||||||
|
\samp{\$\var{name}} and \samp{\$\{\var{name}\}}.
|
||||||
\end{funcdesc}
|
\end{funcdesc}
|
||||||
|
|
||||||
\begin{funcdesc}{getatime}{path}
|
\begin{funcdesc}{getatime}{path}
|
||||||
|
|
|
@ -278,9 +278,11 @@ def expanduser(path):
|
||||||
i, n = 1, len(path)
|
i, n = 1, len(path)
|
||||||
while i < n and path[i] not in '/\\':
|
while i < n and path[i] not in '/\\':
|
||||||
i = i + 1
|
i = i + 1
|
||||||
if i == 1:
|
|
||||||
if 'HOME' in os.environ:
|
if 'HOME' in os.environ:
|
||||||
userhome = os.environ['HOME']
|
userhome = os.environ['HOME']
|
||||||
|
elif 'USERPROFILE' in os.environ:
|
||||||
|
userhome = os.environ['USERPROFILE']
|
||||||
elif not 'HOMEPATH' in os.environ:
|
elif not 'HOMEPATH' in os.environ:
|
||||||
return path
|
return path
|
||||||
else:
|
else:
|
||||||
|
@ -289,25 +291,31 @@ def expanduser(path):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
drive = ''
|
drive = ''
|
||||||
userhome = join(drive, os.environ['HOMEPATH'])
|
userhome = join(drive, os.environ['HOMEPATH'])
|
||||||
else:
|
|
||||||
return path
|
if i != 1: #~user
|
||||||
|
userhome = join(dirname(userhome), path[1:i])
|
||||||
|
|
||||||
return userhome + path[i:]
|
return userhome + path[i:]
|
||||||
|
|
||||||
|
|
||||||
# Expand paths containing shell variable substitutions.
|
# Expand paths containing shell variable substitutions.
|
||||||
# The following rules apply:
|
# The following rules apply:
|
||||||
# - no expansion within single quotes
|
# - 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.
|
# - ${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 With COMMAND.COM you can use any characters in a variable name,
|
||||||
# XXX except '^|<>='.
|
# XXX except '^|<>='.
|
||||||
|
|
||||||
def expandvars(path):
|
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."""
|
Unknown variables are left unchanged."""
|
||||||
if '$' not in path:
|
if '$' not in path and '%' not in path:
|
||||||
return path
|
return path
|
||||||
import string
|
import string
|
||||||
varchars = string.ascii_letters + string.digits + '_-'
|
varchars = string.ascii_letters + string.digits + '_-'
|
||||||
|
@ -325,6 +333,24 @@ def expandvars(path):
|
||||||
except ValueError:
|
except ValueError:
|
||||||
res = res + path
|
res = res + path
|
||||||
index = pathlen - 1
|
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 '$$'
|
elif c == '$': # variable or '$$'
|
||||||
if path[index + 1:index + 2] == '$':
|
if path[index + 1:index + 2] == '$':
|
||||||
res = res + c
|
res = res + c
|
||||||
|
|
|
@ -134,6 +134,13 @@ try:
|
||||||
tester('ntpath.expandvars("${{foo}}")', "baz1}")
|
tester('ntpath.expandvars("${{foo}}")', "baz1}")
|
||||||
tester('ntpath.expandvars("$foo$foo")', "barbar")
|
tester('ntpath.expandvars("$foo$foo")', "barbar")
|
||||||
tester('ntpath.expandvars("$bar$bar")', "$bar$bar")
|
tester('ntpath.expandvars("$bar$bar")', "$bar$bar")
|
||||||
|
tester('ntpath.expandvars("%foo% bar")', "bar bar")
|
||||||
|
tester('ntpath.expandvars("%foo%bar")', "barbar")
|
||||||
|
tester('ntpath.expandvars("%foo%%foo%")', "barbar")
|
||||||
|
tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar")
|
||||||
|
tester('ntpath.expandvars("%?bar%")', "%?bar%")
|
||||||
|
tester('ntpath.expandvars("%foo%%bar")', "bar%bar")
|
||||||
|
tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
|
||||||
finally:
|
finally:
|
||||||
os.environ.clear()
|
os.environ.clear()
|
||||||
os.environ.update(oldenv)
|
os.environ.update(oldenv)
|
||||||
|
|
|
@ -170,6 +170,10 @@ Core and builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Patch #957650: "%var%" environment variable references are now properly
|
||||||
|
expanded in ntpath.expandvars(), also "~user" home directory references
|
||||||
|
are recognized and handled on Windows.
|
||||||
|
|
||||||
- Patch #1429539: pdb now correctly initializes the __main__ module for
|
- Patch #1429539: pdb now correctly initializes the __main__ module for
|
||||||
the debugged script, which means that imports from __main__ work
|
the debugged script, which means that imports from __main__ work
|
||||||
correctly now.
|
correctly now.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue