My favorites | Sign in
Project Logo
                
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
from __future__ import with_statement

import gc
import miniunit
import __builtin__

import storage
import storage_backend

dict_backend = storage.backend
FILE = '/myfile.txt'


class TestFileType(miniunit.TestCase):
def setUp(self):
storage.backend = storage_backend
gc.collect()
gc.collect()
if storage_backend.CheckForFile(FILE):
storage_backend.DeleteFile(FILE)


def test_patching(self):
storage.replace_builtins()
try:
self.assertEqual(__builtin__.file, storage.file)
self.assertEqual(__builtin__.open, storage.open)
finally:
storage.restore_builtins()

self.assertEqual(__builtin__.file, storage.original_file)
self.assertNotEqual(storage.original_file, storage.file)
self.assertEqual(__builtin__.open, storage.original_open)
self.assertNotEqual(storage.original_open, storage.open)


def test_file_simple_read_and_write(self):
source_data = 'Some text\nwith newlines\n'

handle = storage.file(FILE, 'w')
self.assertEquals(handle.mode, 'w')
handle.write(source_data)
handle.close()

handle = storage.file(FILE,'r')
data = handle.read()
self.assertEquals(handle.mode, 'r')
handle.close()
self.assertEqual(data, source_data)

handle = storage.file(FILE)
self.assertEquals(handle.mode, 'r')
data = handle.read()
handle.close()
self.assertEqual(data, source_data)


def test_open_simple_read_and_write(self):
source_data = 'Some text\nwith newlines\n'

handle = storage.open(FILE, 'w')
self.assertTrue(isinstance(handle, storage.file))
self.assertEquals(handle.mode, 'w')
handle.write(source_data)
handle.close()

handle = storage.open(FILE, 'r')
self.assertTrue(isinstance(handle, storage.file))
data = handle.read()
self.assertEquals(handle.mode, 'r')
handle.close()
self.assertEqual(data, source_data)

handle = storage.open(FILE)
self.assertTrue(isinstance(handle, storage.file))
self.assertEquals(handle.mode, 'r')
data = handle.read()
handle.close()
self.assertEqual(data, source_data)


def test_read_to_write(self):
handle = storage.file(FILE, 'w')
handle.write('some new data')
handle.close()

h = storage.file(FILE)
self.assertRaises(IOError, h.write, 'foobar')


def test_write_to_read(self):
h = storage.file(FILE, 'w')
self.assertRaises(IOError, h.read)


def test_multiple_writes(self):
handle = storage.file(FILE, 'w')
handle.write('foo')
handle.write('bar')
handle.close()

self.assertEqual(storage.file(FILE).read(), 'foobar')


def test_repr(self):
write = storage.file(FILE, 'w')
read = storage.file(FILE, 'r')
string = '<open file %r mode %r>'
self.assertEqual(repr(read), string % (FILE, 'r'))
self.assertEqual(repr(write), string % (FILE, 'w'))

string = '<closed file %r mode %r>'
write.close()
read.close()
self.assertEqual(repr(read), string % (FILE, 'r'))
self.assertEqual(repr(write), string % (FILE, 'w'))


def test_invalid_mode(self):
self.assertRaises(ValueError, storage.file, 'filename', 'q')
self.assertRaises(ValueError, storage.file, 'filename', '')
self.assertRaises(TypeError, storage.file, 'filename', None)
self.assertRaises(TypeError, storage.file, 'filename', 3)


def test_open_nonexistent_file(self):
self.assertRaises(IOError, storage.file, FILE + FILE, 'r')


def test_open_write_deletes_and_creates(self):
handle = storage.file(FILE, 'w')
self.assertEqual(storage.file(FILE).read(), '')
handle.write('foobar')
handle.close()

handle = storage.file(FILE, 'w')
self.assertEqual(storage.file(FILE).read(), '')


def test_gc_closes(self):
handle = storage.file(FILE, 'w')
handle.write('some new data')
del handle
gc.collect()
gc.collect()
self.assertEqual(storage.file(FILE).read(), 'some new data')


def test_closed(self):
handle = storage.file(FILE, 'w')
self.assertFalse(handle.closed)

handle.close()
self.assertTrue(handle.closed)

self.assertRaises(ValueError, handle.write, 'foo')

handle = storage.file(FILE)
self.assertFalse(handle.closed)

handle.close()
self.assertTrue(handle.closed)

self.assertRaises(ValueError, handle.read)


def test_read_seek_tell(self):
h = storage.file(FILE, 'w')
h.write('foobar')
h.close()

h = storage.file(FILE)
self.assertEqual(h.tell(), 0)

self.assertEqual(h.read(0), '')
self.assertEqual(h.read(1), 'f')
self.assertEqual(h.tell(), 1)
h.seek(0)
self.assertEqual(h.tell(), 0)
self.assertEqual(h.read(1), 'f')

self.assertEqual(h.read(), 'oobar')
self.assertEqual(h.read(), '')

h.seek(0)
self.assertEqual(h.read(100), 'foobar')

h.seek(1000)
self.assertEqual(h.tell(), 1000)
self.assertEqual(h.read(100), '')

self.assertRaises(IOError, h.seek, -1)
self.assertRaises(TypeError, h.seek, None)

# test deprecation warning for float value?


def test_write_seek_tell(self):
h = storage.file(FILE, 'w')
self.assertEqual(h.tell(), 0)

h.write('f')
self.assertEqual(h.tell(), 1)
h.seek(2)
self.assertEqual(h.tell(), 2)
h.write('g')
h.close()
self.assertEqual(storage.file(FILE).read(), 'f\x00g')

h = storage.file(FILE, 'w')
h.write('f')
h.seek(0)
h.write('g')
h.close()
self.assertEqual(storage.file(FILE).read(), 'g')

h = storage.file(FILE, 'w')
h.seek(1000)
h.write('g')
h.close()

expected = '\x00' * 1000 + 'g'
self.assertEqual(storage.file(FILE).read(), expected)

self.assertRaises(IOError, h.seek, -1)
self.assertRaises(TypeError, h.seek, None)


def test_flush(self):
h = storage.file(FILE, 'w')
h.write('foo')

read_handle = storage.file(FILE)
self.assertEqual(read_handle.read(), '')

h.flush()
self.assertEqual(read_handle.read(), 'foo')
h.close()

self.assertRaises(IOError, read_handle.flush)
read_handle.close()


def test_read_write_binary(self):
h = storage.file(FILE, 'w')
h.write('foo\nbar\n')
h.close()

h = storage.file(FILE)
self.assertEqual(h.read(), 'foo\nbar\n')
h.close()

h = storage.file(FILE, 'rb')
self.assertEqual(h.read(), 'foo\r\nbar\r\n')
h.close()

h = storage.file(FILE, 'wb')
h.write('foo\nbar\n')
h.close()

h = storage.file(FILE, 'rb')
self.assertEqual(h.read(), 'foo\nbar\n')
h.close()

h = storage.file(FILE, 'w')
h.write('foo\nbar\n')
h.close()

h = storage.file(FILE, 'rb')
self.assertEqual(h.read(), 'foo\r\nbar\r\n')
h.close()


def test_assorted_members(self):
h = storage.file(FILE, 'w')
self.assertEquals(h.encoding, None)
self.assertEqual(h.errors, None)
self.assertEqual(h.newlines, None)
self.assertFalse(h.isatty())
h.close()

h = storage.file(FILE)
self.assertEquals(h.encoding, None)
self.assertEqual(h.errors, None)
self.assertEqual(h.newlines, None)
self.assertFalse(h.isatty())
h.close()


def test_fileno(self):
h = storage.file(FILE, 'w')
h2 = storage.file(FILE)
fileno1 = h.fileno()
fileno2 = h2.fileno()

self.assertTrue(isinstance(fileno1, int))
self.assertTrue(isinstance(fileno2, int))

self.assertTrue(fileno1 > 2)
self.assertTrue(fileno2 > 2)

self.assertNotEqual(fileno1, fileno2)

h.close()
h2.close()


def test__iter__(self):
# not as hard as you might think
h = storage.file(FILE, 'w')
i = h.__iter__()
self.assertTrue(h is i)
h.close()


def test_next(self):
h = storage.file(FILE, 'w')
h.write('foo\nbar\nbaz\n')
self.assertRaises(IOError, h.next)
h.close()

h = storage.file(FILE)
self.assertEqual(h.next(), 'foo\n')

self.assertRaises(ValueError, h.read)

self.assertEqual(h.next(), 'bar\n')
self.assertEqual(h.next(), 'baz\n')
self.assertRaises(StopIteration, h.next)
h.close()

h = storage.file(FILE)
self.assertEqual(h.next(), 'foo\n')
h.seek(1)
self.assertEqual(h.next(), 'oo\n')

h.seek(3)
self.assertEqual(h.read(4), '\nbar')
self.assertEqual(h.next(), '\n')


def test_readline(self):
h = storage.file(FILE, 'w')
h.write('foo\nbar\nbaz\n')
self.assertRaises(IOError, h.readline)
h.close()

h = storage.file(FILE)
self.assertEqual(h.readline(), 'foo\n')
self.assertEqual(h.tell(), 4)
h.seek(0)
self.assertEqual(h.readline(), 'foo\n')

self.assertRaises(TypeError, h.readline, None)
self.assertEqual(h.readline(0), '')
self.assertEqual(h.readline(1), 'b')
self.assertEqual(h.readline(100), 'ar\n')
self.assertEqual(h.tell(), 8)
self.assertEqual(h.readline(-1), 'baz\n')
self.assertEqual(h.tell(), 12)
self.assertEqual(h.readline(), '')
self.assertEqual(h.tell(), 12)
h.close()


def test_readlines(self):
h = storage.file(FILE, 'w')
h.write('foo\nbar\nbaz\n')
self.assertRaises(IOError, h.readlines)
h.close()

h = storage.file(FILE)
self.assertEqual(h.readlines(), ['foo\n', 'bar\n', 'baz\n'])
self.assertEqual(h.readlines(), [])
h.seek(0)

self.assertRaises(TypeError, h.readline, None)
self.assertEqual(h.readlines(0), ['foo\n', 'bar\n', 'baz\n'])
h.close()


def test_with(self):
with storage.file(FILE, 'w') as h:
h.write('foo\nbar\nbaz\n')
self.assertTrue(h.closed)

with storage.file(FILE) as h:
self.assertEqual(h.readline(), 'foo\n')
self.assertEqual(h.tell(), 4)
self.assertTrue(h.closed)

try:
with storage.file(FILE) as h:
raise IOError
except IOError:
self.assertTrue(h.closed)
else:
self.fail('IOError in with statement swallowed')


def test_xreadlines(self):
h = storage.file(FILE, 'w')
self.assertTrue(h.xreadlines() is h)
h.close()


def test_softspace(self):
h = storage.file(FILE, 'w')
h.softspace = 1
h.write('blam')
self.assertEqual(h.softspace, 0)

def set_softspace():
h.softspace = 'kablooie'
self.assertRaises(TypeError, set_softspace)

h.close()


def test_truncate(self):
h = storage.file(FILE, 'w')
h.write('kabloooie')

self.assertRaises(IOError, storage.file(FILE).truncate)
self.assertRaises(TypeError, h.truncate, 'foo')
self.assertRaises(IOError, h.truncate, -1)

h.seek(3)
h.truncate()
self.assertEqual(storage.file(FILE).read(), 'kab')

h.truncate(10)
self.assertEqual(storage.file(FILE).read(), 'kab\x00\x00\x00\x00\x00\x00\x00')
self.assertEqual(h.tell(), 3)

h.truncate(2)
self.assertEqual(h.tell(), 3)
h.close()


def test_writelines(self):
h = storage.file(FILE, 'w')


self.assertRaises(IOError, storage.file(FILE).writelines, [])
self.assertRaises(TypeError, h.writelines, object())

h.write('blah')
h.writelines(['\n','q', 'w', 'e'])
h.close()

f = storage.file(FILE)
data = f.read()
f.close()
self.assertEqual(data, 'blah\nqwe')

def test_append_mode(self):
source_data = 'Some text\nwith newlines\n'

with storage.open(FILE, 'ab') as handle:
with storage.open(FILE) as f:
self.assertEqual(f.read(), '')

handle.write(source_data)

with storage.open(FILE, 'rb') as handle:
self.assertEqual(handle.read(), source_data)

with storage.open(FILE, 'a') as handle:
self.assertEqual(handle.tell(), len(source_data))
handle.write(source_data[::-1])

with storage.open(FILE) as handle:
self.assertEqual(handle.read(), source_data + source_data[::-1])


def test_read_write_mode(self):
self.assertRaises(IOError, storage.file, FILE, 'r+')

with storage.file(FILE, 'w') as h:
h.write('foo')

with storage.file(FILE, 'r+') as h:
self.assertEqual(h.tell(), 0)
self.assertEqual(h.read(), 'foo')
self.assertEqual(h.tell(), 3)
h.write('bar')
self.assertEqual(h.tell(), 6)
h.seek(0)
self.assertEqual(h.read(), 'foobar')


def test_write_read_mode(self):
with storage.file(FILE, 'w') as f:
f.write('foo')

with storage.file(FILE, 'w+') as h:
with storage.file(FILE) as f:
self.assertEqual(f.read(), '')

self.assertEqual(h.read(), '')
self.assertEqual(h.tell(), 0)
h.write('foo')
self.assertEqual(h.tell(), 3)
self.assertEqual(h.read(), '')

h.seek(0)
self.assertEqual(h.read(), 'foo')


def test_append_read_mode(self):
with storage.file(FILE, 'w') as f:
f.write('foo')

with storage.file(FILE, 'a+') as h:
with storage.file(FILE) as f:
self.assertEqual(f.read(), 'foo')

self.assertEqual(h.tell(), 3)
self.assertEqual(h.read(), '')
h.write('bar')
self.assertEqual(h.tell(), 6)
self.assertEqual(h.read(), '')

h.seek(0)
self.assertEqual(h.read(), 'foobar')


def test_seek_with_whence(self):
data = 'foo bar baz'
with storage.file(FILE, 'w') as h:
h.write(data)

h = storage.file(FILE)
self.assertRaises(IOError, h.seek, 0, 3)
self.assertRaises(IOError, h.seek, -1, 0)
self.assertRaises(IOError, h.seek, 0, -1)
self.assertRaises(TypeError, h.seek, 0, None)

h.seek(3, 0)
self.assertEqual(h.tell(), 3)

h.seek(-3, 1)
self.assertEqual(h.tell(), 0)
self.assertRaises(IOError, h.seek, -1, 1)

h.seek(3, 1)
self.assertEqual(h.tell(), 3)

h.seek(0, 2)
self.assertEqual(h.tell(), len(data))

h.seek(-2, 2)
self.assertEqual(h.tell(), len(data) - 2)

h.seek(2, 2)
self.assertEqual(h.tell(), len(data) + 2)
self.assertRaises(IOError, h.seek, -(len(data) + 1), 2)


def test_read_only_attributes(self):
def setter(attribute, value):
return lambda: setattr(h, attribute, value)

with storage.file(FILE, 'w') as h:
self.assertRaises(AttributeError, setter('mode', 'w'))
self.assertRaises(AttributeError, setter('name', 'foo2'))
self.assertRaises(AttributeError, setter('closed', True))
self.assertRaises(AttributeError, setter('encoding', 'ascii'))
self.assertRaises(AttributeError, setter('errors', None))
self.assertRaises(AttributeError, setter('newlines', 'foo'))


def test_read_negative(self):
with storage.file(FILE, 'w') as h:
h.write('foo bar baz')

with storage.file(FILE) as h:
self.assertEqual(h.read(-3), 'foo bar baz')


def test_invalid_name(self):
self.assertRaises(IOError, storage.file, '')
self.assertRaises(IOError, storage.file, '', 'w')
self.assertRaises(TypeError, storage.file, None)
self.assertRaises(TypeError, storage.file, None, 'w')
self.assertRaises(TypeError, storage.file, 3)


def test_default_backend(self):
storage.backend = dict_backend

self.assertRaises(IOError, storage.file, FILE)

with storage.file(FILE, 'w') as h:
h.write('foo')

with storage.file(FILE) as h:
self.assertEqual(h.read(), 'foo')


"""
Differences from standard file type:

* Attempting to set the read-only attributes (like mode, name etc) raises an AttributeError
rather than a TypeError
* The exception messages are not all identical (some are better!)
* Strict about modes. Unrecognised modes always raise an exception

(NOTE: The exception method that the standard file type does throw is:
"ValueError: mode string must begin with one of 'r', 'w', 'a' or 'U', not 'z'")

* The deprecated readinto is not implemented

TODO:

* The buffering argument to the constructor is not implemented
* The IOError exceptions raised don't have an associated errno
* encoding, errors and newlines do nothing
* Behavior of tell() and seek() for text mode files may be incorrect (it
should treat '\n' as '\r\n')
* Behaves like Windows, writes '\n' as '\r\n' unless in binary mode. A global
flag to control this?
* Universal modes not supported
* Missing __format__ method needed when we move to 2.6
* Implementations of os and os.path that work with storage_backend
"""
Show details Hide details

Change log

r129 by fuzzyman on Oct 15, 2009   Diff
Default dictionary based backend for the
file type.
Go to: 
Project members, sign in to write a code review

Older revisions

r128 by fuzzyman on Oct 15, 2009   Diff
Using the new file implementation in
the tutorial.
r127 by fuzzyman on Oct 14, 2009   Diff
Read only file attributes are now
properties.
r126 by fuzzyman on Oct 14, 2009   Diff
Whence argument to file.seek now
implemented.
All revisions of this file

File info

Size: 18858 bytes, 627 lines
Hosted by Google Code