blob: 2869ce76c6d81e534300bdba9d14f3ec142d693f [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
Raymond Hettingercb87bc82004-05-31 00:35:52 +00004from weakref import proxy
Fred Drake2ec80fa2000-10-23 16:59:35 +00005
Barry Warsaw408b6d32002-07-30 23:27:12 +00006from test.test_support import verify, TESTFN, TestFailed
Marc-André Lemburgfa44d792000-08-25 22:37:31 +00007from UserList import UserList
8
Raymond Hettingercb87bc82004-05-31 00:35:52 +00009# verify weak references
10f = file(TESTFN, 'w')
11p = proxy(f)
12p.write('teststring')
13verify(f.tell(), p.tell())
14f.close()
15f = None
16try:
17 p.tell()
18except ReferenceError:
19 pass
20else:
21 raise TestFailed('file proxy still exists when the file is gone')
22
Tim Peters015dd822003-05-04 04:16:52 +000023# verify expected attributes exist
24f = file(TESTFN, 'w')
25softspace = f.softspace
26f.name # merely shouldn't blow up
27f.mode # ditto
28f.closed # ditto
29
30# verify softspace is writable
31f.softspace = softspace # merely shouldn't blow up
32
33# verify the others aren't
34for attr in 'name', 'mode', 'closed':
35 try:
36 setattr(f, attr, 'oops')
Barry Warsawb180c062005-04-20 19:41:36 +000037 except (AttributeError, TypeError):
Tim Peters015dd822003-05-04 04:16:52 +000038 pass
39 else:
Barry Warsawb180c062005-04-20 19:41:36 +000040 raise TestFailed('expected exception setting file attr %r' % attr)
Tim Peters015dd822003-05-04 04:16:52 +000041f.close()
42
Skip Montanarobbf12ba2005-05-20 03:07:06 +000043# check invalid mode strings
44for mode in ("", "aU", "wU+"):
45 try:
46 f = file(TESTFN, mode)
47 except ValueError:
48 pass
49 else:
50 f.close()
51 raise TestFailed('%r is an invalid file mode' % mode)
52
Marc-André Lemburgfa44d792000-08-25 22:37:31 +000053# verify writelines with instance sequence
54l = UserList(['1', '2'])
55f = open(TESTFN, 'wb')
56f.writelines(l)
57f.close()
58f = open(TESTFN, 'rb')
59buf = f.read()
60f.close()
Marc-André Lemburg36619082001-01-17 19:11:13 +000061verify(buf == '12')
Marc-André Lemburgfa44d792000-08-25 22:37:31 +000062
Neal Norwitz62f5a9d2002-04-01 00:09:00 +000063# verify readinto
64a = array('c', 'x'*10)
65f = open(TESTFN, 'rb')
66n = f.readinto(a)
67f.close()
68verify(buf == a.tostring()[:n])
69
Marc-André Lemburgfa44d792000-08-25 22:37:31 +000070# verify writelines with integers
71f = open(TESTFN, 'wb')
72try:
73 f.writelines([1, 2, 3])
74except TypeError:
75 pass
76else:
77 print "writelines accepted sequence of integers"
78f.close()
79
80# verify writelines with integers in UserList
81f = open(TESTFN, 'wb')
82l = UserList([1,2,3])
83try:
84 f.writelines(l)
85except TypeError:
86 pass
87else:
88 print "writelines accepted sequence of integers"
89f.close()
90
91# verify writelines with non-string object
92class NonString: pass
93
94f = open(TESTFN, 'wb')
95try:
96 f.writelines([NonString(), NonString()])
97except TypeError:
98 pass
99else:
100 print "writelines accepted sequence of non-string objects"
101f.close()
Fred Drake2ec80fa2000-10-23 16:59:35 +0000102
Neal Norwitzfcf44352005-11-27 20:37:43 +0000103try:
104 sys.stdin.seek(0)
105except IOError:
106 pass
107else:
108 print "should not be able to seek on sys.stdin"
109
110try:
111 sys.stdin.tell()
112except IOError:
113 pass
114else:
115 print "should not be able to seek on sys.stdin"
116
117try:
118 sys.stdin.truncate()
119except IOError:
120 pass
121else:
122 print "should not be able to truncate on sys.stdin"
123
124# verify repr works
125f = open(TESTFN)
126if not repr(f).startswith("<open file '" + TESTFN):
127 print "repr(file) failed"
128f.close()
129
130# verify repr works for unicode too
131f = open(unicode(TESTFN))
132if not repr(f).startswith("<open file u'" + TESTFN):
133 print "repr(file with unicode name) failed"
134f.close()
135
Jeremy Hylton734c7fb2001-11-09 19:34:43 +0000136# verify that we get a sensible error message for bad mode argument
Jeremy Hylton41c83212001-11-09 16:17:24 +0000137bad_mode = "qwerty"
138try:
139 open(TESTFN, bad_mode)
140except IOError, msg:
Jeremy Hylton734c7fb2001-11-09 19:34:43 +0000141 if msg[0] != 0:
142 s = str(msg)
143 if s.find(TESTFN) != -1 or s.find(bad_mode) == -1:
144 print "bad error message for invalid mode: %s" % s
145 # if msg[0] == 0, we're probably on Windows where there may be
146 # no obvious way to discover why open() failed.
Jeremy Hylton41c83212001-11-09 16:17:24 +0000147else:
148 print "no error for invalid mode: %s" % bad_mode
149
Neal Norwitz653d85f2002-01-01 19:11:13 +0000150f = open(TESTFN)
151if f.name != TESTFN:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000152 raise TestFailed, 'file.name should be "%s"' % TESTFN
Neal Norwitz653d85f2002-01-01 19:11:13 +0000153if f.isatty():
Neal Norwitzb1295da2002-04-01 18:59:20 +0000154 raise TestFailed, 'file.isatty() should be false'
Neal Norwitz653d85f2002-01-01 19:11:13 +0000155
156if f.closed:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000157 raise TestFailed, 'file.closed should be false'
Neal Norwitz653d85f2002-01-01 19:11:13 +0000158
Neal Norwitz62f5a9d2002-04-01 00:09:00 +0000159try:
160 f.readinto("")
161except TypeError:
162 pass
163else:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000164 raise TestFailed, 'file.readinto("") should raise a TypeError'
Neal Norwitz62f5a9d2002-04-01 00:09:00 +0000165
Neal Norwitz653d85f2002-01-01 19:11:13 +0000166f.close()
167if not f.closed:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000168 raise TestFailed, 'file.closed should be true'
Neal Norwitz653d85f2002-01-01 19:11:13 +0000169
Andrew MacIntyre4e10ed32004-04-04 07:01:35 +0000170# make sure that explicitly setting the buffer size doesn't cause
171# misbehaviour especially with repeated close() calls
172for s in (-1, 0, 1, 512):
173 try:
174 f = open(TESTFN, 'w', s)
175 f.write(str(s))
176 f.close()
177 f.close()
178 f = open(TESTFN, 'r', s)
179 d = int(f.read())
180 f.close()
181 f.close()
182 except IOError, msg:
183 raise TestFailed, 'error setting buffer size %d: %s' % (s, str(msg))
184 if d != s:
185 raise TestFailed, 'readback failure using buffer size %d'
186
Guido van Rossum3c668c12002-08-06 15:58:24 +0000187methods = ['fileno', 'flush', 'isatty', 'next', 'read', 'readinto',
188 'readline', 'readlines', 'seek', 'tell', 'truncate', 'write',
189 'xreadlines', '__iter__']
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000190if sys.platform.startswith('atheos'):
191 methods.remove('truncate')
192
193for methodname in methods:
Neal Norwitz653d85f2002-01-01 19:11:13 +0000194 method = getattr(f, methodname)
195 try:
196 method()
197 except ValueError:
198 pass
199 else:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000200 raise TestFailed, 'file.%s() on a closed file should raise a ValueError' % methodname
Neal Norwitz653d85f2002-01-01 19:11:13 +0000201
202try:
203 f.writelines([])
204except ValueError:
205 pass
206else:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000207 raise TestFailed, 'file.writelines([]) on a closed file should raise a ValueError'
Neal Norwitz653d85f2002-01-01 19:11:13 +0000208
Fred Drake2ec80fa2000-10-23 16:59:35 +0000209os.unlink(TESTFN)
Tim Petersf1827cf2003-09-07 03:30:18 +0000210
211def bug801631():
212 # SF bug <http://www.python.org/sf/801631>
213 # "file.truncate fault on windows"
214 f = file(TESTFN, 'wb')
215 f.write('12345678901') # 11 bytes
216 f.close()
217
218 f = file(TESTFN,'rb+')
219 data = f.read(5)
220 if data != '12345':
221 raise TestFailed("Read on file opened for update failed %r" % data)
222 if f.tell() != 5:
223 raise TestFailed("File pos after read wrong %d" % f.tell())
224
225 f.truncate()
226 if f.tell() != 5:
227 raise TestFailed("File pos after ftruncate wrong %d" % f.tell())
228
229 f.close()
230 size = os.path.getsize(TESTFN)
231 if size != 5:
232 raise TestFailed("File size after ftruncate wrong %d" % size)
233
234try:
235 bug801631()
236finally:
237 os.unlink(TESTFN)