blob: 33a923d78c1add4dec0000f04e79eb5c0ddfa4c6 [file] [log] [blame]
Fred Drake2ec80fa2000-10-23 16:59:35 +00001import os
Neal Norwitz62f5a9d2002-04-01 00:09:00 +00002from array import array
Fred Drake2ec80fa2000-10-23 16:59:35 +00003
Marc-André Lemburg36619082001-01-17 19:11:13 +00004from test_support import verify, TESTFN
Marc-André Lemburgfa44d792000-08-25 22:37:31 +00005from UserList import UserList
6
7# verify writelines with instance sequence
8l = UserList(['1', '2'])
9f = open(TESTFN, 'wb')
10f.writelines(l)
11f.close()
12f = open(TESTFN, 'rb')
13buf = f.read()
14f.close()
Marc-André Lemburg36619082001-01-17 19:11:13 +000015verify(buf == '12')
Marc-André Lemburgfa44d792000-08-25 22:37:31 +000016
Neal Norwitz62f5a9d2002-04-01 00:09:00 +000017# verify readinto
18a = array('c', 'x'*10)
19f = open(TESTFN, 'rb')
20n = f.readinto(a)
21f.close()
22verify(buf == a.tostring()[:n])
23
Marc-André Lemburgfa44d792000-08-25 22:37:31 +000024# verify writelines with integers
25f = open(TESTFN, 'wb')
26try:
27 f.writelines([1, 2, 3])
28except TypeError:
29 pass
30else:
31 print "writelines accepted sequence of integers"
32f.close()
33
34# verify writelines with integers in UserList
35f = open(TESTFN, 'wb')
36l = UserList([1,2,3])
37try:
38 f.writelines(l)
39except TypeError:
40 pass
41else:
42 print "writelines accepted sequence of integers"
43f.close()
44
45# verify writelines with non-string object
46class NonString: pass
47
48f = open(TESTFN, 'wb')
49try:
50 f.writelines([NonString(), NonString()])
51except TypeError:
52 pass
53else:
54 print "writelines accepted sequence of non-string objects"
55f.close()
Fred Drake2ec80fa2000-10-23 16:59:35 +000056
Jeremy Hylton734c7fb2001-11-09 19:34:43 +000057# verify that we get a sensible error message for bad mode argument
Jeremy Hylton41c83212001-11-09 16:17:24 +000058bad_mode = "qwerty"
59try:
60 open(TESTFN, bad_mode)
61except IOError, msg:
Jeremy Hylton734c7fb2001-11-09 19:34:43 +000062 if msg[0] != 0:
63 s = str(msg)
64 if s.find(TESTFN) != -1 or s.find(bad_mode) == -1:
65 print "bad error message for invalid mode: %s" % s
66 # if msg[0] == 0, we're probably on Windows where there may be
67 # no obvious way to discover why open() failed.
Jeremy Hylton41c83212001-11-09 16:17:24 +000068else:
69 print "no error for invalid mode: %s" % bad_mode
70
Neal Norwitz653d85f2002-01-01 19:11:13 +000071f = open(TESTFN)
72if f.name != TESTFN:
73 raise TestError, 'file.name should be "%s"' % TESTFN
74if f.isatty():
75 raise TestError, 'file.isatty() should be false'
76
77if f.closed:
78 raise TestError, 'file.closed should be false'
79
Neal Norwitz62f5a9d2002-04-01 00:09:00 +000080try:
81 f.readinto("")
82except TypeError:
83 pass
84else:
85 raise TestError, 'file.readinto("") should raise a TypeError'
86
Neal Norwitz653d85f2002-01-01 19:11:13 +000087f.close()
88if not f.closed:
89 raise TestError, 'file.closed should be true'
90
91for methodname in ['fileno', 'flush', 'isatty', 'read', 'readinto', 'readline', 'readlines', 'seek', 'tell', 'truncate', 'write', 'xreadlines' ]:
92 method = getattr(f, methodname)
93 try:
94 method()
95 except ValueError:
96 pass
97 else:
98 raise TestError, 'file.%s() on a closed file should raise a ValueError' % methodname
99
100try:
101 f.writelines([])
102except ValueError:
103 pass
104else:
105 raise TestError, 'file.writelines([]) on a closed file should raise a ValueError'
106
Fred Drake2ec80fa2000-10-23 16:59:35 +0000107os.unlink(TESTFN)