0001 import sys 0002 import os 0003 from array import array 0004 from weakref import proxy 0005 0006 from test.test_support import verify, TESTFN, TestFailed 0007 from UserList import UserList 0008 0009 # verify weak references 0010 f = file(TESTFN, 'w') 0011 p = proxy(f) 0012 p.write('teststring') 0013 verify(f.tell(), p.tell()) 0014 f.close() 0015 f = None 0016 try: 0017 p.tell() 0018 except ReferenceError: 0019 pass 0020 else: 0021 raise TestFailed('file proxy still exists when the file is gone') 0022 0023 # verify expected attributes exist 0024 f = file(TESTFN, 'w') 0025 softspace = f.softspace 0026 f.name # merely shouldn't blow up 0027 f.mode # ditto 0028 f.closed # ditto 0029 0030 # verify softspace is writable 0031 f.softspace = softspace # merely shouldn't blow up 0032 0033 # verify the others aren't 0034 for attr in 'name', 'mode', 'closed': 0035 try: 0036 setattr(f, attr, 'oops') 0037 except TypeError: 0038 pass 0039 else: 0040 raise TestFailed('expected TypeError setting file attr %r' % attr) 0041 f.close() 0042 0043 # verify writelines with instance sequence 0044 l = UserList(['1', '2']) 0045 f = open(TESTFN, 'wb') 0046 f.writelines(l) 0047 f.close() 0048 f = open(TESTFN, 'rb') 0049 buf = f.read() 0050 f.close() 0051 verify(buf == '12') 0052 0053 # verify readinto 0054 a = array('c', 'x'*10) 0055 f = open(TESTFN, 'rb') 0056 n = f.readinto(a) 0057 f.close() 0058 verify(buf == a.tostring()[:n]) 0059 0060 # verify writelines with integers 0061 f = open(TESTFN, 'wb') 0062 try: 0063 f.writelines([1, 2, 3]) 0064 except TypeError: 0065 pass 0066 else: 0067 print "writelines accepted sequence of integers" 0068 f.close() 0069 0070 # verify writelines with integers in UserList 0071 f = open(TESTFN, 'wb') 0072 l = UserList([1,2,3]) 0073 try: 0074 f.writelines(l) 0075 except TypeError: 0076 pass 0077 else: 0078 print "writelines accepted sequence of integers" 0079 f.close() 0080 0081 # verify writelines with non-string object 0082 class NonString: pass 0083 0084 f = open(TESTFN, 'wb') 0085 try: 0086 f.writelines([NonString(), NonString()]) 0087 except TypeError: 0088 pass 0089 else: 0090 print "writelines accepted sequence of non-string objects" 0091 f.close() 0092 0093 # verify that we get a sensible error message for bad mode argument 0094 bad_mode = "qwerty" 0095 try: 0096 open(TESTFN, bad_mode) 0097 except IOError, msg: 0098 if msg[0] != 0: 0099 s = str(msg) 0100 if s.find(TESTFN) != -1 or s.find(bad_mode) == -1: 0101 print "bad error message for invalid mode: %s" % s 0102 # if msg[0] == 0, we're probably on Windows where there may be 0103 # no obvious way to discover why open() failed. 0104 else: 0105 print "no error for invalid mode: %s" % bad_mode 0106 0107 f = open(TESTFN) 0108 if f.name != TESTFN: 0109 raise TestFailed, 'file.name should be "%s"' % TESTFN 0110 if f.isatty(): 0111 raise TestFailed, 'file.isatty() should be false' 0112 0113 if f.closed: 0114 raise TestFailed, 'file.closed should be false' 0115 0116 try: 0117 f.readinto("") 0118 except TypeError: 0119 pass 0120 else: 0121 raise TestFailed, 'file.readinto("") should raise a TypeError' 0122 0123 f.close() 0124 if not f.closed: 0125 raise TestFailed, 'file.closed should be true' 0126 0127 # make sure that explicitly setting the buffer size doesn't cause 0128 # misbehaviour especially with repeated close() calls 0129 for s in (-1, 0, 1, 512): 0130 try: 0131 f = open(TESTFN, 'w', s) 0132 f.write(str(s)) 0133 f.close() 0134 f.close() 0135 f = open(TESTFN, 'r', s) 0136 d = int(f.read()) 0137 f.close() 0138 f.close() 0139 except IOError, msg: 0140 raise TestFailed, 'error setting buffer size %d: %s' % (s, str(msg)) 0141 if d != s: 0142 raise TestFailed, 'readback failure using buffer size %d' 0143 0144 methods = ['fileno', 'flush', 'isatty', 'next', 'read', 'readinto', 0145 'readline', 'readlines', 'seek', 'tell', 'truncate', 'write', 0146 'xreadlines', '__iter__'] 0147 if sys.platform.startswith('atheos'): 0148 methods.remove('truncate') 0149 0150 for methodname in methods: 0151 method = getattr(f, methodname) 0152 try: 0153 method() 0154 except ValueError: 0155 pass 0156 else: 0157 raise TestFailed, 'file.%s() on a closed file should raise a ValueError' % methodname 0158 0159 try: 0160 f.writelines([]) 0161 except ValueError: 0162 pass 0163 else: 0164 raise TestFailed, 'file.writelines([]) on a closed file should raise a ValueError' 0165 0166 os.unlink(TESTFN) 0167 0168 def bug801631(): 0169 # SF bug <http://www.python.org/sf/801631> 0170 # "file.truncate fault on windows" 0171 f = file(TESTFN, 'wb') 0172 f.write('12345678901') # 11 bytes 0173 f.close() 0174 0175 f = file(TESTFN,'rb+') 0176 data = f.read(5) 0177 if data != '12345': 0178 raise TestFailed("Read on file opened for update failed %r" % data) 0179 if f.tell() != 5: 0180 raise TestFailed("File pos after read wrong %d" % f.tell()) 0181 0182 f.truncate() 0183 if f.tell() != 5: 0184 raise TestFailed("File pos after ftruncate wrong %d" % f.tell()) 0185 0186 f.close() 0187 size = os.path.getsize(TESTFN) 0188 if size != 5: 0189 raise TestFailed("File size after ftruncate wrong %d" % size) 0190 0191 try: 0192 bug801631() 0193 finally: 0194 os.unlink(TESTFN) 0195
Generated by PyXR 0.9.4