From dbb101909d4bcc7cfe7a8063bb4ac4ec879ecac8 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 13 Feb 2014 10:13:53 +0200 Subject: Issue #6815: os.path.expandvars() now supports non-ASCII environment variables names and values. --- Lib/posixpath.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'Lib/posixpath.py') diff --git a/Lib/posixpath.py b/Lib/posixpath.py index b1e1a9255e6..9c11d8a32b6 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 -- cgit v1.2.3