blob: 2d2d9c1c219ab11bac954ca9f6afa8057896a666 [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
Jeremy Hylton734c7fb2001-11-09 19:34:43 +0000103# verify that we get a sensible error message for bad mode argument
Jeremy Hylton41c83212001-11-09 16:17:24 +0000104bad_mode = "qwerty"
105try:
106 open(TESTFN, bad_mode)
107except IOError, msg:
Jeremy Hylton734c7fb2001-11-09 19:34:43 +0000108 if msg[0] != 0:
109 s = str(msg)
110 if s.find(TESTFN) != -1 or s.find(bad_mode) == -1:
111 print "bad error message for invalid mode: %s" % s
112 # if msg[0] == 0, we're probably on Windows where there may be
113 # no obvious way to discover why open() failed.
Jeremy Hylton41c83212001-11-09 16:17:24 +0000114else:
115 print "no error for invalid mode: %s" % bad_mode
116
Neal Norwitz653d85f2002-01-01 19:11:13 +0000117f = open(TESTFN)
118if f.name != TESTFN:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000119 raise TestFailed, 'file.name should be "%s"' % TESTFN
Neal Norwitz653d85f2002-01-01 19:11:13 +0000120if f.isatty():
Neal Norwitzb1295da2002-04-01 18:59:20 +0000121 raise TestFailed, 'file.isatty() should be false'
Neal Norwitz653d85f2002-01-01 19:11:13 +0000122
123if f.closed:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000124 raise TestFailed, 'file.closed should be false'
Neal Norwitz653d85f2002-01-01 19:11:13 +0000125
Neal Norwitz62f5a9d2002-04-01 00:09:00 +0000126try:
127 f.readinto("")
128except TypeError:
129 pass
130else:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000131 raise TestFailed, 'file.readinto("") should raise a TypeError'
Neal Norwitz62f5a9d2002-04-01 00:09:00 +0000132
Neal Norwitz653d85f2002-01-01 19:11:13 +0000133f.close()
134if not f.closed:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000135 raise TestFailed, 'file.closed should be true'
Neal Norwitz653d85f2002-01-01 19:11:13 +0000136
Andrew MacIntyre4e10ed32004-04-04 07:01:35 +0000137# make sure that explicitly setting the buffer size doesn't cause
138# misbehaviour especially with repeated close() calls
139for s in (-1, 0, 1, 512):
140 try:
141 f = open(TESTFN, 'w', s)
142 f.write(str(s))
143 f.close()
144 f.close()
145 f = open(TESTFN, 'r', s)
146 d = int(f.read())
147 f.close()
148 f.close()
149 except IOError, msg:
150 raise TestFailed, 'error setting buffer size %d: %s' % (s, str(msg))
151 if d != s:
152 raise TestFailed, 'readback failure using buffer size %d'
153
Guido van Rossum3c668c12002-08-06 15:58:24 +0000154methods = ['fileno', 'flush', 'isatty', 'next', 'read', 'readinto',
155 'readline', 'readlines', 'seek', 'tell', 'truncate', 'write',
156 'xreadlines', '__iter__']
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000157if sys.platform.startswith('atheos'):
158 methods.remove('truncate')
159
160for methodname in methods:
Neal Norwitz653d85f2002-01-01 19:11:13 +0000161 method = getattr(f, methodname)
162 try:
163 method()
164 except ValueError:
165 pass
166 else:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000167 raise TestFailed, 'file.%s() on a closed file should raise a ValueError' % methodname
Neal Norwitz653d85f2002-01-01 19:11:13 +0000168
169try:
170 f.writelines([])
171except ValueError:
172 pass
173else:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000174 raise TestFailed, 'file.writelines([]) on a closed file should raise a ValueError'
Neal Norwitz653d85f2002-01-01 19:11:13 +0000175
Fred Drake2ec80fa2000-10-23 16:59:35 +0000176os.unlink(TESTFN)
Tim Petersf1827cf2003-09-07 03:30:18 +0000177
178def bug801631():
179 # SF bug <http://www.python.org/sf/801631>
180 # "file.truncate fault on windows"
181 f = file(TESTFN, 'wb')
182 f.write('12345678901') # 11 bytes
183 f.close()
184
185 f = file(TESTFN,'rb+')
186 data = f.read(5)
187 if data != '12345':
188 raise TestFailed("Read on file opened for update failed %r" % data)
189 if f.tell() != 5:
190 raise TestFailed("File pos after read wrong %d" % f.tell())
191
192 f.truncate()
193 if f.tell() != 5:
194 raise TestFailed("File pos after ftruncate wrong %d" % f.tell())
195
196 f.close()
197 size = os.path.getsize(TESTFN)
198 if size != 5:
199 raise TestFailed("File size after ftruncate wrong %d" % size)
200
201try:
202 bug801631()
203finally:
204 os.unlink(TESTFN)