summaryrefslogtreecommitdiffstats
path: root/Lib/ntpath.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-02-13 08:13:53 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-02-13 08:13:53 (GMT)
commitdbb101909d4bcc7cfe7a8063bb4ac4ec879ecac8 (patch)
treed4c26e5ec3c4b507d0694b0f65618c6b05b3f182 /Lib/ntpath.py
parent61e2493b8341be74928872ce6d7fb3a350bd1697 (diff)
downloadcpython-dbb101909d4bcc7cfe7a8063bb4ac4ec879ecac8.zip
cpython-dbb101909d4bcc7cfe7a8063bb4ac4ec879ecac8.tar.gz
cpython-dbb101909d4bcc7cfe7a8063bb4ac4ec879ecac8.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.py60
1 files changed, 31 insertions, 29 deletions
diff --git a/Lib/ntpath.py b/Lib/ntpath.py
index 5a012bd..303e586 100644
--- a/Lib/ntpath.py
+++ b/Lib/ntpath.py
@@ -362,6 +362,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
@@ -371,6 +372,7 @@ def expandvars(path):
percent = '%'
brace = '{'
dollar = '$'
+ environ = os.environ
res = path[:0]
index = 0
pathlen = len(path)
@@ -399,14 +401,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:
@@ -420,39 +421,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