from weakref
import proxy
from test
.test_support
import verify
, TESTFN
, TestFailed
from UserList
import UserList
verify(f
.tell(), p
.tell())
raise TestFailed('file proxy still exists when the file is gone')
# verify expected attributes exist
f
.name
# merely shouldn't blow up
# verify softspace is writable
f
.softspace
= softspace
# merely shouldn't blow up
# verify the others aren't
for attr
in 'name', 'mode', 'closed':
raise TestFailed('expected TypeError setting file attr %r' % attr
)
# verify writelines with instance sequence
verify(buf
== a
.tostring()[:n
])
# verify writelines with integers
print "writelines accepted sequence of integers"
# verify writelines with integers in UserList
print "writelines accepted sequence of integers"
# verify writelines with non-string object
f
.writelines([NonString(), NonString()])
print "writelines accepted sequence of non-string objects"
# verify that we get a sensible error message for bad mode argument
if s
.find(TESTFN
) != -1 or s
.find(bad_mode
) == -1:
print "bad error message for invalid mode: %s" % s
# if msg[0] == 0, we're probably on Windows where there may be
# no obvious way to discover why open() failed.
print "no error for invalid mode: %s" % bad_mode
raise TestFailed
, 'file.name should be "%s"' % TESTFN
raise TestFailed
, 'file.isatty() should be false'
raise TestFailed
, 'file.closed should be false'
raise TestFailed
, 'file.readinto("") should raise a TypeError'
raise TestFailed
, 'file.closed should be true'
# make sure that explicitly setting the buffer size doesn't cause
# misbehaviour especially with repeated close() calls
for s
in (-1, 0, 1, 512):
raise TestFailed
, 'error setting buffer size %d: %s' % (s
, str(msg
))
raise TestFailed
, 'readback failure using buffer size %d'
methods
= ['fileno', 'flush', 'isatty', 'next', 'read', 'readinto',
'readline', 'readlines', 'seek', 'tell', 'truncate', 'write',
'xreadlines', '__iter__']
if sys
.platform
.startswith('atheos'):
methods
.remove('truncate')
for methodname
in methods
:
method
= getattr(f
, methodname
)
raise TestFailed
, 'file.%s() on a closed file should raise a ValueError' % methodname
raise TestFailed
, 'file.writelines([]) on a closed file should raise a ValueError'
# SF bug <http://www.python.org/sf/801631>
# "file.truncate fault on windows"
f
.write('12345678901') # 11 bytes
raise TestFailed("Read on file opened for update failed %r" % data
)
raise TestFailed("File pos after read wrong %d" % f
.tell())
raise TestFailed("File pos after ftruncate wrong %d" % f
.tell())
size
= os
.path
.getsize(TESTFN
)
raise TestFailed("File size after ftruncate wrong %d" % size
)