blob: 41abc5a33e5c591a1711124c657451ebf33d6034 [file] [log] [blame]
Guido van Rossum3b0a3292002-08-09 16:38:32 +00001from test.test_support import verify, TESTFN
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +00002import sys, os
Guido van Rossum3b0a3292002-08-09 16:38:32 +00003import gzip
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +00004
Guido van Rossum3b0a3292002-08-09 16:38:32 +00005filename = TESTFN
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +00006
7data1 = """ int length=DEFAULTALLOC, err = Z_OK;
8 PyObject *RetVal;
9 int flushmode = Z_FINISH;
10 unsigned long start_total_out;
11
12"""
13
14data2 = """/* zlibmodule.c -- gzip-compatible data compression */
Neal Norwitz014f1032004-07-29 03:55:56 +000015/* See http://www.gzip.org/zlib/
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000016/* See http://www.winimage.com/zLibDll for Windows */
17"""
18
Tim Peters5cfb05e2004-07-27 21:02:02 +000019f = gzip.GzipFile(filename, 'wb') ; f.write(data1 * 50)
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000020
Tim Peters5cfb05e2004-07-27 21:02:02 +000021# Try flush and fileno.
22f.flush()
23f.fileno()
24if hasattr(os, 'fsync'):
25 os.fsync(f.fileno())
26f.close()
27
28# Try reading.
Skip Montanaro12424bc2002-05-23 01:43:05 +000029f = gzip.GzipFile(filename, 'r') ; d = f.read() ; f.close()
Marc-André Lemburg36619082001-01-17 19:11:13 +000030verify(d == data1*50)
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000031
32# Append to the previous file
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000033f = gzip.GzipFile(filename, 'ab') ; f.write(data2 * 15) ; f.close()
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000034
Guido van Rossum052364b1999-04-07 19:00:58 +000035f = gzip.GzipFile(filename, 'rb') ; d = f.read() ; f.close()
Marc-André Lemburg36619082001-01-17 19:11:13 +000036verify(d == (data1*50) + (data2*15))
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000037
38# Try .readline() with varying line lengths
39
40f = gzip.GzipFile(filename, 'rb')
41line_length = 0
42while 1:
Guido van Rossumf27cc5b2001-01-17 21:43:06 +000043 L = f.readline(line_length)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000044 if L == "" and line_length != 0: break
Marc-André Lemburg36619082001-01-17 19:11:13 +000045 verify(len(L) <= line_length)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000046 line_length = (line_length + 1) % 50
47f.close()
48
Fred Drake004d5e62000-10-23 17:22:08 +000049# Try .readlines()
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000050
51f = gzip.GzipFile(filename, 'rb')
52L = f.readlines()
53f.close()
54
55f = gzip.GzipFile(filename, 'rb')
56while 1:
57 L = f.readlines(150)
Fred Drake004d5e62000-10-23 17:22:08 +000058 if L == []: break
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000059f.close()
60
Martin v. Löwis8cc965c2001-08-09 07:21:56 +000061# Try seek, read test
62
63f = gzip.GzipFile(filename)
64while 1:
65 oldpos = f.tell()
66 line1 = f.readline()
67 if not line1: break
68 newpos = f.tell()
69 f.seek(oldpos) # negative seek
70 if len(line1)>10:
71 amount = 10
72 else:
73 amount = len(line1)
74 line2 = f.read(amount)
75 verify(line1[:amount] == line2)
76 f.seek(newpos) # positive seek
77f.close()
78
79# Try seek, write test
80f = gzip.GzipFile(filename, 'w')
81for pos in range(0, 256, 16):
Tim Petersab9ba272001-08-09 21:40:30 +000082 f.seek(pos)
Martin v. Löwis8cc965c2001-08-09 07:21:56 +000083 f.write('GZ\n')
84f.close()
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000085
Skip Montanaro12424bc2002-05-23 01:43:05 +000086f = gzip.GzipFile(filename, 'r')
87verify(f.myfileobj.mode == 'rb')
88f.close()
89
Guido van Rossumf27cc5b2001-01-17 21:43:06 +000090os.unlink(filename)