0001 from test.test_support import verify, TESTFN 0002 import sys, os 0003 import gzip 0004 0005 filename = TESTFN 0006 0007 data1 = """ int length=DEFAULTALLOC, err = Z_OK; 0008 PyObject *RetVal; 0009 int flushmode = Z_FINISH; 0010 unsigned long start_total_out; 0011 0012 """ 0013 0014 data2 = """/* zlibmodule.c -- gzip-compatible data compression */ 0015 /* See http://www.gzip.org/zlib/ 0016 /* See http://www.winimage.com/zLibDll for Windows */ 0017 """ 0018 0019 f = gzip.GzipFile(filename, 'wb') ; f.write(data1 * 50) 0020 0021 # Try flush and fileno. 0022 f.flush() 0023 f.fileno() 0024 if hasattr(os, 'fsync'): 0025 os.fsync(f.fileno()) 0026 f.close() 0027 0028 # Try reading. 0029 f = gzip.GzipFile(filename, 'r') ; d = f.read() ; f.close() 0030 verify(d == data1*50) 0031 0032 # Append to the previous file 0033 f = gzip.GzipFile(filename, 'ab') ; f.write(data2 * 15) ; f.close() 0034 0035 f = gzip.GzipFile(filename, 'rb') ; d = f.read() ; f.close() 0036 verify(d == (data1*50) + (data2*15)) 0037 0038 # Try .readline() with varying line lengths 0039 0040 f = gzip.GzipFile(filename, 'rb') 0041 line_length = 0 0042 while 1: 0043 L = f.readline(line_length) 0044 if L == "" and line_length != 0: break 0045 verify(len(L) <= line_length) 0046 line_length = (line_length + 1) % 50 0047 f.close() 0048 0049 # Try .readlines() 0050 0051 f = gzip.GzipFile(filename, 'rb') 0052 L = f.readlines() 0053 f.close() 0054 0055 f = gzip.GzipFile(filename, 'rb') 0056 while 1: 0057 L = f.readlines(150) 0058 if L == []: break 0059 f.close() 0060 0061 # Try seek, read test 0062 0063 f = gzip.GzipFile(filename) 0064 while 1: 0065 oldpos = f.tell() 0066 line1 = f.readline() 0067 if not line1: break 0068 newpos = f.tell() 0069 f.seek(oldpos) # negative seek 0070 if len(line1)>10: 0071 amount = 10 0072 else: 0073 amount = len(line1) 0074 line2 = f.read(amount) 0075 verify(line1[:amount] == line2) 0076 f.seek(newpos) # positive seek 0077 f.close() 0078 0079 # Try seek, write test 0080 f = gzip.GzipFile(filename, 'w') 0081 for pos in range(0, 256, 16): 0082 f.seek(pos) 0083 f.write('GZ\n') 0084 f.close() 0085 0086 f = gzip.GzipFile(filename, 'r') 0087 verify(f.myfileobj.mode == 'rb') 0088 f.close() 0089 0090 os.unlink(filename) 0091
Generated by PyXR 0.9.4