1
+ import contextlib
1
2
import multiprocessing
2
3
import os
3
4
import sys
4
5
import time
6
+ import warnings
5
7
from unittest import mock
6
8
7
9
import pytest
8
10
from py import error
9
11
from py .path import local
10
12
11
13
14
+ @contextlib .contextmanager
15
+ def ignore_encoding_warning ():
16
+ with warnings .catch_warnings ():
17
+ with contextlib .suppress (NameError ): # new in 3.10
18
+ warnings .simplefilter ("ignore" , EncodingWarning )
19
+ yield
20
+
21
+
12
22
class CommonFSTests :
13
23
def test_constructor_equality (self , path1 ):
14
24
p = path1 .__class__ (path1 )
@@ -223,7 +233,8 @@ def test_cmp(self, path1):
223
233
assert not (path1 < path1 )
224
234
225
235
def test_simple_read (self , path1 ):
226
- x = path1 .join ("samplefile" ).read ("r" )
236
+ with ignore_encoding_warning ():
237
+ x = path1 .join ("samplefile" ).read ("r" )
227
238
assert x == "samplefile\n "
228
239
229
240
def test_join_div_operator (self , path1 ):
@@ -265,12 +276,14 @@ def test_newext(self, path1):
265
276
266
277
def test_readlines (self , path1 ):
267
278
fn = path1 .join ("samplefile" )
268
- contents = fn .readlines ()
279
+ with ignore_encoding_warning ():
280
+ contents = fn .readlines ()
269
281
assert contents == ["samplefile\n " ]
270
282
271
283
def test_readlines_nocr (self , path1 ):
272
284
fn = path1 .join ("samplefile" )
273
- contents = fn .readlines (cr = 0 )
285
+ with ignore_encoding_warning ():
286
+ contents = fn .readlines (cr = 0 )
274
287
assert contents == ["samplefile" , "" ]
275
288
276
289
def test_file (self , path1 ):
@@ -362,8 +375,8 @@ def test_copy_file(self, path1):
362
375
initpy .copy (copied )
363
376
try :
364
377
assert copied .check ()
365
- s1 = initpy .read ( )
366
- s2 = copied .read ( )
378
+ s1 = initpy .read_text ( encoding = "utf-8" )
379
+ s2 = copied .read_text ( encoding = "utf-8" )
367
380
assert s1 == s2
368
381
finally :
369
382
if copied .check ():
@@ -376,8 +389,8 @@ def test_copy_dir(self, path1):
376
389
otherdir .copy (copied )
377
390
assert copied .check (dir = 1 )
378
391
assert copied .join ("__init__.py" ).check (file = 1 )
379
- s1 = otherdir .join ("__init__.py" ).read ( )
380
- s2 = copied .join ("__init__.py" ).read ( )
392
+ s1 = otherdir .join ("__init__.py" ).read_text ( encoding = "utf-8" )
393
+ s2 = copied .join ("__init__.py" ).read_text ( encoding = "utf-8" )
381
394
assert s1 == s2
382
395
finally :
383
396
if copied .check (dir = 1 ):
@@ -463,13 +476,13 @@ def setuptestfs(path):
463
476
return
464
477
# print "setting up test fs for", repr(path)
465
478
samplefile = path .ensure ("samplefile" )
466
- samplefile .write ("samplefile\n " )
479
+ samplefile .write_text ("samplefile\n " , encoding = "utf-8 " )
467
480
468
481
execfile = path .ensure ("execfile" )
469
- execfile .write ("x=42" )
482
+ execfile .write_text ("x=42" , encoding = "utf-8 " )
470
483
471
484
execfilepy = path .ensure ("execfile.py" )
472
- execfilepy .write ("x=42" )
485
+ execfilepy .write_text ("x=42" , encoding = "utf-8 " )
473
486
474
487
d = {1 : 2 , "hello" : "world" , "answer" : 42 }
475
488
path .ensure ("samplepickle" ).dump (d )
@@ -481,22 +494,24 @@ def setuptestfs(path):
481
494
otherdir .ensure ("__init__.py" )
482
495
483
496
module_a = otherdir .ensure ("a.py" )
484
- module_a .write ("from .b import stuff as result\n " )
497
+ module_a .write_text ("from .b import stuff as result\n " , encoding = "utf-8 " )
485
498
module_b = otherdir .ensure ("b.py" )
486
- module_b .write ('stuff="got it"\n ' )
499
+ module_b .write_text ('stuff="got it"\n ' , encoding = "utf-8" )
487
500
module_c = otherdir .ensure ("c.py" )
488
- module_c .write (
501
+ module_c .write_text (
489
502
"""import py;
490
503
import otherdir.a
491
504
value = otherdir.a.result
492
- """
505
+ """ ,
506
+ encoding = "utf-8" ,
493
507
)
494
508
module_d = otherdir .ensure ("d.py" )
495
- module_d .write (
509
+ module_d .write_text (
496
510
"""import py;
497
511
from otherdir import a
498
512
value2 = a.result
499
- """
513
+ """ ,
514
+ encoding = "utf-8" ,
500
515
)
501
516
502
517
@@ -534,9 +549,11 @@ def batch_make_numbered_dirs(rootdir, repeats):
534
549
for i in range (repeats ):
535
550
dir_ = local .make_numbered_dir (prefix = "repro-" , rootdir = rootdir )
536
551
file_ = dir_ .join ("foo" )
537
- file_ .write ("%s" % i )
538
- actual = int (file_ .read ())
539
- assert actual == i , f"int(file_.read()) is { actual } instead of { i } "
552
+ file_ .write_text ("%s" % i , encoding = "utf-8" )
553
+ actual = int (file_ .read_text (encoding = "utf-8" ))
554
+ assert (
555
+ actual == i
556
+ ), f"int(file_.read_text(encoding='utf-8')) is { actual } instead of { i } "
540
557
dir_ .join (".lock" ).remove (ignore_errors = True )
541
558
return True
542
559
@@ -692,14 +709,14 @@ def test_gt_with_strings(self, path1):
692
709
693
710
def test_open_and_ensure (self , path1 ):
694
711
p = path1 .join ("sub1" , "sub2" , "file" )
695
- with p .open ("w" , ensure = 1 ) as f :
712
+ with p .open ("w" , ensure = 1 , encoding = "utf-8" ) as f :
696
713
f .write ("hello" )
697
- assert p .read ( ) == "hello"
714
+ assert p .read_text ( encoding = "utf-8" ) == "hello"
698
715
699
716
def test_write_and_ensure (self , path1 ):
700
717
p = path1 .join ("sub1" , "sub2" , "file" )
701
- p .write ("hello" , ensure = 1 )
702
- assert p .read ( ) == "hello"
718
+ p .write_text ("hello" , ensure = 1 , encoding = "utf-8" )
719
+ assert p .read_text ( encoding = "utf-8" ) == "hello"
703
720
704
721
@pytest .mark .parametrize ("bin" , (False , True ))
705
722
def test_dump (self , tmpdir , bin ):
@@ -770,9 +787,9 @@ def test_ensure_filepath_withdir(self, tmpdir):
770
787
newfile = tmpdir .join ("test1" , "test" )
771
788
newfile .ensure ()
772
789
assert newfile .check (file = 1 )
773
- newfile .write ("42" )
790
+ newfile .write_text ("42" , encoding = "utf-8 " )
774
791
newfile .ensure ()
775
- s = newfile .read ( )
792
+ s = newfile .read_text ( encoding = "utf-8" )
776
793
assert s == "42"
777
794
778
795
def test_ensure_filepath_withoutdir (self , tmpdir ):
@@ -806,9 +823,9 @@ def test_long_filenames(self, tmpdir):
806
823
newfilename = "/test" * 60 # type:ignore[unreachable]
807
824
l1 = tmpdir .join (newfilename )
808
825
l1 .ensure (file = True )
809
- l1 .write ("foo" )
826
+ l1 .write_text ("foo" , encoding = "utf-8 " )
810
827
l2 = tmpdir .join (newfilename )
811
- assert l2 .read ( ) == "foo"
828
+ assert l2 .read_text ( encoding = "utf-8" ) == "foo"
812
829
813
830
def test_visit_depth_first (self , tmpdir ):
814
831
tmpdir .ensure ("a" , "1" )
@@ -1278,22 +1295,22 @@ class TestPOSIXLocalPath:
1278
1295
def test_hardlink (self , tmpdir ):
1279
1296
linkpath = tmpdir .join ("test" )
1280
1297
filepath = tmpdir .join ("file" )
1281
- filepath .write ("Hello" )
1298
+ filepath .write_text ("Hello" , encoding = "utf-8 " )
1282
1299
nlink = filepath .stat ().nlink
1283
1300
linkpath .mklinkto (filepath )
1284
1301
assert filepath .stat ().nlink == nlink + 1
1285
1302
1286
1303
def test_symlink_are_identical (self , tmpdir ):
1287
1304
filepath = tmpdir .join ("file" )
1288
- filepath .write ("Hello" )
1305
+ filepath .write_text ("Hello" , encoding = "utf-8 " )
1289
1306
linkpath = tmpdir .join ("test" )
1290
1307
linkpath .mksymlinkto (filepath )
1291
1308
assert linkpath .readlink () == str (filepath )
1292
1309
1293
1310
def test_symlink_isfile (self , tmpdir ):
1294
1311
linkpath = tmpdir .join ("test" )
1295
1312
filepath = tmpdir .join ("file" )
1296
- filepath .write ( " " )
1313
+ filepath .write_text ( "" , encoding = "utf-8 " )
1297
1314
linkpath .mksymlinkto (filepath )
1298
1315
assert linkpath .check (file = 1 )
1299
1316
assert not linkpath .check (link = 0 , file = 1 )
@@ -1302,10 +1319,12 @@ def test_symlink_isfile(self, tmpdir):
1302
1319
def test_symlink_relative (self , tmpdir ):
1303
1320
linkpath = tmpdir .join ("test" )
1304
1321
filepath = tmpdir .join ("file" )
1305
- filepath .write ("Hello" )
1322
+ filepath .write_text ("Hello" , encoding = "utf-8 " )
1306
1323
linkpath .mksymlinkto (filepath , absolute = False )
1307
1324
assert linkpath .readlink () == "file"
1308
- assert filepath .read () == linkpath .read ()
1325
+ assert filepath .read_text (encoding = "utf-8" ) == linkpath .read_text (
1326
+ encoding = "utf-8"
1327
+ )
1309
1328
1310
1329
def test_symlink_not_existing (self , tmpdir ):
1311
1330
linkpath = tmpdir .join ("testnotexisting" )
@@ -1338,7 +1357,7 @@ def test_symlink_remove(self, tmpdir):
1338
1357
def test_realpath_file (self , tmpdir ):
1339
1358
linkpath = tmpdir .join ("test" )
1340
1359
filepath = tmpdir .join ("file" )
1341
- filepath .write ( " " )
1360
+ filepath .write_text ( "" , encoding = "utf-8 " )
1342
1361
linkpath .mksymlinkto (filepath )
1343
1362
realpath = linkpath .realpath ()
1344
1363
assert realpath .basename == "file"
@@ -1383,7 +1402,7 @@ def test_atime(self, tmpdir):
1383
1402
atime1 = path .atime ()
1384
1403
# we could wait here but timer resolution is very
1385
1404
# system dependent
1386
- path .read ()
1405
+ path .read_binary ()
1387
1406
time .sleep (ATIME_RESOLUTION )
1388
1407
atime2 = path .atime ()
1389
1408
time .sleep (ATIME_RESOLUTION )
@@ -1467,7 +1486,7 @@ def test_copy_stat_dir(self, tmpdir):
1467
1486
test_files = ["a" , "b" , "c" ]
1468
1487
src = tmpdir .join ("src" )
1469
1488
for f in test_files :
1470
- src .join (f ).write (f , ensure = True )
1489
+ src .join (f ).write_text (f , ensure = True , encoding = "utf-8" )
1471
1490
dst = tmpdir .join ("dst" )
1472
1491
# a small delay before the copy
1473
1492
time .sleep (ATIME_RESOLUTION )
@@ -1521,10 +1540,11 @@ def test_listdir(self, tmpdir):
1521
1540
def test_read_write (self , tmpdir ):
1522
1541
x = tmpdir .join ("hello" )
1523
1542
part = "hällo"
1524
- x .write (part )
1525
- assert x .read () == part
1526
- x .write (part .encode (sys .getdefaultencoding ()))
1527
- assert x .read () == part .encode (sys .getdefaultencoding ())
1543
+ with ignore_encoding_warning ():
1544
+ x .write (part )
1545
+ assert x .read () == part
1546
+ x .write (part .encode (sys .getdefaultencoding ()))
1547
+ assert x .read () == part .encode (sys .getdefaultencoding ())
1528
1548
1529
1549
1530
1550
class TestBinaryAndTextMethods :
0 commit comments