@@ -44,7 +44,8 @@ class BaseTests:
44
44
def writeTmp (self , content , * , mode = 'w' ): # opening in text mode is the default
45
45
fd , name = tempfile .mkstemp ()
46
46
self .addCleanup (os_helper .unlink , name )
47
- with open (fd , mode ) as f :
47
+ encoding = None if "b" in mode else "utf-8"
48
+ with open (fd , mode , encoding = encoding ) as f :
48
49
f .write (content )
49
50
return name
50
51
@@ -96,7 +97,7 @@ def test_buffer_sizes(self):
96
97
97
98
if verbose :
98
99
print ('1. Simple iteration' )
99
- fi = FileInput (files = (t1 , t2 , t3 , t4 ))
100
+ fi = FileInput (files = (t1 , t2 , t3 , t4 ), encoding = "utf-8" )
100
101
lines = list (fi )
101
102
fi .close ()
102
103
self .assertEqual (len (lines ), 31 )
@@ -107,7 +108,7 @@ def test_buffer_sizes(self):
107
108
108
109
if verbose :
109
110
print ('2. Status variables' )
110
- fi = FileInput (files = (t1 , t2 , t3 , t4 ))
111
+ fi = FileInput (files = (t1 , t2 , t3 , t4 ), encoding = "utf-8" )
111
112
s = "x"
112
113
while s and s != 'Line 6 of file 2\n ' :
113
114
s = fi .readline ()
@@ -126,7 +127,7 @@ def test_buffer_sizes(self):
126
127
127
128
if verbose :
128
129
print ('4. Stdin' )
129
- fi = FileInput (files = (t1 , t2 , t3 , t4 , '-' ))
130
+ fi = FileInput (files = (t1 , t2 , t3 , t4 , '-' ), encoding = "utf-8" )
130
131
savestdin = sys .stdin
131
132
try :
132
133
sys .stdin = StringIO ("Line 1 of stdin\n Line 2 of stdin\n " )
@@ -140,7 +141,7 @@ def test_buffer_sizes(self):
140
141
141
142
if verbose :
142
143
print ('5. Boundary conditions' )
143
- fi = FileInput (files = (t1 , t2 , t3 , t4 ))
144
+ fi = FileInput (files = (t1 , t2 , t3 , t4 ), encoding = "utf-8" )
144
145
self .assertEqual (fi .lineno (), 0 )
145
146
self .assertEqual (fi .filename (), None )
146
147
fi .nextfile ()
@@ -151,15 +152,15 @@ def test_buffer_sizes(self):
151
152
print ('6. Inplace' )
152
153
savestdout = sys .stdout
153
154
try :
154
- fi = FileInput (files = (t1 , t2 , t3 , t4 ), inplace = 1 )
155
+ fi = FileInput (files = (t1 , t2 , t3 , t4 ), inplace = 1 , encoding = "utf-8" )
155
156
for line in fi :
156
157
line = line [:- 1 ].upper ()
157
158
print (line )
158
159
fi .close ()
159
160
finally :
160
161
sys .stdout = savestdout
161
162
162
- fi = FileInput (files = (t1 , t2 , t3 , t4 ))
163
+ fi = FileInput (files = (t1 , t2 , t3 , t4 ), encoding = "utf-8" )
163
164
for line in fi :
164
165
self .assertEqual (line [- 1 ], '\n ' )
165
166
m = pat .match (line [:- 1 ])
@@ -182,7 +183,7 @@ def test_zero_byte_files(self):
182
183
t2 = self .writeTmp ("" )
183
184
t3 = self .writeTmp ("The only line there is.\n " )
184
185
t4 = self .writeTmp ("" )
185
- fi = FileInput (files = (t1 , t2 , t3 , t4 ))
186
+ fi = FileInput (files = (t1 , t2 , t3 , t4 ), encoding = "utf-8" )
186
187
187
188
line = fi .readline ()
188
189
self .assertEqual (line , 'The only line there is.\n ' )
@@ -200,7 +201,7 @@ def test_zero_byte_files(self):
200
201
def test_files_that_dont_end_with_newline (self ):
201
202
t1 = self .writeTmp ("A\n B\n C" )
202
203
t2 = self .writeTmp ("D\n E\n F" )
203
- fi = FileInput (files = (t1 , t2 ))
204
+ fi = FileInput (files = (t1 , t2 ), encoding = "utf-8" )
204
205
lines = list (fi )
205
206
self .assertEqual (lines , ["A\n " , "B\n " , "C" , "D\n " , "E\n " , "F" ])
206
207
self .assertEqual (fi .filelineno (), 3 )
@@ -213,14 +214,14 @@ def test_files_that_dont_end_with_newline(self):
213
214
## encoding = sys.getfilesystemencoding()
214
215
## if encoding is None:
215
216
## encoding = 'ascii'
216
- ## fi = FileInput(files=str(t1, encoding))
217
+ ## fi = FileInput(files=str(t1, encoding), encoding="utf-8" )
217
218
## lines = list(fi)
218
219
## self.assertEqual(lines, ["A\n", "B"])
219
220
220
221
def test_fileno (self ):
221
222
t1 = self .writeTmp ("A\n B" )
222
223
t2 = self .writeTmp ("C\n D" )
223
- fi = FileInput (files = (t1 , t2 ))
224
+ fi = FileInput (files = (t1 , t2 ), encoding = "utf-8" )
224
225
self .assertEqual (fi .fileno (), - 1 )
225
226
line = next (fi )
226
227
self .assertNotEqual (fi .fileno (), - 1 )
@@ -232,7 +233,7 @@ def test_fileno(self):
232
233
def test_opening_mode (self ):
233
234
try :
234
235
# invalid mode, should raise ValueError
235
- fi = FileInput (mode = "w" )
236
+ fi = FileInput (mode = "w" , encoding = "utf-8" )
236
237
self .fail ("FileInput should reject invalid mode argument" )
237
238
except ValueError :
238
239
pass
@@ -281,7 +282,7 @@ def __init__(self):
281
282
self .invoked = False
282
283
def __call__ (self , * args , ** kargs ):
283
284
self .invoked = True
284
- return open (* args )
285
+ return open (* args , encoding = "utf-8" )
285
286
286
287
t = self .writeTmp ("\n " )
287
288
custom_open_hook = CustomOpenHook ()
@@ -346,7 +347,7 @@ def old_hook(filename, mode):
346
347
def test_context_manager (self ):
347
348
t1 = self .writeTmp ("A\n B\n C" )
348
349
t2 = self .writeTmp ("D\n E\n F" )
349
- with FileInput (files = (t1 , t2 )) as fi :
350
+ with FileInput (files = (t1 , t2 ), encoding = "utf-8" ) as fi :
350
351
lines = list (fi )
351
352
self .assertEqual (lines , ["A\n " , "B\n " , "C" , "D\n " , "E\n " , "F" ])
352
353
self .assertEqual (fi .filelineno (), 3 )
@@ -356,21 +357,21 @@ def test_context_manager(self):
356
357
def test_close_on_exception (self ):
357
358
t1 = self .writeTmp ("" )
358
359
try :
359
- with FileInput (files = t1 ) as fi :
360
+ with FileInput (files = t1 , encoding = "utf-8" ) as fi :
360
361
raise OSError
361
362
except OSError :
362
363
self .assertEqual (fi ._files , ())
363
364
364
365
def test_empty_files_list_specified_to_constructor (self ):
365
- with FileInput (files = []) as fi :
366
+ with FileInput (files = [], encoding = "utf-8" ) as fi :
366
367
self .assertEqual (fi ._files , ('-' ,))
367
368
368
369
@warnings_helper .ignore_warnings (category = DeprecationWarning )
369
370
def test__getitem__ (self ):
370
371
"""Tests invoking FileInput.__getitem__() with the current
371
372
line number"""
372
373
t = self .writeTmp ("line1\n line2\n " )
373
- with FileInput (files = [t ]) as fi :
374
+ with FileInput (files = [t ], encoding = "utf-8" ) as fi :
374
375
retval1 = fi [0 ]
375
376
self .assertEqual (retval1 , "line1\n " )
376
377
retval2 = fi [1 ]
@@ -388,7 +389,7 @@ def test__getitem__invalid_key(self):
388
389
"""Tests invoking FileInput.__getitem__() with an index unequal to
389
390
the line number"""
390
391
t = self .writeTmp ("line1\n line2\n " )
391
- with FileInput (files = [t ]) as fi :
392
+ with FileInput (files = [t ], encoding = "utf-8" ) as fi :
392
393
with self .assertRaises (RuntimeError ) as cm :
393
394
fi [1 ]
394
395
self .assertEqual (cm .exception .args , ("accessing lines out of order" ,))
@@ -398,7 +399,7 @@ def test__getitem__eof(self):
398
399
"""Tests invoking FileInput.__getitem__() with the line number but at
399
400
end-of-input"""
400
401
t = self .writeTmp ('' )
401
- with FileInput (files = [t ]) as fi :
402
+ with FileInput (files = [t ], encoding = "utf-8" ) as fi :
402
403
with self .assertRaises (IndexError ) as cm :
403
404
fi [0 ]
404
405
self .assertEqual (cm .exception .args , ("end of input reached" ,))
@@ -413,7 +414,7 @@ def test_nextfile_oserror_deleting_backup(self):
413
414
try :
414
415
t = self .writeTmp ("\n " )
415
416
self .addCleanup (safe_unlink , t + '.bak' )
416
- with FileInput (files = [t ], inplace = True ) as fi :
417
+ with FileInput (files = [t ], inplace = True , encoding = "utf-8" ) as fi :
417
418
next (fi ) # make sure the file is opened
418
419
os .unlink = os_unlink_replacement
419
420
fi .nextfile ()
@@ -432,7 +433,7 @@ def test_readline_os_fstat_raises_OSError(self):
432
433
os_fstat_replacement = UnconditionallyRaise (OSError )
433
434
try :
434
435
t = self .writeTmp ("\n " )
435
- with FileInput (files = [t ], inplace = True ) as fi :
436
+ with FileInput (files = [t ], inplace = True , encoding = "utf-8" ) as fi :
436
437
os .fstat = os_fstat_replacement
437
438
fi .readline ()
438
439
finally :
@@ -450,7 +451,7 @@ def test_readline_os_chmod_raises_OSError(self):
450
451
os_chmod_replacement = UnconditionallyRaise (OSError )
451
452
try :
452
453
t = self .writeTmp ("\n " )
453
- with FileInput (files = [t ], inplace = True ) as fi :
454
+ with FileInput (files = [t ], inplace = True , encoding = "utf-8" ) as fi :
454
455
os .chmod = os_chmod_replacement
455
456
fi .readline ()
456
457
finally :
@@ -469,7 +470,7 @@ def fileno(self):
469
470
470
471
unconditionally_raise_ValueError = FilenoRaisesValueError ()
471
472
t = self .writeTmp ("\n " )
472
- with FileInput (files = [t ]) as fi :
473
+ with FileInput (files = [t ], encoding = "utf-8" ) as fi :
473
474
file_backup = fi ._file
474
475
try :
475
476
fi ._file = unconditionally_raise_ValueError
@@ -517,7 +518,7 @@ def test_iteration_buffering(self):
517
518
518
519
def test_pathlib_file (self ):
519
520
t1 = Path (self .writeTmp ("Pathlib file." ))
520
- with FileInput (t1 ) as fi :
521
+ with FileInput (t1 , encoding = "utf-8" ) as fi :
521
522
line = fi .readline ()
522
523
self .assertEqual (line , 'Pathlib file.' )
523
524
self .assertEqual (fi .lineno (), 1 )
@@ -526,11 +527,11 @@ def test_pathlib_file(self):
526
527
527
528
def test_pathlib_file_inplace (self ):
528
529
t1 = Path (self .writeTmp ('Pathlib file.' ))
529
- with FileInput (t1 , inplace = True ) as fi :
530
+ with FileInput (t1 , inplace = True , encoding = "utf-8" ) as fi :
530
531
line = fi .readline ()
531
532
self .assertEqual (line , 'Pathlib file.' )
532
533
print ('Modified %s' % line )
533
- with open (t1 ) as f :
534
+ with open (t1 , encoding = "utf-8" ) as f :
534
535
self .assertEqual (f .read (), 'Modified Pathlib file.\n ' )
535
536
536
537
0 commit comments