@@ -22,13 +22,15 @@ def tester(fn, wantResult):
2222 fn = fn .replace ('["' , '[b"' )
2323 fn = fn .replace (", '" , ", b'" )
2424 fn = fn .replace (', "' , ', b"' )
25+ fn = os .fsencode (fn ).decode ('latin1' )
26+ fn = fn .encode ('ascii' , 'backslashreplace' ).decode ('ascii' )
2527 with warnings .catch_warnings ():
2628 warnings .simplefilter ("ignore" , DeprecationWarning )
2729 gotResult = eval (fn )
2830 if isinstance (wantResult , str ):
29- wantResult = wantResult . encode ( 'ascii' )
31+ wantResult = os . fsencode ( wantResult )
3032 elif isinstance (wantResult , tuple ):
31- wantResult = tuple (r . encode ( 'ascii' ) for r in wantResult )
33+ wantResult = tuple (os . fsencode ( r ) for r in wantResult )
3234
3335 gotResult = eval (fn )
3436 if wantResult != gotResult :
@@ -223,7 +225,6 @@ def test_expandvars(self):
223225 tester ('ntpath.expandvars("$[foo]bar")' , "$[foo]bar" )
224226 tester ('ntpath.expandvars("$bar bar")' , "$bar bar" )
225227 tester ('ntpath.expandvars("$?bar")' , "$?bar" )
226- tester ('ntpath.expandvars("${foo}bar")' , "barbar" )
227228 tester ('ntpath.expandvars("$foo}bar")' , "bar}bar" )
228229 tester ('ntpath.expandvars("${foo")' , "${foo" )
229230 tester ('ntpath.expandvars("${{foo}}")' , "baz1}" )
@@ -237,6 +238,26 @@ def test_expandvars(self):
237238 tester ('ntpath.expandvars("%foo%%bar")' , "bar%bar" )
238239 tester ('ntpath.expandvars("\' %foo%\' %bar")' , "\' %foo%\' %bar" )
239240
241+ @unittest .skipUnless (support .FS_NONASCII , 'need support.FS_NONASCII' )
242+ def test_expandvars_nonascii (self ):
243+ def check (value , expected ):
244+ tester ('ntpath.expandvars(%r)' % value , expected )
245+ with support .EnvironmentVarGuard () as env :
246+ env .clear ()
247+ nonascii = support .FS_NONASCII
248+ env ['spam' ] = nonascii
249+ env [nonascii ] = 'ham' + nonascii
250+ check ('$spam bar' , '%s bar' % nonascii )
251+ check ('$%s bar' % nonascii , '$%s bar' % nonascii )
252+ check ('${spam}bar' , '%sbar' % nonascii )
253+ check ('${%s}bar' % nonascii , 'ham%sbar' % nonascii )
254+ check ('$spam}bar' , '%s}bar' % nonascii )
255+ check ('$%s}bar' % nonascii , '$%s}bar' % nonascii )
256+ check ('%spam% bar' , '%s bar' % nonascii )
257+ check ('%{}% bar' .format (nonascii ), 'ham%s bar' % nonascii )
258+ check ('%spam%bar' , '%sbar' % nonascii )
259+ check ('%{}%bar' .format (nonascii ), 'ham%sbar' % nonascii )
260+
240261 def test_abspath (self ):
241262 # ntpath.abspath() can only be used on a system with the "nt" module
242263 # (reasonably), so we protect this test with "import nt". This allows
0 commit comments