blob: b8bcab7bd8f7db643aa26bd586be0d0551100a3e [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
Guido van Rossum3c668c12002-08-06 15:58:24 +0000112methods = ['fileno', 'flush', 'isatty', 'next', 'read', 'readinto',
113 'readline', 'readlines', 'seek', 'tell', 'truncate', 'write',
114 'xreadlines', '__iter__']
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000115if sys.platform.startswith('atheos'):
116 methods.remove('truncate')
117
118for methodname in methods:
Neal Norwitz653d85f2002-01-01 19:11:13 +0000119 method = getattr(f, methodname)
120 try:
121 method()
122 except ValueError:
123 pass
124 else:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000125 raise TestFailed, 'file.%s() on a closed file should raise a ValueError' % methodname
Neal Norwitz653d85f2002-01-01 19:11:13 +0000126
127try:
128 f.writelines([])
129except ValueError:
130 pass
131else:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000132 raise TestFailed, 'file.writelines([]) on a closed file should raise a ValueError'
Neal Norwitz653d85f2002-01-01 19:11:13 +0000133
Fred Drake2ec80fa2000-10-23 16:59:35 +0000134os.unlink(TESTFN)
Tim Petersf1827cf2003-09-07 03:30:18 +0000135
136def bug801631():
137 # SF bug <http://www.python.org/sf/801631>
138 # "file.truncate fault on windows"
139 f = file(TESTFN, 'wb')
140 f.write('12345678901') # 11 bytes
141 f.close()
142
143 f = file(TESTFN,'rb+')
144 data = f.read(5)
145 if data != '12345':
146 raise TestFailed("Read on file opened for update failed %r" % data)
147 if f.tell() != 5:
148 raise TestFailed("File pos after read wrong %d" % f.tell())
149
150 f.truncate()
151 if f.tell() != 5:
152 raise TestFailed("File pos after ftruncate wrong %d" % f.tell())
153
154 f.close()
155 size = os.path.getsize(TESTFN)
156 if size != 5:
157 raise TestFailed("File size after ftruncate wrong %d" % size)
158
159try:
160 bug801631()
161finally:
162 os.unlink(TESTFN)