3 from array
import array
5 from test
.test_support
import verify
, TESTFN
, TestFailed
6 from UserList
import UserList
8 # verify expected attributes exist
10 softspace
= f
.softspace
11 f
.name
# merely shouldn't blow up
15 # verify softspace is writable
16 f
.softspace
= softspace
# merely shouldn't blow up
18 # verify the others aren't
19 for attr
in 'name', 'mode', 'closed':
21 setattr(f
, attr
, 'oops')
25 raise TestFailed('expected TypeError setting file attr %r' % attr
)
28 # verify writelines with instance sequence
29 l
= UserList(['1', '2'])
30 f
= open(TESTFN
, 'wb')
33 f
= open(TESTFN
, 'rb')
39 a
= array('c', 'x'*10)
40 f
= open(TESTFN
, 'rb')
43 verify(buf
== a
.tostring()[:n
])
45 # verify writelines with integers
46 f
= open(TESTFN
, 'wb')
48 f
.writelines([1, 2, 3])
52 print "writelines accepted sequence of integers"
55 # verify writelines with integers in UserList
56 f
= open(TESTFN
, 'wb')
63 print "writelines accepted sequence of integers"
66 # verify writelines with non-string object
69 f
= open(TESTFN
, 'wb')
71 f
.writelines([NonString(), NonString()])
75 print "writelines accepted sequence of non-string objects"
78 # verify that we get a sensible error message for bad mode argument
81 open(TESTFN
, bad_mode
)
85 if s
.find(TESTFN
) != -1 or s
.find(bad_mode
) == -1:
86 print "bad error message for invalid mode: %s" % s
87 # if msg[0] == 0, we're probably on Windows where there may be
88 # no obvious way to discover why open() failed.
90 print "no error for invalid mode: %s" % bad_mode
94 raise TestFailed
, 'file.name should be "%s"' % TESTFN
96 raise TestFailed
, 'file.isatty() should be false'
99 raise TestFailed
, 'file.closed should be false'
106 raise TestFailed
, 'file.readinto("") should raise a TypeError'
110 raise TestFailed
, 'file.closed should be true'
112 # make sure that explicitly setting the buffer size doesn't cause
113 # misbehaviour especially with repeated close() calls
114 for s
in (-1, 0, 1, 512):
116 f
= open(TESTFN
, 'w', s
)
120 f
= open(TESTFN
, 'r', s
)
125 raise TestFailed
, 'error setting buffer size %d: %s' % (s
, str(msg
))
127 raise TestFailed
, 'readback failure using buffer size %d'
129 methods
= ['fileno', 'flush', 'isatty', 'next', 'read', 'readinto',
130 'readline', 'readlines', 'seek', 'tell', 'truncate', 'write',
131 'xreadlines', '__iter__']
132 if sys
.platform
.startswith('atheos'):
133 methods
.remove('truncate')
135 for methodname
in methods
:
136 method
= getattr(f
, methodname
)
142 raise TestFailed
, 'file.%s() on a closed file should raise a ValueError' % methodname
149 raise TestFailed
, 'file.writelines([]) on a closed file should raise a ValueError'