blob: 22db9a2920489506ff8128c82a94842715fb664b [file] [log] [blame]
Martin v. Löwisf90ae202002-06-11 06:22:31 +00001import sys
Fred Drake2ec80fa2000-10-23 16:59:35 +00002import os
Neal Norwitz62f5a9d2002-04-01 00:09:00 +00003from array import array
Fred Drake2ec80fa2000-10-23 16:59:35 +00004
Barry Warsaw408b6d32002-07-30 23:27:12 +00005from test.test_support import verify, TESTFN, TestFailed
Marc-André Lemburgfa44d792000-08-25 22:37:31 +00006from UserList import UserList
7
Tim Peters015dd822003-05-04 04:16:52 +00008# verify expected attributes exist
9f = file(TESTFN, 'w')
10softspace = f.softspace
11f.name # merely shouldn't blow up
12f.mode # ditto
13f.closed # ditto
14
15# verify softspace is writable
16f.softspace = softspace # merely shouldn't blow up
17
18# verify the others aren't
19for attr in 'name', 'mode', 'closed':
20 try:
21 setattr(f, attr, 'oops')
22 except TypeError:
23 pass
24 else:
25 raise TestFailed('expected TypeError setting file attr %r' % attr)
26f.close()
27
Marc-André Lemburgfa44d792000-08-25 22:37:31 +000028# verify writelines with instance sequence
29l = UserList(['1', '2'])
30f = open(TESTFN, 'wb')
31f.writelines(l)
32f.close()
33f = open(TESTFN, 'rb')
34buf = f.read()
35f.close()
Marc-André Lemburg36619082001-01-17 19:11:13 +000036verify(buf == '12')
Marc-André Lemburgfa44d792000-08-25 22:37:31 +000037
Neal Norwitz62f5a9d2002-04-01 00:09:00 +000038# verify readinto
39a = array('c', 'x'*10)
40f = open(TESTFN, 'rb')
41n = f.readinto(a)
42f.close()
43verify(buf == a.tostring()[:n])
44
Marc-André Lemburgfa44d792000-08-25 22:37:31 +000045# verify writelines with integers
46f = open(TESTFN, 'wb')
47try:
48 f.writelines([1, 2, 3])
49except TypeError:
50 pass
51else:
52 print "writelines accepted sequence of integers"
53f.close()
54
55# verify writelines with integers in UserList
56f = open(TESTFN, 'wb')
57l = UserList([1,2,3])
58try:
59 f.writelines(l)
60except TypeError:
61 pass
62else:
63 print "writelines accepted sequence of integers"
64f.close()
65
66# verify writelines with non-string object
67class NonString: pass
68
69f = open(TESTFN, 'wb')
70try:
71 f.writelines([NonString(), NonString()])
72except TypeError:
73 pass
74else:
75 print "writelines accepted sequence of non-string objects"
76f.close()
Fred Drake2ec80fa2000-10-23 16:59:35 +000077
Jeremy Hylton734c7fb2001-11-09 19:34:43 +000078# verify that we get a sensible error message for bad mode argument
Jeremy Hylton41c83212001-11-09 16:17:24 +000079bad_mode = "qwerty"
80try:
81 open(TESTFN, bad_mode)
82except IOError, msg:
Jeremy Hylton734c7fb2001-11-09 19:34:43 +000083 if msg[0] != 0:
84 s = str(msg)
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.
Jeremy Hylton41c83212001-11-09 16:17:24 +000089else:
90 print "no error for invalid mode: %s" % bad_mode
91
Neal Norwitz653d85f2002-01-01 19:11:13 +000092f = open(TESTFN)
93if f.name != TESTFN:
Neal Norwitzb1295da2002-04-01 18:59:20 +000094 raise TestFailed, 'file.name should be "%s"' % TESTFN
Neal Norwitz653d85f2002-01-01 19:11:13 +000095if f.isatty():
Neal Norwitzb1295da2002-04-01 18:59:20 +000096 raise TestFailed, 'file.isatty() should be false'
Neal Norwitz653d85f2002-01-01 19:11:13 +000097
98if f.closed:
Neal Norwitzb1295da2002-04-01 18:59:20 +000099 raise TestFailed, 'file.closed should be false'
Neal Norwitz653d85f2002-01-01 19:11:13 +0000100
Neal Norwitz62f5a9d2002-04-01 00:09:00 +0000101try:
102 f.readinto("")
103except TypeError:
104 pass
105else:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000106 raise TestFailed, 'file.readinto("") should raise a TypeError'
Neal Norwitz62f5a9d2002-04-01 00:09:00 +0000107
Neal Norwitz653d85f2002-01-01 19:11:13 +0000108f.close()
109if not f.closed:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000110 raise TestFailed, 'file.closed should be true'
Neal Norwitz653d85f2002-01-01 19:11:13 +0000111
Andrew MacIntyre4e10ed32004-04-04 07:01:35 +0000112# make sure that explicitly setting the buffer size doesn't cause
113# misbehaviour especially with repeated close() calls
114for s in (-1, 0, 1, 512):
115 try:
116 f = open(TESTFN, 'w', s)
117 f.write(str(s))
118 f.close()
119 f.close()
120 f = open(TESTFN, 'r', s)
121 d = int(f.read())
122 f.close()
123 f.close()
124 except IOError, msg:
125 raise TestFailed, 'error setting buffer size %d: %s' % (s, str(msg))
126 if d != s:
127 raise TestFailed, 'readback failure using buffer size %d'
128
Guido van Rossum3c668c12002-08-06 15:58:24 +0000129methods = ['fileno', 'flush', 'isatty', 'next', 'read', 'readinto',
130 'readline', 'readlines', 'seek', 'tell', 'truncate', 'write',
131 'xreadlines', '__iter__']
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000132if sys.platform.startswith('atheos'):
133 methods.remove('truncate')
134
135for methodname in methods:
Neal Norwitz653d85f2002-01-01 19:11:13 +0000136 method = getattr(f, methodname)
137 try:
138 method()
139 except ValueError:
140 pass
141 else:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000142 raise TestFailed, 'file.%s() on a closed file should raise a ValueError' % methodname
Neal Norwitz653d85f2002-01-01 19:11:13 +0000143
144try:
145 f.writelines([])
146except ValueError:
147 pass
148else:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000149 raise TestFailed, 'file.writelines([]) on a closed file should raise a ValueError'
Neal Norwitz653d85f2002-01-01 19:11:13 +0000150
Fred Drake2ec80fa2000-10-23 16:59:35 +0000151os.unlink(TESTFN)
Tim Petersf1827cf2003-09-07 03:30:18 +0000152
153def bug801631():
154 # SF bug <http://www.python.org/sf/801631>
155 # "file.truncate fault on windows"
156 f = file(TESTFN, 'wb')
157 f.write('12345678901') # 11 bytes
158 f.close()
159
160 f = file(TESTFN,'rb+')
161 data = f.read(5)
162 if data != '12345':
163 raise TestFailed("Read on file opened for update failed %r" % data)
164 if f.tell() != 5:
165 raise TestFailed("File pos after read wrong %d" % f.tell())
166
167 f.truncate()
168 if f.tell() != 5:
169 raise TestFailed("File pos after ftruncate wrong %d" % f.tell())
170
171 f.close()
172 size = os.path.getsize(TESTFN)
173 if size != 5:
174 raise TestFailed("File size after ftruncate wrong %d" % size)
175
176try:
177 bug801631()
178finally:
179 os.unlink(TESTFN)