cpython/Lib/test/test_gzip.py
Thomas Wouters 9fe394c1be Merged revisions 53538-53622 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r53545 | andrew.kuchling | 2007-01-24 21:06:41 +0100 (Wed, 24 Jan 2007) | 1 line

  Strengthen warning about using lock()
........
  r53556 | thomas.heller | 2007-01-25 19:34:14 +0100 (Thu, 25 Jan 2007) | 3 lines

  Fix for #1643874: When calling SysAllocString, create a PyCObject
  which will eventually call SysFreeString to free the BSTR resource.
........
  r53563 | andrew.kuchling | 2007-01-25 21:02:13 +0100 (Thu, 25 Jan 2007) | 1 line

  Add item
........
  r53564 | brett.cannon | 2007-01-25 21:22:02 +0100 (Thu, 25 Jan 2007) | 8 lines

  Fix time.strptime's %U support.  Basically rewrote the algorithm to be more
  generic so that one only has to shift certain values based on whether the week
  was specified to start on Monday or Sunday.  Cut out a lot of edge case code
  compared to the previous version.  Also broke algorithm out into its own
  function (that is private to the module).

  Fixes bug #1643943 (thanks Biran Nahas for the report).
........
  r53570 | brett.cannon | 2007-01-26 00:30:39 +0100 (Fri, 26 Jan 2007) | 4 lines

  Remove specific mention of my name and email address from modules.  Not really
  needed and all bug reports should go to the bug tracker, not directly to me.
  Plus I am not the only person to have edited these files at this point.
........
  r53573 | fred.drake | 2007-01-26 17:28:44 +0100 (Fri, 26 Jan 2007) | 1 line

  fix typo (extraneous ")")
........
  r53575 | georg.brandl | 2007-01-27 18:43:02 +0100 (Sat, 27 Jan 2007) | 4 lines

  Patch #1638243: the compiler package is now able to correctly compile
  a with statement; previously, executing code containing a with statement
  compiled by the compiler package crashed the interpreter.
........
  r53578 | georg.brandl | 2007-01-27 18:59:42 +0100 (Sat, 27 Jan 2007) | 3 lines

  Patch #1634778: add missing encoding aliases for iso8859_15 and
  iso8859_16.
........
  r53579 | georg.brandl | 2007-01-27 20:38:50 +0100 (Sat, 27 Jan 2007) | 2 lines

  Bug #1645944: os.access now returns bool but docstring is not updated
........
  r53590 | brett.cannon | 2007-01-28 21:58:00 +0100 (Sun, 28 Jan 2007) | 2 lines

  Use the thread lock's context manager instead of a try/finally statement.
........
  r53591 | brett.cannon | 2007-01-29 05:41:44 +0100 (Mon, 29 Jan 2007) | 2 lines

  Add a test for slicing an exception.
........
  r53594 | andrew.kuchling | 2007-01-29 21:21:43 +0100 (Mon, 29 Jan 2007) | 1 line

  Minor edits to the curses HOWTO
........
  r53596 | andrew.kuchling | 2007-01-29 21:55:40 +0100 (Mon, 29 Jan 2007) | 1 line

  Various minor edits
........
  r53597 | andrew.kuchling | 2007-01-29 22:28:48 +0100 (Mon, 29 Jan 2007) | 1 line

  More edits
........
  r53601 | tim.peters | 2007-01-30 04:03:46 +0100 (Tue, 30 Jan 2007) | 2 lines

  Whitespace normalization.
........
  r53603 | georg.brandl | 2007-01-30 21:21:30 +0100 (Tue, 30 Jan 2007) | 2 lines

  Bug #1648191: typo in docs.
........
  r53605 | brett.cannon | 2007-01-30 22:34:36 +0100 (Tue, 30 Jan 2007) | 8 lines

  No more raising of string exceptions!

  The next step of PEP 352 (for 2.6) causes raising a string exception to trigger
  a TypeError.  Trying to catch a string exception raises a DeprecationWarning.
  References to string exceptions has been removed from the docs since they are
  now just an error.
........
  r53618 | raymond.hettinger | 2007-02-01 22:02:59 +0100 (Thu, 01 Feb 2007) | 1 line

  Bug #1648179:  set.update() not recognizing __iter__ overrides in dict subclasses.
........
2007-02-05 01:24:16 +00:00

160 lines
4.1 KiB
Python

#! /usr/bin/env python
"""Test script for the gzip module.
"""
import unittest
from test import test_support
import sys, os
import gzip
data1 = """ int length=DEFAULTALLOC, err = Z_OK;
PyObject *RetVal;
int flushmode = Z_FINISH;
unsigned long start_total_out;
"""
data2 = """/* zlibmodule.c -- gzip-compatible data compression */
/* See http://www.gzip.org/zlib/
/* See http://www.winimage.com/zLibDll for Windows */
"""
class TestGzip(unittest.TestCase):
filename = test_support.TESTFN
def setUp (self):
pass
def tearDown (self):
try:
os.unlink(self.filename)
except os.error:
pass
def test_write (self):
f = gzip.GzipFile(self.filename, 'wb') ; f.write(data1 * 50)
# Try flush and fileno.
f.flush()
f.fileno()
if hasattr(os, 'fsync'):
os.fsync(f.fileno())
f.close()
def test_read(self):
self.test_write()
# Try reading.
f = gzip.GzipFile(self.filename, 'r') ; d = f.read() ; f.close()
self.assertEqual(d, data1*50)
def test_append(self):
self.test_write()
# Append to the previous file
f = gzip.GzipFile(self.filename, 'ab') ; f.write(data2 * 15) ; f.close()
f = gzip.GzipFile(self.filename, 'rb') ; d = f.read() ; f.close()
self.assertEqual(d, (data1*50) + (data2*15))
def test_many_append(self):
# Bug #1074261 was triggered when reading a file that contained
# many, many members. Create such a file and verify that reading it
# works.
f = gzip.open(self.filename, 'wb', 9)
f.write('a')
f.close()
for i in range(0,200):
f = gzip.open(self.filename, "ab", 9) # append
f.write('a')
f.close()
# Try reading the file
zgfile = gzip.open(self.filename, "rb")
contents = ""
while 1:
ztxt = zgfile.read(8192)
contents += ztxt
if not ztxt: break
zgfile.close()
self.assertEquals(contents, 'a'*201)
def test_readline(self):
self.test_write()
# Try .readline() with varying line lengths
f = gzip.GzipFile(self.filename, 'rb')
line_length = 0
while 1:
L = f.readline(line_length)
if L == "" and line_length != 0: break
self.assert_(len(L) <= line_length)
line_length = (line_length + 1) % 50
f.close()
def test_readlines(self):
self.test_write()
# Try .readlines()
f = gzip.GzipFile(self.filename, 'rb')
L = f.readlines()
f.close()
f = gzip.GzipFile(self.filename, 'rb')
while 1:
L = f.readlines(150)
if L == []: break
f.close()
def test_seek_read(self):
self.test_write()
# Try seek, read test
f = gzip.GzipFile(self.filename)
while 1:
oldpos = f.tell()
line1 = f.readline()
if not line1: break
newpos = f.tell()
f.seek(oldpos) # negative seek
if len(line1)>10:
amount = 10
else:
amount = len(line1)
line2 = f.read(amount)
self.assertEqual(line1[:amount], line2)
f.seek(newpos) # positive seek
f.close()
def test_seek_whence(self):
self.test_write()
# Try seek(whence=1), read test
f = gzip.GzipFile(self.filename)
f.read(10)
f.seek(10, whence=1)
y = f.read(10)
f.close()
self.assertEquals(y, data1[20:30])
def test_seek_write(self):
# Try seek, write test
f = gzip.GzipFile(self.filename, 'w')
for pos in range(0, 256, 16):
f.seek(pos)
f.write('GZ\n')
f.close()
def test_mode(self):
self.test_write()
f = gzip.GzipFile(self.filename, 'r')
self.assertEqual(f.myfileobj.mode, 'rb')
f.close()
def test_main(verbose=None):
test_support.run_unittest(TestGzip)
if __name__ == "__main__":
test_main(verbose=True)