diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2014-02-13 10:13:53 +0200 |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2014-02-13 10:13:53 +0200 |
commit | dbb101909d4bcc7cfe7a8063bb4ac4ec879ecac8 (patch) | |
tree | d4c26e5ec3c4b507d0694b0f65618c6b05b3f182 /Lib/posixpath.py | |
parent | 61e2493b8341be74928872ce6d7fb3a350bd1697 (diff) | |
download | cpython-git-dbb101909d4bcc7cfe7a8063bb4ac4ec879ecac8.tar.gz |
Issue #6815: os.path.expandvars() now supports non-ASCII environment
variables names and values.
Diffstat (limited to 'Lib/posixpath.py')
-rw-r--r-- | Lib/posixpath.py | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/Lib/posixpath.py b/Lib/posixpath.py index b1e1a9255e..9c11d8a32b 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -300,6 +300,7 @@ def expandvars(path): search = _varprogb.search start = b'{' end = b'}' + environ = getattr(os, 'environb', None) else: if '$' not in path: return path @@ -309,6 +310,7 @@ def expandvars(path): search = _varprog.search start = '{' end = '}' + environ = os.environ i = 0 while True: m = search(path, i) @@ -318,18 +320,18 @@ def expandvars(path): name = m.group(1) if name.startswith(start) and name.endswith(end): name = name[1:-1] - if isinstance(name, bytes): - name = str(name, 'ASCII') - if name in os.environ: + try: + if environ is None: + value = os.fsencode(os.environ[os.fsdecode(var)]) + else: + value = environ[name] + except KeyError: + i = j + else: tail = path[j:] - value = os.environ[name] - if isinstance(path, bytes): - value = value.encode('ASCII') path = path[:i] + value i = len(path) path += tail - else: - i = j return path |