mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Issue #6815: os.path.expandvars() now supports non-ASCII environment
variables names and values.
This commit is contained in:
parent
61e2493b83
commit
dbb101909d
5 changed files with 96 additions and 42 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue