Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 1 | from test.test_support import verify, TESTFN |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 2 | import sys, os |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 3 | import gzip |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 5 | filename = TESTFN |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 6 | |
| 7 | data1 = """ int length=DEFAULTALLOC, err = Z_OK; |
| 8 | PyObject *RetVal; |
| 9 | int flushmode = Z_FINISH; |
| 10 | unsigned long start_total_out; |
| 11 | |
| 12 | """ |
| 13 | |
| 14 | data2 = """/* zlibmodule.c -- gzip-compatible data compression */ |
Neal Norwitz | 014f103 | 2004-07-29 03:55:56 +0000 | [diff] [blame] | 15 | /* See http://www.gzip.org/zlib/ |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 16 | /* See http://www.winimage.com/zLibDll for Windows */ |
| 17 | """ |
| 18 | |
Tim Peters | 5cfb05e | 2004-07-27 21:02:02 +0000 | [diff] [blame] | 19 | f = gzip.GzipFile(filename, 'wb') ; f.write(data1 * 50) |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 20 | |
Tim Peters | 5cfb05e | 2004-07-27 21:02:02 +0000 | [diff] [blame] | 21 | # Try flush and fileno. |
| 22 | f.flush() |
| 23 | f.fileno() |
| 24 | if hasattr(os, 'fsync'): |
| 25 | os.fsync(f.fileno()) |
| 26 | f.close() |
| 27 | |
| 28 | # Try reading. |
Skip Montanaro | 12424bc | 2002-05-23 01:43:05 +0000 | [diff] [blame] | 29 | f = gzip.GzipFile(filename, 'r') ; d = f.read() ; f.close() |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 30 | verify(d == data1*50) |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 31 | |
| 32 | # Append to the previous file |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 33 | f = gzip.GzipFile(filename, 'ab') ; f.write(data2 * 15) ; f.close() |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | 052364b | 1999-04-07 19:00:58 +0000 | [diff] [blame] | 35 | f = gzip.GzipFile(filename, 'rb') ; d = f.read() ; f.close() |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 36 | verify(d == (data1*50) + (data2*15)) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 37 | |
| 38 | # Try .readline() with varying line lengths |
| 39 | |
| 40 | f = gzip.GzipFile(filename, 'rb') |
| 41 | line_length = 0 |
| 42 | while 1: |
Guido van Rossum | f27cc5b | 2001-01-17 21:43:06 +0000 | [diff] [blame] | 43 | L = f.readline(line_length) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 44 | if L == "" and line_length != 0: break |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 45 | verify(len(L) <= line_length) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 46 | line_length = (line_length + 1) % 50 |
| 47 | f.close() |
| 48 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 49 | # Try .readlines() |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 50 | |
| 51 | f = gzip.GzipFile(filename, 'rb') |
| 52 | L = f.readlines() |
| 53 | f.close() |
| 54 | |
| 55 | f = gzip.GzipFile(filename, 'rb') |
| 56 | while 1: |
| 57 | L = f.readlines(150) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 58 | if L == []: break |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 59 | f.close() |
| 60 | |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 61 | # Try seek, read test |
| 62 | |
| 63 | f = gzip.GzipFile(filename) |
| 64 | while 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 |
| 77 | f.close() |
| 78 | |
| 79 | # Try seek, write test |
| 80 | f = gzip.GzipFile(filename, 'w') |
| 81 | for pos in range(0, 256, 16): |
Tim Peters | ab9ba27 | 2001-08-09 21:40:30 +0000 | [diff] [blame] | 82 | f.seek(pos) |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 83 | f.write('GZ\n') |
| 84 | f.close() |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 85 | |
Skip Montanaro | 12424bc | 2002-05-23 01:43:05 +0000 | [diff] [blame] | 86 | f = gzip.GzipFile(filename, 'r') |
| 87 | verify(f.myfileobj.mode == 'rb') |
| 88 | f.close() |
| 89 | |
Guido van Rossum | f27cc5b | 2001-01-17 21:43:06 +0000 | [diff] [blame] | 90 | os.unlink(filename) |