diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2014-02-19 21:27:37 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2014-02-19 21:27:37 (GMT) |
commit | 2ac9d3110898a1cfc779dd436f05cd6ac231cbb3 (patch) | |
tree | b62d200630b8675414e833a7f1200cd424cf7ebe /Lib/ntpath.py | |
parent | c77d4ba85b4d2f08b4de094bc9d9104113772374 (diff) | |
download | cpython-2ac9d3110898a1cfc779dd436f05cd6ac231cbb3.zip cpython-2ac9d3110898a1cfc779dd436f05cd6ac231cbb3.tar.gz cpython-2ac9d3110898a1cfc779dd436f05cd6ac231cbb3.tar.bz2 |
Issue #6815: os.path.expandvars() now supports non-ASCII Unicode environment
variables names and values.
Diffstat (limited to 'Lib/ntpath.py')
-rw-r--r-- | Lib/ntpath.py | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/Lib/ntpath.py b/Lib/ntpath.py index fc9463c..42469fe 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -294,6 +294,13 @@ def expandvars(path): return path import string varchars = string.ascii_letters + string.digits + '_-' + if isinstance(path, unicode): + encoding = sys.getfilesystemencoding() + def getenv(var): + return os.environ[var.encode(encoding)].decode(encoding) + else: + def getenv(var): + return os.environ[var] res = '' index = 0 pathlen = len(path) @@ -322,9 +329,9 @@ def expandvars(path): index = pathlen - 1 else: var = path[:index] - if var in os.environ: - res = res + os.environ[var] - else: + try: + res = res + getenv(var) + except KeyError: res = res + '%' + var + '%' elif c == '$': # variable or '$$' if path[index + 1:index + 2] == '$': @@ -336,9 +343,9 @@ def expandvars(path): try: index = path.index('}') var = path[:index] - if var in os.environ: - res = res + os.environ[var] - else: + try: + res = res + getenv(var) + except KeyError: res = res + '${' + var + '}' except ValueError: res = res + '${' + path @@ -351,9 +358,9 @@ def expandvars(path): var = var + c index = index + 1 c = path[index:index + 1] - if var in os.environ: - res = res + os.environ[var] - else: + try: + res = res + getenv(var) + except KeyError: res = res + '$' + var if c != '': index = index - 1 |