-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathtest_file.py
828 lines (677 loc) · 33.4 KB
/
test_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
# Licensed to the .NET Foundation under one or more agreements.
# The .NET Foundation licenses this file to you under the Apache 2.0 License.
# See the LICENSE file in the project root for more information.
import os
import sys
import unittest
import thread
CP16623_LOCK = thread.allocate_lock()
from iptest import IronPythonTestCase, is_cli, is_mono, is_netcoreapp21, is_cpython, is_posix, run_test
class FileTest(IronPythonTestCase):
def setUp(self):
super(FileTest, self).setUp()
self.temp_file = os.path.join(self.temporary_dir, "temp.dat")
self.rng_seed = 0
# The set of read modes we wish to test. Each tuple consists of a human readable
# name for the mode followed by the corresponding mode string that will be
# passed to the file constructor.
self.read_modes = (("binary", "rb"), ("text", "r"), ("universal", "rU"))
# Same deal as above but for write modes. Note that writing doesn't support a
# universal newline mode.
self.write_modes = (("binary", "wb"), ("text", "w"))
# This module tests operations on the builtin file object. It is not yet complete, the tests cover read(),
# read(size), readline() and write() for binary, text and universal newline modes.
def test_sanity(self):
for i in range(5):
### general file robustness tests
f = file("onlyread.tmp", "w")
f.write("will only be read")
f.flush()
f.close()
sin = file("onlyread.tmp", "r")
sout = file("onlywrite.tmp", "w")
# writer is null for sin
self.assertRaises(IOError, sin.write, "abc")
self.assertRaises(IOError, sin.writelines, ["abc","def"])
# reader is null for sout
if is_cli:
self.assertRaises(IOError, sout.read)
self.assertRaises(IOError, sout.read, 10)
self.assertRaises(IOError, sout.readline)
self.assertRaises(IOError, sout.readline, 10)
self.assertRaises(IOError, sout.readlines)
self.assertRaises(IOError, sout.readlines, 10)
sin.close()
sout.close()
# now close a file and try to perform other I/O operations on it...
# should throw ValueError according to docs
f = file("onlywrite.tmp", "w")
f.close()
f.close()
self.assertRaises(ValueError, f.__iter__)
self.assertRaises(ValueError, f.flush)
self.assertRaises(ValueError, f.fileno)
self.assertRaises(ValueError, f.next)
self.assertRaises(ValueError, f.read)
self.assertRaises(ValueError, f.read, 10)
self.assertRaises(ValueError, f.readline)
self.assertRaises(ValueError, f.readline, 10)
self.assertRaises(ValueError, f.readlines)
self.assertRaises(ValueError, f.readlines, 10)
self.assertRaises(ValueError, f.seek, 10)
self.assertRaises(ValueError, f.seek, 10, 10)
self.assertRaises(ValueError, f.write, "abc")
self.assertRaises(ValueError, f.writelines, ["abc","def"])
os.unlink("onlyread.tmp")
os.unlink("onlywrite.tmp")
# Test binary reading and writing fidelity using a round trip method. First
# construct some pseudo random binary data in a string (making it long enough
# that it's likely we'd show up any problems with the data being passed through
# a character encoding/decoding scheme). Then write this data to disk (in binary
# mode), read it back again (in binary) and check that no changes have occured.
# Construct the binary data. We want the test to be repeatable so seed the
# random number generator with a fixed value. Use a simple linear congruential
# method to generate the random byte values.
def test_read_write_fidelity(self):
def randbyte():
self.rng_seed = (1664525 * self.rng_seed) + 1013904223
return (self.rng_seed >> 8) & 0xff
data = ""
for i in range(10 * 1024):
data += chr(randbyte())
# Keep a copy of the data safe.
orig_data = data
# Write the data to disk in binary mode.
with file(self.temp_file, "wb") as f:
f.write(data)
# And read it back in again.
with file(self.temp_file, "rb") as f:
data = f.read()
# Check nothing changed.
self.assertTrue(data == orig_data)
def test_cp10983(self):
# writing non-unicode characters > 127 should be preserved
with open(self.temp_file, 'w') as x:
x.write('\xa33')
with open(self.temp_file) as x:
data = x.read()
self.assertEqual(ord(data[0]), 163)
self.assertEqual(ord(data[1]), 51)
with open(self.temp_file, 'w') as x:
x.write("a2\xa33\u0163\x0F\x0FF\t\\\x0FF\x0FE\x00\x01\x7F\x7E\x80")
with open(self.temp_file) as x:
data = x.read()
self.assertEqual(data, 'a2\xa33\\u0163\x0f\x0fF\t\\\x0fF\x0fE\x00\x01\x7F\x7E\x80')
@unittest.skipUnless(is_cli, 'IronPython specific test')
def test_cp27179(self):
# file.write() accepting Array[Byte]
from System import Array, Byte
data_string = 'abcdef\nghijkl\n\n'
data = Array[Byte](map(Byte, map(ord, data_string)))
with open(self.temp_file, 'w+') as f:
f.write(data)
with open(self.temp_file, 'r') as f:
data_read = f.read()
self.assertEqual(data_string, data_read)
# Helper used to format newline characters into a visible format.
def format_newlines(self, string):
out = ""
for char in string:
if char == '\r':
out += "\\r"
elif char == '\n':
out += "\\n"
else:
out += char
return out
# The following is the setup for a set of pattern mode tests that will check
# some tricky edge cases for newline translation for both reading and writing.
# The entry point is the test_patterns() function.
@unittest.skipIf(is_posix, "posix doesn't do any newline translation if b is specified, so this test is invalid")
def test_newlines(self):
# Read mode test cases. Each tuple has three values; the raw on-disk value we
# start with (which also doubles as the value we should get back when we read in
# binary mode) then the value we expect to get when reading in text mode and
# finally the value we expect to get in universal newline mode.
read_patterns = (("\r", "\r", "\n"),
("\n", "\n", "\n"),
("\r\n", "\n", "\n"),
("\n\r", "\n\r", "\n\n"),
("\r\r", "\r\r", "\n\n"),
("\n\n", "\n\n", "\n\n"),
("\r\n\r\n", "\n\n", "\n\n"),
("\n\r\n\r", "\n\n\r", "\n\n\n"),
("The quick brown fox", "The quick brown fox", "The quick brown fox"),
("The \rquick\n brown fox\r\n", "The \rquick\n brown fox\n", "The \nquick\n brown fox\n"),
("The \r\rquick\r\n\r\n brown fox", "The \r\rquick\n\n brown fox", "The \n\nquick\n\n brown fox"))
# Write mode test cases. Same deal as above but with one less member in each
# tuple due to the lack of a universal newline write mode. The first value
# represents the in-memory value we start with (and expect to write in binary
# write mode) and the next value indicates the value we expect to end up on disk
# in text mode.
write_patterns = (("\r", "\r"),
("\n", "\r\n"),
("\r\n", "\r\r\n"),
("\n\r", "\r\n\r"),
("\r\r", "\r\r"),
("\n\n", "\r\n\r\n"),
("\r\n\r\n", "\r\r\n\r\r\n"),
("\n\r\n\r", "\r\n\r\r\n\r"),
("The quick brown fox", "The quick brown fox"),
("The \rquick\n brown fox\r\n", "The \rquick\r\n brown fox\r\r\n"),
("The \r\rquick\r\n\r\n brown fox", "The \r\rquick\r\r\n\r\r\n brown fox"))
# Test a specific read mode pattern.
def test_read_pattern(pattern):
# Write the initial data to disk using binary mode (we test this
# functionality earlier so we're satisfied it gets there unaltered).
with file(self.temp_file, "wb") as f:
f.write(pattern[0])
# Read the data back in each read mode, checking that we get the correct
# transform each time.
for mode in range(3):
test_read_mode(pattern, mode)
# Test a specific read mode pattern for a given reading mode.
def test_read_mode(pattern, mode):
# Read the data back from disk using the given read mode.
with file(self.temp_file, self.read_modes[mode][1]) as f:
contents = f.read()
# Check it equals what we expected for this mode.
self.assertTrue(contents == pattern[mode])
# Test a specific write mode pattern.
def test_write_pattern(pattern):
for mode in range(2):
test_write_mode(pattern, mode)
# Test a specific write mode pattern for a given write mode.
def test_write_mode(pattern, mode):
# Write the raw data using the given mode.
with file(self.temp_file, self.write_modes[mode][1]) as f:
f.write(pattern[0])
# Read the data back in using binary mode (we tested this gets us back
# unaltered data earlier).
with file(self.temp_file, "rb") as f:
contents = f.read()
# Check it equals what we expected for this mode.
self.assertTrue(contents == pattern[mode])
# Run through the read and write mode tests for all patterns.
def test_patterns():
for pattern in read_patterns:
test_read_pattern(pattern)
for pattern in write_patterns:
test_write_pattern(pattern)
# Actually run the pattern mode tests.
test_patterns()
# Now some tests of read(size).
# Test data is in the following format: ("raw data", read_size, (binary mode result strings) (binary mode result tell() result)
# (text mode result strings) (text mode result tell() result)
# (universal mode result strings) (universal mode result tell() results)
@unittest.skipUnless(is_cli, 'IronPython specific test')
def test_read_size(self):
read_size_tests = [("Hello", 1, ("H", "e", "l", "l", "o"), (1,2,3,4,5),
("H", "e", "l", "l", "o"), (1,2,3,4,5),
("H", "e", "l", "l", "o"), (1,2,3,4,5)),
("Hello", 2, ("He", "ll", "o"), (2,4,5),
("He", "ll", "o"), (2,4,5),
("He", "ll", "o"), (2,4,5))]
if is_posix:
read_size_tests.append(("H\re\n\r\nllo", 1, ("H", "\r", "e", "\n", "\r", "\n", "l", "l", "o"), (1,2,3,4,5,6,7, 8, 9),
("H", "\r", "e", "\n", "\r", "\n", "l", "l", "o"), (1, 2, 3, 4, 5, 6, 7, 8, 9),
("H", "\n", "e", "\n", "\n", "l", "l", "o"), (1, 2, 3, 4, 6, 7, 8, 9)))
read_size_tests.append(("H\re\n\r\nllo", 2, ("H\r", "e\n", "\r\n", "ll", "o"), (2, 4, 6, 8, 9),
("H\r", "e\n", "\r\n", "ll", "o"), (2, 4, 6, 8, 9),
("H\n", "e\n", "\nl", "lo"), (2, 4, 7, 9)))
else:
read_size_tests.append(("H\re\n\r\nllo", 1, ("H", "\r", "e", "\n", "\r", "\n", "l", "l", "o"), (1,2,3,4,5,6,7, 8, 9),
("H", "\r", "e", "\n", "\n", "l", "l", "o"), (1,2,3,4,6,7,8,9),
("H", "\n", "e", "\n", "\n", "l", "l", "o"), (1,2,3,4,6,7,8,9)))
read_size_tests.append(("H\re\n\r\nllo", 2, ("H\r", "e\n", "\r\n", "ll", "o"), (2, 4, 6, 8, 9),
("H\r", "e\n", "\nl", "lo"), (2,4,7, 9),
("H\n", "e\n", "\nl", "lo"), (2,4,7, 9)))
for test in read_size_tests:
# Write the test pattern to disk in binary mode.
with file(self.temp_file, "wb") as f:
f.write(test[0])
# Read the data back in each of the read modes we test.
for mode in range(3):
with file(self.temp_file, self.read_modes[mode][1]) as f:
self.assertFalse(f.closed)
# We read the data in the size specified by the test and expect to get
# the set of strings given for this specific mode.
size = test[1]
strings = test[2 + mode*2]
lengths = test[3 + mode*2]
count = 0
while True:
data = f.read(size)
if data == "":
self.assertTrue(count == len(strings))
break
count = count + 1
self.assertTrue(count <= len(strings))
self.assertTrue(data == strings[count - 1])
self.assertEqual(f.tell(), lengths[count-1])
f.close()
self.assertTrue(f.closed)
# And some readline tests.
# Test data is in the following format: ("raw data", (binary mode result strings)
# (text mode result strings)
# (universal mode result strings))
def test_readline(self):
readline_tests = [("Mary had a little lamb", ("Mary had a little lamb", ),
("Mary had a little lamb", ),
("Mary had a little lamb", )),
("Mary had a little lamb\r", ("Mary had a little lamb\r", ),
("Mary had a little lamb\r", ),
("Mary had a little lamb\n", )),
("Mary had a \rlittle lamb\r", ("Mary had a \rlittle lamb\r", ),
("Mary had a \rlittle lamb\r", ),
("Mary had a \n", "little lamb\n"))]
# wb doesn't change the output to just \n like on Windows (binary mode means nothing on POSIX)
if is_posix:
readline_tests.append(("Mary \r\nhad \na little lamb", ("Mary \r\n", "had \n", "a little lamb"),
("Mary \r\n", "had \n", "a little lamb"),
("Mary \n", "had \n", "a little lamb")))
else:
readline_tests.append(("Mary \r\nhad \na little lamb", ("Mary \r\n", "had \n", "a little lamb"),
("Mary \n", "had \n", "a little lamb"),
("Mary \n", "had \n", "a little lamb")))
for test in readline_tests:
# Write the test pattern to disk in binary mode.
with file(self.temp_file, "wb") as f:
f.write(test[0])
# Read the data back in each of the read modes we test.
for mode in range(3):
with file(self.temp_file, self.read_modes[mode][1]) as f:
# We read the data by line and expect to get a specific sets of lines back.
strings = test[1 + mode]
count = 0
while True:
data = f.readline()
if data == "":
self.assertEqual(count, len(strings))
break
count = count + 1
self.assertTrue(count <= len(strings))
self.assertEqual(data, strings[count - 1])
def format_tuple(self, tup):
if tup == None:
return "None"
if (isinstance(tup, str)):
return format_newlines(tup)
out = "("
for entry in tup:
out += format_newlines(entry) + ", "
out += ")"
return out
# Test the 'newlines' attribute.
# Format of the test data is the raw data written to the test file followed by a tuple representing the values
# of newlines expected after each line is read from the file in universal newline mode.
@unittest.skipUnless(is_cli, 'IronPython specific test')
def test_newlines_attribute(self):
newlines_tests = (("123", (None, )),
("1\r\n2\r3\n", ("\r\n", ("\r\n", "\r"), ("\r\n", "\r", "\n"))),
("1\r2\n3\r\n", ("\r", ("\r", "\n"), ("\r\n", "\r", "\n"))),
("1\n2\r\n3\r", ("\n", ("\r\n", "\n"), ("\r\n", "\r", "\n"))),
("1\r\n2\r\n3\r\n", ("\r\n", "\r\n", "\r\n")),
("1\r2\r3\r", ("\r", "\r", "\r")),
("1\n2\n3\n", ("\n", "\n", "\n")))
for test in newlines_tests:
# Write the test pattern to disk in binary mode.
with file(self.temp_file, "wb") as f:
f.write(test[0])
# Verify newlines isn't set while writing.
self.assertTrue(f.newlines == None)
# Verify that reading the file in binary or text mode won't set newlines.
with file(self.temp_file, "rb") as f:
data = f.read()
self.assertTrue(f.newlines == None)
with file(self.temp_file, "r") as f:
data = f.read()
self.assertTrue(f.newlines == None)
# Read file in universal mode line by line and verify we see the expected output at each stage.
expected = test[1]
with file(self.temp_file, "rU") as f:
self.assertTrue(f.newlines == None)
count = 0
while True:
data = f.readline()
if data == "":
break
self.assertTrue(count < len(expected))
self.assertTrue(f.newlines == expected[count])
count = count + 1
## coverage: a sequence of file operation
@unittest.skipIf(is_posix, 'file sequence specific to windows (because of newlines)')
def test_coverage(self):
with file(self.temp_file, 'w') as f:
self.assertTrue(str(f).startswith("<open file '%s', mode 'w'" % self.temp_file))
self.assertTrue(f.fileno() <> -1)
self.assertTrue(f.fileno() <> 0)
# write
self.assertRaises(TypeError, f.writelines, [3])
f.writelines(["firstline\n"])
f.close()
self.assertTrue(str(f).startswith("<closed file '%s', mode 'w'" % self.temp_file))
# append
with file(self.temp_file, 'a+') as f:
f.writelines(['\n', 'secondline\n'])
pos = len('secondline\n') + 1
f.seek(-1 * pos, 1)
f.writelines(['thirdline\n'])
# read
with file(self.temp_file, 'r+', 512) as f:
f.seek(-1 * pos - 2, 2)
self.assertEqual(f.readline(), 'e\n')
self.assertEqual(f.readline(5), 'third')
self.assertEqual(f.read(-1), 'line\n')
self.assertEqual(f.read(-1), '')
# read
with file(self.temp_file, 'rb', 512) as f:
f.seek(-1 * pos - 2, 2)
self.assertEqual(f.readline(), 'e\r\n')
self.assertEqual(f.readline(5), 'third')
self.assertEqual(f.read(-1), 'line\r\n')
self.assertEqual(f.read(-1), '')
## file op in os
os.unlink(self.temp_file)
fd = os.open(self.temp_file, os.O_CREAT | os.O_WRONLY)
os.write(fd, "hello ")
os.close(fd)
fd = os.open(self.temp_file, os.O_APPEND | os.O_WRONLY)
os.write(fd, "world")
os.close(fd)
fd = os.open(self.temp_file, 0)
self.assertEqual(os.read(fd, 1024), "hello world")
os.close(fd)
os.unlink(self.temp_file)
def test_encoding(self):
f = file(self.temp_file, 'w')
# we throw on flush, CPython throws on write, so both write & close need to catch
try:
f.write(u'\u6211')
f.close()
self.fail('UnicodeEncodeError should have been thrown')
except UnicodeEncodeError:
pass
if hasattr(sys, "setdefaultencoding"):
#and verify UTF8 round trips correctly
setenc = sys.setdefaultencoding
saved = sys.getdefaultencoding()
try:
setenc('utf8')
with file(self.temp_file, 'w') as f:
f.write(u'\u6211')
with file(self.temp_file, 'r') as f:
txt = f.read()
self.assertEqual(txt, u'\u6211')
finally:
setenc(saved)
@unittest.skipUnless(is_cli, 'IronPython specific test')
def test_net_stream(self):
import System
fs = System.IO.FileStream(self.temp_file, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite)
with file(fs, "wb") as f:
f.write('hello\rworld\ngoodbye\r\n')
with file(self.temp_file, 'rb') as f:
self.assertEqual(f.read(), 'hello\rworld\ngoodbye\r\n')
with file(self.temp_file, 'rU') as f:
self.assertEqual(f.read(), 'hello\nworld\ngoodbye\n')
@unittest.skipUnless(is_cli, 'IronPython specific test')
@unittest.skipIf(is_mono, 'Mono has a different GC setup, so we can not rely on the Collect to work the same')
def test_file_manager(self):
def return_fd1():
f = file(self.temp_file, 'w')
return f.fileno()
def return_fd2():
return os.open(self.temp_file, 0)
import System
fd = return_fd1()
System.GC.Collect()
System.GC.WaitForPendingFinalizers()
self.assertRaises(OSError, os.fdopen, fd)
fd = return_fd2()
System.GC.Collect()
System.GC.WaitForPendingFinalizers()
f = os.fdopen(fd)
f.close()
self.assertRaises(OSError, os.fdopen, fd)
def test_sharing(self):
modes = ['w', 'w+', 'a+', 'r', 'w']
for xx in modes:
for yy in modes:
x = file('tempfile.txt', xx)
y = file('tempfile.txt', yy)
x.close()
y.close()
os.unlink('tempfile.txt')
@unittest.skipIf(is_posix, 'Currently failing on CI because of running as root, needs more debug')
def test_overwrite_readonly(self):
filename = "tmp.txt"
f = file(filename, "w+")
f.write("I am read-only")
f.close()
os.chmod(filename, 256)
try:
try:
f = file(filename, "w+") # FAIL
finally:
os.chmod(filename, 128)
os.unlink(filename)
except IOError, e:
pass
else:
self.assertUnreachable() # should throw
#any other exceptions fail
def test_inheritance_kwarg_override(self):
class TEST(file):
def __init__(self,fname,VERBOSITY=0):
file.__init__(self,fname,"w",1)
self.VERBOSITY = VERBOSITY
f=TEST(r'sometext.txt',VERBOSITY=1)
self.assertEqual(f.VERBOSITY, 1)
f.close()
os.unlink('sometext.txt')
# file newline handling test
@unittest.skipIf(is_posix, "this test doesn't really make sense for posix since b doesn't change the behavior")
def test_newline(self):
def test_newline(norm, mode):
f = file("testfile.tmp", mode)
self.assertTrue(f.read() == norm)
for x in xrange(len(norm)):
f.seek(0)
a = f.read(x)
b = f.read(1)
c = f.read()
self.assertTrue(a+b+c == norm)
f.close()
self.assertRaises(TypeError, file, None) # arg must be string
self.assertRaises(TypeError, file, [])
self.assertRaises(TypeError, file, 1)
norm = "Hi\nHello\nHey\nBye\nAhoy\n"
unnorm = "Hi\r\nHello\r\nHey\r\nBye\r\nAhoy\r\n"
f = file("testfile.tmp", "wb")
f.write(unnorm)
f.close()
test_newline(norm, "r")
test_newline(unnorm, "rb")
os.unlink("testfile.tmp")
def test_creation(self):
f = file.__new__(file, None)
self.assertTrue(repr(f).startswith("<closed file '<uninitialized file>', mode '<uninitialized file>' at"))
self.assertRaises(TypeError, file, None)
def test_repr(self):
class x(file):
def __repr__(self): return 'abc'
f = x('repr_does_not_exist', 'w')
self.assertEqual(repr(f), 'abc')
f.close()
os.unlink('repr_does_not_exist')
def test_truncate(self):
# truncate()
with file('abc.txt', 'w') as a:
a.write('hello world\n')
a.truncate()
with file('abc.txt', 'r') as a:
self.assertEqual(a.readlines(), ['hello world\n'])
os.unlink('abc.txt')
# truncate(#)
with file('abc.txt', 'w') as a:
a.write('hello\nworld\n')
a.truncate(6)
with file('abc.txt', 'r') as a:
if is_posix:
self.assertEqual(a.readlines(), ['hello\n'])
else:
self.assertEqual(a.readlines(), ['hello\r'])
os.unlink('abc.txt')
# truncate(#) invalid args
with file('abc.txt', 'w') as a:
self.assertRaises(IOError, a.truncate, -1)
self.assertRaises(TypeError, a.truncate, None)
# read-only file
with file('abc.txt', 'r') as a:
self.assertRaises(IOError, a.truncate)
self.assertRaises(IOError, a.truncate, 0)
os.unlink('abc.txt')
# std-out
self.assertRaises(IOError, sys.stdout.truncate)
def test_modes(self):
"""test various strange mode combinations and error reporting"""
try:
with file('test_file', 'w') as x:
self.assertEqual(x.mode, 'w')
# don't allow empty modes
self.assertRaisesMessage(ValueError, 'empty mode string', file, 'abc', '')
# mode must start with valid value
self.assertRaisesMessage(ValueError, "mode string must begin with one of 'r', 'w', 'a' or 'U', not 'p'", file, 'abc', 'p')
# allow anything w/ U but r and w
self.assertRaisesMessage(ValueError, "universal newline mode can only be used with modes starting with 'r'", file, 'abc', 'Uw')
self.assertRaisesMessage(ValueError, "universal newline mode can only be used with modes starting with 'r'", file, 'abc', 'Ua')
self.assertRaisesMessage(ValueError, "universal newline mode can only be used with modes starting with 'r'", file, 'abc', 'Uw+')
self.assertRaisesMessage(ValueError, "universal newline mode can only be used with modes starting with 'r'", file, 'abc', 'Ua+')
# check invalid modes
self.assertRaises(ValueError, file, 'test_file', 'pU')
self.assertRaises(ValueError, file, 'test_file', 'pU+')
self.assertRaises(ValueError, file, 'test_file', 'rFOOBAR')
finally:
os.unlink('test_file')
@unittest.skipUnless(is_cli, 'Unstable with CPython')
def test_cp16623(self):
'''
If this test ever fails randomly, there is a problem around file thread
safety. Do not wrap this test case with retry_on_failure!
'''
global FINISHED_COUNTER
FINISHED_COUNTER = 0
import time
expected_lines = ["a", "bbb" * 100, "cc"]
total_threads = 50
file_name = os.path.join(self.temporary_dir, "cp16623.txt")
with open(file_name, "w") as f:
def write_stuff():
global FINISHED_COUNTER
global CP16623_LOCK
for j in xrange(100):
for i in xrange(50):
print >> f, "a"
print >> f, "bbb" * 1000
for i in xrange(10):
print >> f, "cc"
with CP16623_LOCK:
FINISHED_COUNTER += 1
for i in xrange(total_threads):
thread.start_new_thread(write_stuff, ())
#Give all threads some time to finish
for i in xrange(total_threads):
if FINISHED_COUNTER!=total_threads:
print "*",
time.sleep(1)
else:
break
self.assertEqual(FINISHED_COUNTER, total_threads)
#Verifications - since print isn't threadsafe the following
#is pointless... Just make sure IP doesn't throw.
#f = open(file_name, "r")
#lines = f.readlines()
#for line in lines:
# self.assertTrue(line in expected_lines, line)
def test_write_buffer(self):
try:
for mode in ('b', ''):
with open('foo', 'w+' + mode) as foo:
b = buffer(b'hello world', 6)
foo.write(b)
with open('foo', 'r') as foo:
self.assertEqual(foo.readlines(), ['world'])
with open('foo', 'w+') as foo:
b = buffer(u'hello world', 6)
foo.write(b)
with open('foo', 'r') as foo:
self.assertEqual(foo.readlines(), ['world'])
with open('foo', 'w+b') as foo:
b = buffer(u'hello world', 6)
foo.write(b)
with open('foo', 'r') as foo:
if is_cpython:
self.assertEqual(foo.readlines(), ['l\x00o\x00 \x00w\x00o\x00r\x00l\x00d\x00'])
else:
self.assertEqual(foo.readlines(), ['world'])
finally:
self.delete_files("foo")
@unittest.skipIf(is_netcoreapp21, "https://github.com/IronLanguages/ironpython2/issues/348")
def test_errors(self):
try:
file('some_file_that_really_does_not_exist')
except Exception, e:
self.assertEqual(e.errno, 2)
else:
self.assertUnreachable()
try:
file('path_too_long' * 100)
except Exception, e:
self.assertEqual(e.errno, 2)
else:
self.assertUnreachable()
def test_write_bytes(self):
f = open("temp_ip", "w+")
try:
f.write(b"Hello\n")
f.close()
f = file('temp_ip')
self.assertEqual(f.readlines(), ['Hello\n'])
f.close()
finally:
f.close()
os.unlink('temp_ip')
def test_kw_args(self):
file(name = 'some_test_file.txt', mode = 'w').close()
os.unlink('some_test_file.txt')
def test_buffering_kwparam(self):
#--Positive
for x in [-2147483648, -1, 0, 1, 2, 1024, 2147483646, 2147483647]:
f = file(name = 'some_test_file.txt', mode = 'w', buffering=x)
f.close()
os.unlink('some_test_file.txt')
self.assertRaisesMessage(TypeError, "integer argument expected, got float",
file, 'some_test_file.txt', 'w', 3.14)
#--Negative
for x in [None, "abc", u"", [], tuple()]:
self.assertRaises(TypeError, #"an integer is required",
lambda: file(name = 'some_test_file.txt', mode = 'w', buffering=x))
for x in [2147483648, -2147483649]:
self.assertRaises(OverflowError, #"long int too large to convert to int",
lambda: file(name = 'some_test_file.txt', mode = 'w', buffering=x))
def test_open_with_BOM(self):
"""https://github.com/IronLanguages/main/issues/1088"""
fileName = os.path.join(self.test_dir, "file_without_BOM.txt")
with open(fileName, "r") as f:
if is_posix: self.assertEqual(f.read(), "\x42\xc3\x93\x4d\x0d\x0a")
else: self.assertEqual(f.read(), "\x42\xc3\x93\x4d\x0a")
with open(fileName, "rb") as f:
self.assertEqual(f.read(), "\x42\xc3\x93\x4d\x0d\x0a")
fileName = os.path.join(self.test_dir, "file_with_BOM.txt")
with open(fileName, "r") as f:
if is_posix: self.assertEqual(f.read(), "\xef\xbb\xbf\x42\xc3\x93\x4d\x0d\x0a")
else: self.assertEqual(f.read(), "\xef\xbb\xbf\x42\xc3\x93\x4d\x0a")
with open(fileName, "rb") as f:
self.assertEqual(f.read(), "\xef\xbb\xbf\x42\xc3\x93\x4d\x0d\x0a")
run_test(__name__)