3 from array
import array
5 from test
.test_support
import verify
, TESTFN
, TestFailed
6 from UserList
import UserList
8 # verify writelines with instance sequence
9 l
= UserList(['1', '2'])
10 f
= open(TESTFN
, 'wb')
13 f
= open(TESTFN
, 'rb')
19 a
= array('c', 'x'*10)
20 f
= open(TESTFN
, 'rb')
23 verify(buf
== a
.tostring()[:n
])
25 # verify writelines with integers
26 f
= open(TESTFN
, 'wb')
28 f
.writelines([1, 2, 3])
32 print "writelines accepted sequence of integers"
35 # verify writelines with integers in UserList
36 f
= open(TESTFN
, 'wb')
43 print "writelines accepted sequence of integers"
46 # verify writelines with non-string object
49 f
= open(TESTFN
, 'wb')
51 f
.writelines([NonString(), NonString()])
55 print "writelines accepted sequence of non-string objects"
58 # verify that we get a sensible error message for bad mode argument
61 open(TESTFN
, bad_mode
)
65 if s
.find(TESTFN
) != -1 or s
.find(bad_mode
) == -1:
66 print "bad error message for invalid mode: %s" % s
67 # if msg[0] == 0, we're probably on Windows where there may be
68 # no obvious way to discover why open() failed.
70 print "no error for invalid mode: %s" % bad_mode
74 raise TestFailed
, 'file.name should be "%s"' % TESTFN
76 raise TestFailed
, 'file.isatty() should be false'
79 raise TestFailed
, 'file.closed should be false'
86 raise TestFailed
, 'file.readinto("") should raise a TypeError'
90 raise TestFailed
, 'file.closed should be true'
92 methods
= ['fileno', 'flush', 'isatty', 'next', 'read', 'readinto',
93 'readline', 'readlines', 'seek', 'tell', 'truncate', 'write',
94 'xreadlines', '__iter__']
95 if sys
.platform
.startswith('atheos'):
96 methods
.remove('truncate')
98 for methodname
in methods
:
99 method
= getattr(f
, methodname
)
105 raise TestFailed
, 'file.%s() on a closed file should raise a ValueError' % methodname
112 raise TestFailed
, 'file.writelines([]) on a closed file should raise a ValueError'