33"""
44
55import locale
6+ import subprocess
67import sys
78import textwrap
89import unittest
910from test import support
1011from test .support .script_helper import assert_python_ok , assert_python_failure
12+ from test .support import os_helper
1113
1214
1315MS_WINDOWS = (sys .platform == 'win32' )
@@ -171,7 +173,7 @@ def test_io(self):
171173 filename = __file__
172174
173175 out = self .get_output ('-c' , code , filename , PYTHONUTF8 = '1' )
174- self .assertEqual (out , 'UTF -8/strict' )
176+ self .assertEqual (out . lower () , 'utf -8/strict' )
175177
176178 def _check_io_encoding (self , module , encoding = None , errors = None ):
177179 filename = __file__
@@ -193,10 +195,10 @@ def _check_io_encoding(self, module, encoding=None, errors=None):
193195 PYTHONUTF8 = '1' )
194196
195197 if not encoding :
196- encoding = 'UTF -8'
198+ encoding = 'utf -8'
197199 if not errors :
198200 errors = 'strict'
199- self .assertEqual (out , f'{ encoding } /{ errors } ' )
201+ self .assertEqual (out . lower () , f'{ encoding } /{ errors } ' )
200202
201203 def check_io_encoding (self , module ):
202204 self ._check_io_encoding (module , encoding = "latin1" )
@@ -215,12 +217,12 @@ def test_pyio_encoding(self):
215217 def test_locale_getpreferredencoding (self ):
216218 code = 'import locale; print(locale.getpreferredencoding(False), locale.getpreferredencoding(True))'
217219 out = self .get_output ('-X' , 'utf8' , '-c' , code )
218- self .assertEqual (out , 'UTF -8 UTF -8' )
220+ self .assertEqual (out , 'utf -8 utf -8' )
219221
220222 for loc in POSIX_LOCALES :
221223 with self .subTest (LC_ALL = loc ):
222224 out = self .get_output ('-X' , 'utf8' , '-c' , code , LC_ALL = loc )
223- self .assertEqual (out , 'UTF -8 UTF -8' )
225+ self .assertEqual (out , 'utf -8 utf -8' )
224226
225227 @unittest .skipIf (MS_WINDOWS , 'test specific to Unix' )
226228 def test_cmd_line (self ):
@@ -268,6 +270,32 @@ def test_optim_level(self):
268270 out = self .get_output ('-X' , 'utf8' , '-E' , '-c' , code )
269271 self .assertEqual (out , '1' )
270272
273+ @unittest .skipIf (MS_WINDOWS ,
274+ "os.device_encoding() doesn't implement "
275+ "the UTF-8 Mode on Windows" )
276+ @support .requires_subprocess ()
277+ def test_device_encoding (self ):
278+ # Use stdout as TTY
279+ if not sys .stdout .isatty ():
280+ self .skipTest ("sys.stdout is not a TTY" )
281+
282+ filename = 'out.txt'
283+ self .addCleanup (os_helper .unlink , filename )
284+
285+ code = (f'import os, sys; fd = sys.stdout.fileno(); '
286+ f'out = open({ filename !r} , "w", encoding="utf-8"); '
287+ f'print(os.isatty(fd), os.device_encoding(fd), file=out); '
288+ f'out.close()' )
289+ cmd = [sys .executable , '-X' , 'utf8' , '-c' , code ]
290+ # The stdout TTY is inherited to the child process
291+ proc = subprocess .run (cmd , text = True )
292+ self .assertEqual (proc .returncode , 0 , proc )
293+
294+ # In UTF-8 Mode, device_encoding(fd) returns "UTF-8" if fd is a TTY
295+ with open (filename , encoding = "utf8" ) as fp :
296+ out = fp .read ().rstrip ()
297+ self .assertEqual (out , 'True utf-8' )
298+
271299
272300if __name__ == "__main__" :
273301 unittest .main ()
0 commit comments