blob: 261db2ccb6216236d52b076129dad2f55810620e [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
Neal Norwitzb1295da2002-04-01 18:59:20 +00005from test_support import verify, TESTFN, TestFailed
Marc-André Lemburgfa44d792000-08-25 22:37:31 +00006from UserList import UserList
7
8# verify writelines with instance sequence
9l = UserList(['1', '2'])
10f = open(TESTFN, 'wb')
11f.writelines(l)
12f.close()
13f = open(TESTFN, 'rb')
14buf = f.read()
15f.close()
Marc-André Lemburg36619082001-01-17 19:11:13 +000016verify(buf == '12')
Marc-André Lemburgfa44d792000-08-25 22:37:31 +000017
Neal Norwitz62f5a9d2002-04-01 00:09:00 +000018# verify readinto
19a = array('c', 'x'*10)
20f = open(TESTFN, 'rb')
21n = f.readinto(a)
22f.close()
23verify(buf == a.tostring()[:n])
24
Marc-André Lemburgfa44d792000-08-25 22:37:31 +000025# verify writelines with integers
26f = open(TESTFN, 'wb')
27try:
28 f.writelines([1, 2, 3])
29except TypeError:
30 pass
31else:
32 print "writelines accepted sequence of integers"
33f.close()
34
35# verify writelines with integers in UserList
36f = open(TESTFN, 'wb')
37l = UserList([1,2,3])
38try:
39 f.writelines(l)
40except TypeError:
41 pass
42else:
43 print "writelines accepted sequence of integers"
44f.close()
45
46# verify writelines with non-string object
47class NonString: pass
48
49f = open(TESTFN, 'wb')
50try:
51 f.writelines([NonString(), NonString()])
52except TypeError:
53 pass
54else:
55 print "writelines accepted sequence of non-string objects"
56f.close()
Fred Drake2ec80fa2000-10-23 16:59:35 +000057
Jeremy Hylton734c7fb2001-11-09 19:34:43 +000058# verify that we get a sensible error message for bad mode argument
Jeremy Hylton41c83212001-11-09 16:17:24 +000059bad_mode = "qwerty"
60try:
61 open(TESTFN, bad_mode)
62except IOError, msg:
Jeremy Hylton734c7fb2001-11-09 19:34:43 +000063 if msg[0] != 0:
64 s = str(msg)
65 if s.find(TESTFN) != -1 or s.find(bad_mode) == -1:
66 print "bad error message for invalid mode: %s" % s
67 # if msg[0] == 0, we're probably on Windows where there may be
68 # no obvious way to discover why open() failed.
Jeremy Hylton41c83212001-11-09 16:17:24 +000069else:
70 print "no error for invalid mode: %s" % bad_mode
71
Neal Norwitz653d85f2002-01-01 19:11:13 +000072f = open(TESTFN)
73if f.name != TESTFN:
Neal Norwitzb1295da2002-04-01 18:59:20 +000074 raise TestFailed, 'file.name should be "%s"' % TESTFN
Neal Norwitz653d85f2002-01-01 19:11:13 +000075if f.isatty():
Neal Norwitzb1295da2002-04-01 18:59:20 +000076 raise TestFailed, 'file.isatty() should be false'
Neal Norwitz653d85f2002-01-01 19:11:13 +000077
78if f.closed:
Neal Norwitzb1295da2002-04-01 18:59:20 +000079 raise TestFailed, 'file.closed should be false'
Neal Norwitz653d85f2002-01-01 19:11:13 +000080
Neal Norwitz62f5a9d2002-04-01 00:09:00 +000081try:
82 f.readinto("")
83except TypeError:
84 pass
85else:
Neal Norwitzb1295da2002-04-01 18:59:20 +000086 raise TestFailed, 'file.readinto("") should raise a TypeError'
Neal Norwitz62f5a9d2002-04-01 00:09:00 +000087
Neal Norwitz653d85f2002-01-01 19:11:13 +000088f.close()
89if not f.closed:
Neal Norwitzb1295da2002-04-01 18:59:20 +000090 raise TestFailed, 'file.closed should be true'
Neal Norwitz653d85f2002-01-01 19:11:13 +000091
Martin v. Löwisf90ae202002-06-11 06:22:31 +000092methods = ['fileno', 'flush', 'isatty', 'read', 'readinto', 'readline', 'readlines', 'seek', 'tell', 'truncate', 'write', 'xreadlines' ]
93if sys.platform.startswith('atheos'):
94 methods.remove('truncate')
95
96for methodname in methods:
Neal Norwitz653d85f2002-01-01 19:11:13 +000097 method = getattr(f, methodname)
98 try:
99 method()
100 except ValueError:
101 pass
102 else:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000103 raise TestFailed, 'file.%s() on a closed file should raise a ValueError' % methodname
Neal Norwitz653d85f2002-01-01 19:11:13 +0000104
105try:
106 f.writelines([])
107except ValueError:
108 pass
109else:
Neal Norwitzb1295da2002-04-01 18:59:20 +0000110 raise TestFailed, 'file.writelines([]) on a closed file should raise a ValueError'
Neal Norwitz653d85f2002-01-01 19:11:13 +0000111
Fred Drake2ec80fa2000-10-23 16:59:35 +0000112os.unlink(TESTFN)