annotate Lib/test/test_gzip.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 5d5d9074f4ca
children 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35049
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
1 #! /usr/bin/env python
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
2 """Test script for the gzip module.
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
3 """
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
4
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
5 import unittest
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
6 from test import test_support
45468
1d84bf5f9478 Patch #2167 from calvin: Remove unused imports
Christian Heimes <christian@cheimes.de>
parents: 40839
diff changeset
7 import os
24710
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 24442
diff changeset
8 import gzip
50600
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
9 import struct
10471
ac3a465e50a6 Added a simple test suite for gzip. It simply opens a temp file,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
10
ac3a465e50a6 Added a simple test suite for gzip. It simply opens a temp file,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
11
ac3a465e50a6 Added a simple test suite for gzip. It simply opens a temp file,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
12 data1 = """ int length=DEFAULTALLOC, err = Z_OK;
ac3a465e50a6 Added a simple test suite for gzip. It simply opens a temp file,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
13 PyObject *RetVal;
ac3a465e50a6 Added a simple test suite for gzip. It simply opens a temp file,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
14 int flushmode = Z_FINISH;
ac3a465e50a6 Added a simple test suite for gzip. It simply opens a temp file,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
15 unsigned long start_total_out;
ac3a465e50a6 Added a simple test suite for gzip. It simply opens a temp file,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
16
ac3a465e50a6 Added a simple test suite for gzip. It simply opens a temp file,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
17 """
ac3a465e50a6 Added a simple test suite for gzip. It simply opens a temp file,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
18
ac3a465e50a6 Added a simple test suite for gzip. It simply opens a temp file,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
19 data2 = """/* zlibmodule.c -- gzip-compatible data compression */
32853
1682201f05b2 SF bug #999776, zlib home page wrong
Neal Norwitz <nnorwitz@gmail.com>
parents: 32834
diff changeset
20 /* See http://www.gzip.org/zlib/
10471
ac3a465e50a6 Added a simple test suite for gzip. It simply opens a temp file,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
21 /* See http://www.winimage.com/zLibDll for Windows */
ac3a465e50a6 Added a simple test suite for gzip. It simply opens a temp file,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
22 """
ac3a465e50a6 Added a simple test suite for gzip. It simply opens a temp file,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
23
35049
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
24
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
25 class TestGzip(unittest.TestCase):
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
26 filename = test_support.TESTFN
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
27
47256
d5591c16b997 #2959: allow multiple close() calls for GzipFile.
Georg Brandl <georg@python.org>
parents: 46388
diff changeset
28 def setUp(self):
46388
8d555850f73d Remove the test file before writing it in case there is no write permission.
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
29 test_support.unlink(self.filename)
10471
ac3a465e50a6 Added a simple test suite for gzip. It simply opens a temp file,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
30
47256
d5591c16b997 #2959: allow multiple close() calls for GzipFile.
Georg Brandl <georg@python.org>
parents: 46388
diff changeset
31 def tearDown(self):
46388
8d555850f73d Remove the test file before writing it in case there is no write permission.
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
32 test_support.unlink(self.filename)
35049
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
33
32834
718305112f9f Added a new fileno() method. ZODB's repozo.py wants this so it can
Tim Peters <tim.peters@gmail.com>
parents: 24710
diff changeset
34
47256
d5591c16b997 #2959: allow multiple close() calls for GzipFile.
Georg Brandl <georg@python.org>
parents: 46388
diff changeset
35 def test_write(self):
35049
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
36 f = gzip.GzipFile(self.filename, 'wb') ; f.write(data1 * 50)
10471
ac3a465e50a6 Added a simple test suite for gzip. It simply opens a temp file,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
37
35049
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
38 # Try flush and fileno.
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
39 f.flush()
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
40 f.fileno()
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
41 if hasattr(os, 'fsync'):
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
42 os.fsync(f.fileno())
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
43 f.close()
10471
ac3a465e50a6 Added a simple test suite for gzip. It simply opens a temp file,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
44
47256
d5591c16b997 #2959: allow multiple close() calls for GzipFile.
Georg Brandl <georg@python.org>
parents: 46388
diff changeset
45 # Test multiple close() calls.
d5591c16b997 #2959: allow multiple close() calls for GzipFile.
Georg Brandl <georg@python.org>
parents: 46388
diff changeset
46 f.close()
d5591c16b997 #2959: allow multiple close() calls for GzipFile.
Georg Brandl <georg@python.org>
parents: 46388
diff changeset
47
35049
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
48 def test_read(self):
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
49 self.test_write()
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
50 # Try reading.
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
51 f = gzip.GzipFile(self.filename, 'r') ; d = f.read() ; f.close()
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
52 self.assertEqual(d, data1*50)
13744
5a3114e8c027 Exercise .readline() and .readlines(). More data is written to the
Andrew M. Kuchling <amk@amk.ca>
parents: 10537
diff changeset
53
35049
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
54 def test_append(self):
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
55 self.test_write()
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
56 # Append to the previous file
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
57 f = gzip.GzipFile(self.filename, 'ab') ; f.write(data2 * 15) ; f.close()
13744
5a3114e8c027 Exercise .readline() and .readlines(). More data is written to the
Andrew M. Kuchling <amk@amk.ca>
parents: 10537
diff changeset
58
35049
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
59 f = gzip.GzipFile(self.filename, 'rb') ; d = f.read() ; f.close()
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
60 self.assertEqual(d, (data1*50) + (data2*15))
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
61
35050
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
62 def test_many_append(self):
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
63 # Bug #1074261 was triggered when reading a file that contained
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
64 # many, many members. Create such a file and verify that reading it
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
65 # works.
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
66 f = gzip.open(self.filename, 'wb', 9)
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
67 f.write('a')
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
68 f.close()
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
69 for i in range(0,200):
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
70 f = gzip.open(self.filename, "ab", 9) # append
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
71 f.write('a')
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
72 f.close()
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
73
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
74 # Try reading the file
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
75 zgfile = gzip.open(self.filename, "rb")
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
76 contents = ""
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
77 while 1:
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
78 ztxt = zgfile.read(8192)
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
79 contents += ztxt
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
80 if not ztxt: break
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
81 zgfile.close()
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
82 self.assertEquals(contents, 'a'*201)
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
83
e79793a8eeea [Bug #1074261, patch #1074381] Restrict the size of chunks read from the file in order to avoid overflow or huge memory consumption. Patch by Mark Eichin
Andrew M. Kuchling <amk@amk.ca>
parents: 35049
diff changeset
84
35049
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
85 def test_readline(self):
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
86 self.test_write()
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
87 # Try .readline() with varying line lengths
13744
5a3114e8c027 Exercise .readline() and .readlines(). More data is written to the
Andrew M. Kuchling <amk@amk.ca>
parents: 10537
diff changeset
88
35049
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
89 f = gzip.GzipFile(self.filename, 'rb')
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
90 line_length = 0
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
91 while 1:
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
92 L = f.readline(line_length)
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
93 if L == "" and line_length != 0: break
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
94 self.assert_(len(L) <= line_length)
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
95 line_length = (line_length + 1) % 50
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
96 f.close()
13744
5a3114e8c027 Exercise .readline() and .readlines(). More data is written to the
Andrew M. Kuchling <amk@amk.ca>
parents: 10537
diff changeset
97
35049
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
98 def test_readlines(self):
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
99 self.test_write()
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
100 # Try .readlines()
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
101
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
102 f = gzip.GzipFile(self.filename, 'rb')
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
103 L = f.readlines()
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
104 f.close()
13744
5a3114e8c027 Exercise .readline() and .readlines(). More data is written to the
Andrew M. Kuchling <amk@amk.ca>
parents: 10537
diff changeset
105
35049
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
106 f = gzip.GzipFile(self.filename, 'rb')
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
107 while 1:
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
108 L = f.readlines(150)
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
109 if L == []: break
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
110 f.close()
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
111
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
112 def test_seek_read(self):
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
113 self.test_write()
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
114 # Try seek, read test
19088
1960ceec1a00 Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents: 15965
diff changeset
115
35049
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
116 f = gzip.GzipFile(self.filename)
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
117 while 1:
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
118 oldpos = f.tell()
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
119 line1 = f.readline()
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
120 if not line1: break
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
121 newpos = f.tell()
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
122 f.seek(oldpos) # negative seek
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
123 if len(line1)>10:
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
124 amount = 10
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
125 else:
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
126 amount = len(line1)
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
127 line2 = f.read(amount)
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
128 self.assertEqual(line1[:amount], line2)
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
129 f.seek(newpos) # positive seek
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
130 f.close()
19088
1960ceec1a00 Patch #448474: Add support for tell() and seek() to gzip.GzipFile.
Martin v. Löwis <martin@v.loewis.de>
parents: 15965
diff changeset
131
40334
9f7fc68a20ae Patch #1355023: support whence argument for GzipFile.seek.
Martin v. Löwis <martin@v.loewis.de>
parents: 35050
diff changeset
132 def test_seek_whence(self):
9f7fc68a20ae Patch #1355023: support whence argument for GzipFile.seek.
Martin v. Löwis <martin@v.loewis.de>
parents: 35050
diff changeset
133 self.test_write()
9f7fc68a20ae Patch #1355023: support whence argument for GzipFile.seek.
Martin v. Löwis <martin@v.loewis.de>
parents: 35050
diff changeset
134 # Try seek(whence=1), read test
9f7fc68a20ae Patch #1355023: support whence argument for GzipFile.seek.
Martin v. Löwis <martin@v.loewis.de>
parents: 35050
diff changeset
135
9f7fc68a20ae Patch #1355023: support whence argument for GzipFile.seek.
Martin v. Löwis <martin@v.loewis.de>
parents: 35050
diff changeset
136 f = gzip.GzipFile(self.filename)
9f7fc68a20ae Patch #1355023: support whence argument for GzipFile.seek.
Martin v. Löwis <martin@v.loewis.de>
parents: 35050
diff changeset
137 f.read(10)
9f7fc68a20ae Patch #1355023: support whence argument for GzipFile.seek.
Martin v. Löwis <martin@v.loewis.de>
parents: 35050
diff changeset
138 f.seek(10, whence=1)
9f7fc68a20ae Patch #1355023: support whence argument for GzipFile.seek.
Martin v. Löwis <martin@v.loewis.de>
parents: 35050
diff changeset
139 y = f.read(10)
9f7fc68a20ae Patch #1355023: support whence argument for GzipFile.seek.
Martin v. Löwis <martin@v.loewis.de>
parents: 35050
diff changeset
140 f.close()
9f7fc68a20ae Patch #1355023: support whence argument for GzipFile.seek.
Martin v. Löwis <martin@v.loewis.de>
parents: 35050
diff changeset
141 self.assertEquals(y, data1[20:30])
40732
5dae0d7f683a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40334
diff changeset
142
35049
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
143 def test_seek_write(self):
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
144 # Try seek, write test
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
145 f = gzip.GzipFile(self.filename, 'w')
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
146 for pos in range(0, 256, 16):
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
147 f.seek(pos)
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
148 f.write('GZ\n')
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
149 f.close()
10471
ac3a465e50a6 Added a simple test suite for gzip. It simply opens a temp file,
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
150
35049
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
151 def test_mode(self):
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
152 self.test_write()
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
153 f = gzip.GzipFile(self.filename, 'r')
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
154 self.assertEqual(f.myfileobj.mode, 'rb')
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
155 f.close()
23518
c03e8899d4b1 force gzip module to open files using 'b'inary mode.
Skip Montanaro <skip@pobox.com>
parents: 19119
diff changeset
156
40839
3983ee0f6f96 Patch #1647484: Renamed GzipFile's filename attribute to name. The
Lars Gustäbel <lars@gustaebel.de>
parents: 40732
diff changeset
157 def test_1647484(self):
3983ee0f6f96 Patch #1647484: Renamed GzipFile's filename attribute to name. The
Lars Gustäbel <lars@gustaebel.de>
parents: 40732
diff changeset
158 for mode in ('wb', 'rb'):
3983ee0f6f96 Patch #1647484: Renamed GzipFile's filename attribute to name. The
Lars Gustäbel <lars@gustaebel.de>
parents: 40732
diff changeset
159 f = gzip.GzipFile(self.filename, mode)
3983ee0f6f96 Patch #1647484: Renamed GzipFile's filename attribute to name. The
Lars Gustäbel <lars@gustaebel.de>
parents: 40732
diff changeset
160 self.assert_(hasattr(f, "name"))
3983ee0f6f96 Patch #1647484: Renamed GzipFile's filename attribute to name. The
Lars Gustäbel <lars@gustaebel.de>
parents: 40732
diff changeset
161 self.assertEqual(f.name, self.filename)
3983ee0f6f96 Patch #1647484: Renamed GzipFile's filename attribute to name. The
Lars Gustäbel <lars@gustaebel.de>
parents: 40732
diff changeset
162 f.close()
3983ee0f6f96 Patch #1647484: Renamed GzipFile's filename attribute to name. The
Lars Gustäbel <lars@gustaebel.de>
parents: 40732
diff changeset
163
50600
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
164 def test_mtime(self):
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
165 mtime = 123456789
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
166 fWrite = gzip.GzipFile(self.filename, 'w', mtime = mtime)
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
167 fWrite.write(data1)
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
168 fWrite.close()
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
169 fRead = gzip.GzipFile(self.filename)
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
170 dataRead = fRead.read()
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
171 self.assertEqual(dataRead, data1)
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
172 self.assert_(hasattr(fRead, 'mtime'))
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
173 self.assertEqual(fRead.mtime, mtime)
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
174 fRead.close()
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
175
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
176 def test_metadata(self):
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
177 mtime = 123456789
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
178
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
179 fWrite = gzip.GzipFile(self.filename, 'w', mtime = mtime)
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
180 fWrite.write(data1)
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
181 fWrite.close()
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
182
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
183 fRead = open(self.filename, 'rb')
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
184
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
185 # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
186
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
187 idBytes = fRead.read(2)
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
188 self.assertEqual(idBytes, '\x1f\x8b') # gzip ID
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
189
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
190 cmByte = fRead.read(1)
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
191 self.assertEqual(cmByte, '\x08') # deflate
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
192
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
193 flagsByte = fRead.read(1)
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
194 self.assertEqual(flagsByte, '\x08') # only the FNAME flag is set
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
195
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
196 mtimeBytes = fRead.read(4)
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
197 self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
198
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
199 xflByte = fRead.read(1)
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
200 self.assertEqual(xflByte, '\x02') # maximum compression
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
201
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
202 osByte = fRead.read(1)
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
203 self.assertEqual(osByte, '\xff') # OS "unknown" (OS-independent)
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
204
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
205 # Since the FNAME flag is set, the zero-terminated filename follows.
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
206 # RFC 1952 specifies that this is the name of the input file, if any.
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
207 # However, the gzip module defaults to storing the name of the output
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
208 # file in this field.
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
209 nameBytes = fRead.read(len(self.filename) + 1)
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
210 self.assertEqual(nameBytes, self.filename + '\x00')
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
211
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
212 # Since no other flags were set, the header ends here.
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
213 # Rather than process the compressed data, let's seek to the trailer.
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
214 fRead.seek(os.stat(self.filename).st_size - 8)
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
215
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
216 crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1]
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
217 self.assertEqual(crc32Bytes, '\xaf\xd7d\x83')
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
218
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
219 isizeBytes = fRead.read(4)
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
220 self.assertEqual(isizeBytes, struct.pack('<i', len(data1)))
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
221
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
222 fRead.close()
9de21e615577 Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream.
Antoine Pitrou <solipsis@pitrou.net>
parents: 47256
diff changeset
223
50698
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
224 def test_with_open(self):
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
225 # GzipFile supports the context management protocol
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
226 with gzip.GzipFile(self.filename, "wb") as f:
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
227 f.write(b"xxx")
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
228 f = gzip.GzipFile(self.filename, "rb")
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
229 f.close()
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
230 try:
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
231 with f:
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
232 pass
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
233 except ValueError:
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
234 pass
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
235 else:
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
236 self.fail("__enter__ on a closed file didn't raise an exception")
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
237 try:
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
238 with gzip.GzipFile(self.filename, "wb") as f:
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
239 1/0
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
240 except ZeroDivisionError:
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
241 pass
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
242 else:
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
243 self.fail("1/0 didn't raise an exception")
5d5d9074f4ca Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50600
diff changeset
244
35049
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
245 def test_main(verbose=None):
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
246 test_support.run_unittest(TestGzip)
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
247
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
248 if __name__ == "__main__":
8ae8e64ca9bd Convert gzip test suite to use unittest
Andrew M. Kuchling <amk@amk.ca>
parents: 32853
diff changeset
249 test_main(verbose=True)