Issue #6815: os.path.expandvars() now supports non-ASCII environment

variables names and values.
This commit is contained in:
Serhiy Storchaka 2014-02-13 10:13:53 +02:00
parent 61e2493b83
commit dbb101909d
5 changed files with 96 additions and 42 deletions

View file

@ -22,13 +22,15 @@ def tester(fn, wantResult):
fn = fn.replace('["', '[b"')
fn = fn.replace(", '", ", b'")
fn = fn.replace(', "', ', b"')
fn = os.fsencode(fn).decode('latin1')
fn = fn.encode('ascii', 'backslashreplace').decode('ascii')
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
gotResult = eval(fn)
if isinstance(wantResult, str):
wantResult = wantResult.encode('ascii')
wantResult = os.fsencode(wantResult)
elif isinstance(wantResult, tuple):
wantResult = tuple(r.encode('ascii') for r in wantResult)
wantResult = tuple(os.fsencode(r) for r in wantResult)
gotResult = eval(fn)
if wantResult != gotResult:
@ -223,7 +225,6 @@ class TestNtpath(unittest.TestCase):
tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar")
tester('ntpath.expandvars("$bar bar")', "$bar bar")
tester('ntpath.expandvars("$?bar")', "$?bar")
tester('ntpath.expandvars("${foo}bar")', "barbar")
tester('ntpath.expandvars("$foo}bar")', "bar}bar")
tester('ntpath.expandvars("${foo")', "${foo")
tester('ntpath.expandvars("${{foo}}")', "baz1}")
@ -237,6 +238,26 @@ class TestNtpath(unittest.TestCase):
tester('ntpath.expandvars("%foo%%bar")', "bar%bar")
tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
@unittest.skipUnless(support.FS_NONASCII, 'need support.FS_NONASCII')
def test_expandvars_nonascii(self):
def check(value, expected):
tester('ntpath.expandvars(%r)' % value, expected)
with support.EnvironmentVarGuard() as env:
env.clear()
nonascii = support.FS_NONASCII
env['spam'] = nonascii
env[nonascii] = 'ham' + nonascii
check('$spam bar', '%s bar' % nonascii)
check('$%s bar' % nonascii, '$%s bar' % nonascii)
check('${spam}bar', '%sbar' % nonascii)
check('${%s}bar' % nonascii, 'ham%sbar' % nonascii)
check('$spam}bar', '%s}bar' % nonascii)
check('$%s}bar' % nonascii, '$%s}bar' % nonascii)
check('%spam% bar', '%s bar' % nonascii)
check('%{}% bar'.format(nonascii), 'ham%s bar' % nonascii)
check('%spam%bar', '%sbar' % nonascii)
check('%{}%bar'.format(nonascii), 'ham%sbar' % nonascii)
def test_abspath(self):
# ntpath.abspath() can only be used on a system with the "nt" module
# (reasonably), so we protect this test with "import nt". This allows