diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2014-02-13 08:14:48 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2014-02-13 08:14:48 (GMT) |
commit | 7dfaa27fddb57ffcaf79bf21c49fd2a2cd741ab9 (patch) | |
tree | e5795f29cb7a2be5c2ed8d6b6bdab8091b231cdd /Lib/ntpath.py | |
parent | b58f053e48923cba2708410bd215371a7d1d5250 (diff) | |
parent | dbb101909d4bcc7cfe7a8063bb4ac4ec879ecac8 (diff) | |
download | cpython-7dfaa27fddb57ffcaf79bf21c49fd2a2cd741ab9.zip cpython-7dfaa27fddb57ffcaf79bf21c49fd2a2cd741ab9.tar.gz cpython-7dfaa27fddb57ffcaf79bf21c49fd2a2cd741ab9.tar.bz2 |
Issue #6815: os.path.expandvars() now supports non-ASCII environment
variables names and values.
Diffstat (limited to 'Lib/ntpath.py')
-rw-r--r-- | Lib/ntpath.py | 60 |
1 files changed, 31 insertions, 29 deletions
diff --git a/Lib/ntpath.py b/Lib/ntpath.py index de7a247..af3fb87 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -377,6 +377,7 @@ def expandvars(path): percent = b'%' brace = b'{' dollar = b'$' + environ = getattr(os, 'environb', None) else: if '$' not in path and '%' not in path: return path @@ -386,6 +387,7 @@ def expandvars(path): percent = '%' brace = '{' dollar = '$' + environ = os.environ res = path[:0] index = 0 pathlen = len(path) @@ -414,14 +416,13 @@ def expandvars(path): index = pathlen - 1 else: var = path[:index] - if isinstance(path, bytes): - var = var.decode('ascii') - if var in os.environ: - value = os.environ[var] - else: - value = '%' + var + '%' - if isinstance(path, bytes): - value = value.encode('ascii') + try: + if environ is None: + value = os.fsencode(os.environ[os.fsdecode(var)]) + else: + value = environ[var] + except KeyError: + value = percent + var + percent res += value elif c == dollar: # variable or '$$' if path[index + 1:index + 2] == dollar: @@ -435,39 +436,40 @@ def expandvars(path): index = path.index(b'}') else: index = path.index('}') - var = path[:index] - if isinstance(path, bytes): - var = var.decode('ascii') - if var in os.environ: - value = os.environ[var] - else: - value = '${' + var + '}' - if isinstance(path, bytes): - value = value.encode('ascii') - res += value except ValueError: if isinstance(path, bytes): res += b'${' + path else: res += '${' + path index = pathlen - 1 + else: + var = path[:index] + try: + if environ is None: + value = os.fsencode(os.environ[os.fsdecode(var)]) + else: + value = environ[var] + except KeyError: + if isinstance(path, bytes): + value = b'${' + var + b'}' + else: + value = '${' + var + '}' + res += value else: - var = '' + var = path[:0] index += 1 c = path[index:index + 1] while c and c in varchars: - if isinstance(path, bytes): - var += c.decode('ascii') - else: - var += c + var += c index += 1 c = path[index:index + 1] - if var in os.environ: - value = os.environ[var] - else: - value = '$' + var - if isinstance(path, bytes): - value = value.encode('ascii') + try: + if environ is None: + value = os.fsencode(os.environ[os.fsdecode(var)]) + else: + value = environ[var] + except KeyError: + value = dollar + var res += value if c: index -= 1 |