3
3
"""
4
4
5
5
import locale
6
+ import subprocess
6
7
import sys
7
8
import textwrap
8
9
import unittest
9
10
from test import support
10
11
from test .support .script_helper import assert_python_ok , assert_python_failure
12
+ from test .support import os_helper
11
13
12
14
13
15
MS_WINDOWS = (sys .platform == 'win32' )
@@ -159,8 +161,6 @@ def test_stdio(self):
159
161
'stdout: utf-8/namereplace' ,
160
162
'stderr: utf-8/backslashreplace' ])
161
163
162
- # TODO: RUSTPYTHON
163
- @unittest .expectedFailure
164
164
def test_io (self ):
165
165
code = textwrap .dedent ('''
166
166
import sys
@@ -171,7 +171,7 @@ def test_io(self):
171
171
filename = __file__
172
172
173
173
out = self .get_output ('-c' , code , filename , PYTHONUTF8 = '1' )
174
- self .assertEqual (out , 'UTF -8/strict' )
174
+ self .assertEqual (out . lower () , 'utf -8/strict' )
175
175
176
176
def _check_io_encoding (self , module , encoding = None , errors = None ):
177
177
filename = __file__
@@ -193,19 +193,17 @@ def _check_io_encoding(self, module, encoding=None, errors=None):
193
193
PYTHONUTF8 = '1' )
194
194
195
195
if not encoding :
196
- encoding = 'UTF -8'
196
+ encoding = 'utf -8'
197
197
if not errors :
198
198
errors = 'strict'
199
- self .assertEqual (out , f'{ encoding } /{ errors } ' )
199
+ self .assertEqual (out . lower () , f'{ encoding } /{ errors } ' )
200
200
201
201
def check_io_encoding (self , module ):
202
202
self ._check_io_encoding (module , encoding = "latin1" )
203
203
self ._check_io_encoding (module , errors = "namereplace" )
204
204
self ._check_io_encoding (module ,
205
205
encoding = "latin1" , errors = "namereplace" )
206
206
207
- # TODO: RUSTPYTHON
208
- @unittest .expectedFailure
209
207
def test_io_encoding (self ):
210
208
self .check_io_encoding ('io' )
211
209
@@ -215,12 +213,12 @@ def test_pyio_encoding(self):
215
213
def test_locale_getpreferredencoding (self ):
216
214
code = 'import locale; print(locale.getpreferredencoding(False), locale.getpreferredencoding(True))'
217
215
out = self .get_output ('-X' , 'utf8' , '-c' , code )
218
- self .assertEqual (out , 'UTF -8 UTF -8' )
216
+ self .assertEqual (out , 'utf -8 utf -8' )
219
217
220
218
for loc in POSIX_LOCALES :
221
219
with self .subTest (LC_ALL = loc ):
222
220
out = self .get_output ('-X' , 'utf8' , '-c' , code , LC_ALL = loc )
223
- self .assertEqual (out , 'UTF -8 UTF -8' )
221
+ self .assertEqual (out , 'utf -8 utf -8' )
224
222
225
223
@unittest .skipIf (MS_WINDOWS , 'test specific to Unix' )
226
224
def test_cmd_line (self ):
@@ -268,6 +266,34 @@ def test_optim_level(self):
268
266
out = self .get_output ('-X' , 'utf8' , '-E' , '-c' , code )
269
267
self .assertEqual (out , '1' )
270
268
269
+ # TODO: RUSTPYTHON
270
+ @unittest .expectedFailure
271
+ @unittest .skipIf (MS_WINDOWS ,
272
+ "os.device_encoding() doesn't implement "
273
+ "the UTF-8 Mode on Windows" )
274
+ @support .requires_subprocess ()
275
+ def test_device_encoding (self ):
276
+ # Use stdout as TTY
277
+ if not sys .stdout .isatty ():
278
+ self .skipTest ("sys.stdout is not a TTY" )
279
+
280
+ filename = 'out.txt'
281
+ self .addCleanup (os_helper .unlink , filename )
282
+
283
+ code = (f'import os, sys; fd = sys.stdout.fileno(); '
284
+ f'out = open({ filename !r} , "w", encoding="utf-8"); '
285
+ f'print(os.isatty(fd), os.device_encoding(fd), file=out); '
286
+ f'out.close()' )
287
+ cmd = [sys .executable , '-X' , 'utf8' , '-c' , code ]
288
+ # The stdout TTY is inherited to the child process
289
+ proc = subprocess .run (cmd , text = True )
290
+ self .assertEqual (proc .returncode , 0 , proc )
291
+
292
+ # In UTF-8 Mode, device_encoding(fd) returns "UTF-8" if fd is a TTY
293
+ with open (filename , encoding = "utf8" ) as fp :
294
+ out = fp .read ().rstrip ()
295
+ self .assertEqual (out , 'True utf-8' )
296
+
271
297
272
298
if __name__ == "__main__" :
273
299
unittest .main ()
0 commit comments