3 from array
import array
4 from weakref
import proxy
6 from test
.test_support
import verify
, TESTFN
, TestFailed
7 from UserList
import UserList
9 # verify weak references
13 verify(f
.tell(), p
.tell())
18 except ReferenceError:
21 raise TestFailed('file proxy still exists when the file is gone')
23 # verify expected attributes exist
25 softspace
= f
.softspace
26 f
.name
# merely shouldn't blow up
30 # verify softspace is writable
31 f
.softspace
= softspace
# merely shouldn't blow up
33 # verify the others aren't
34 for attr
in 'name', 'mode', 'closed':
36 setattr(f
, attr
, 'oops')
37 except (AttributeError, TypeError):
40 raise TestFailed('expected exception setting file attr %r' % attr
)
43 # check invalid mode strings
44 for mode
in ("", "aU", "wU+"):
46 f
= file(TESTFN
, mode
)
51 raise TestFailed('%r is an invalid file mode' % mode
)
53 # verify writelines with instance sequence
54 l
= UserList(['1', '2'])
55 f
= open(TESTFN
, 'wb')
58 f
= open(TESTFN
, 'rb')
64 a
= array('c', 'x'*10)
65 f
= open(TESTFN
, 'rb')
68 verify(buf
== a
.tostring()[:n
])
70 # verify writelines with integers
71 f
= open(TESTFN
, 'wb')
73 f
.writelines([1, 2, 3])
77 print "writelines accepted sequence of integers"
80 # verify writelines with integers in UserList
81 f
= open(TESTFN
, 'wb')
88 print "writelines accepted sequence of integers"
91 # verify writelines with non-string object
94 f
= open(TESTFN
, 'wb')
96 f
.writelines([NonString(), NonString()])
100 print "writelines accepted sequence of non-string objects"
108 print "should not be able to seek on sys.stdin"
115 print "should not be able to truncate on sys.stdin"
119 if not repr(f
).startswith("<open file '" + TESTFN
):
120 print "repr(file) failed"
123 # verify repr works for unicode too
124 f
= open(unicode(TESTFN
))
125 if not repr(f
).startswith("<open file u'" + TESTFN
):
126 print "repr(file with unicode name) failed"
129 # verify that we get a sensible error message for bad mode argument
132 open(TESTFN
, bad_mode
)
136 if s
.find(TESTFN
) != -1 or s
.find(bad_mode
) == -1:
137 print "bad error message for invalid mode: %s" % s
138 # if msg[0] == 0, we're probably on Windows where there may be
139 # no obvious way to discover why open() failed.
141 print "no error for invalid mode: %s" % bad_mode
145 raise TestFailed
, 'file.name should be "%s"' % TESTFN
147 raise TestFailed
, 'file.isatty() should be false'
150 raise TestFailed
, 'file.closed should be false'
157 raise TestFailed
, 'file.readinto("") should raise a TypeError'
161 raise TestFailed
, 'file.closed should be true'
163 # make sure that explicitly setting the buffer size doesn't cause
164 # misbehaviour especially with repeated close() calls
165 for s
in (-1, 0, 1, 512):
167 f
= open(TESTFN
, 'w', s
)
171 f
= open(TESTFN
, 'r', s
)
176 raise TestFailed
, 'error setting buffer size %d: %s' % (s
, str(msg
))
178 raise TestFailed
, 'readback failure using buffer size %d'
180 methods
= ['fileno', 'flush', 'isatty', 'next', 'read', 'readinto',
181 'readline', 'readlines', 'seek', 'tell', 'truncate', 'write',
182 'xreadlines', '__iter__']
183 if sys
.platform
.startswith('atheos'):
184 methods
.remove('truncate')
186 for methodname
in methods
:
187 method
= getattr(f
, methodname
)
193 raise TestFailed
, 'file.%s() on a closed file should raise a ValueError' % methodname
200 raise TestFailed
, 'file.writelines([]) on a closed file should raise a ValueError'
205 # SF bug <http://www.python.org/sf/801631>
206 # "file.truncate fault on windows"
207 f
= file(TESTFN
, 'wb')
208 f
.write('12345678901') # 11 bytes
211 f
= file(TESTFN
,'rb+')
214 raise TestFailed("Read on file opened for update failed %r" % data
)
216 raise TestFailed("File pos after read wrong %d" % f
.tell())
220 raise TestFailed("File pos after ftruncate wrong %d" % f
.tell())
223 size
= os
.path
.getsize(TESTFN
)
225 raise TestFailed("File size after ftruncate wrong %d" % size
)