Index: Lib/tarfile.py =================================================================== --- Lib/tarfile.py (revision 75934) +++ Lib/tarfile.py (working copy) @@ -33,10 +33,10 @@ __version__ = "$Revision$" # $Source$ -version = "0.9.0" -__author__ = "Lars Gustäbel (lars@gustaebel.de)" -__date__ = "$Date$" -__cvsid__ = "$Id$" +version = "0.9.0" +__author__ = "Lars Gustäbel (lars@gustaebel.de)" +__date__ = "$Date$" +__cvsid__ = "$Id$" __credits__ = "Gustavo Niemeyer, Niels Gustäbel, Richard Townsend." #--------- @@ -147,19 +147,19 @@ S_IFCHR = 0020000 # character device S_IFIFO = 0010000 # fifo -TSUID = 04000 # set UID on execution -TSGID = 02000 # set GID on execution -TSVTX = 01000 # reserved +TSUID = 04000 # set UID on execution +TSGID = 02000 # set GID on execution +TSVTX = 01000 # reserved -TUREAD = 0400 # read by owner +TUREAD = 0400 # read by owner TUWRITE = 0200 # write by owner -TUEXEC = 0100 # execute/search by owner -TGREAD = 0040 # read by group +TUEXEC = 0100 # execute/search by owner +TGREAD = 0040 # read by group TGWRITE = 0020 # write by group -TGEXEC = 0010 # execute/search by group -TOREAD = 0004 # read by other +TGEXEC = 0010 # execute/search by group +TOREAD = 0004 # read by other TOWRITE = 0002 # write by other -TOEXEC = 0001 # execute/search by other +TOEXEC = 0001 # execute/search by other #--------------------------------------------------------- # initialization @@ -289,30 +289,30 @@ return filemode_table = ( - ((S_IFLNK, "l"), - (S_IFREG, "-"), - (S_IFBLK, "b"), - (S_IFDIR, "d"), - (S_IFCHR, "c"), - (S_IFIFO, "p")), + ((S_IFLNK, "l"), + (S_IFREG, "-"), + (S_IFBLK, "b"), + (S_IFDIR, "d"), + (S_IFCHR, "c"), + (S_IFIFO, "p")), - ((TUREAD, "r"),), - ((TUWRITE, "w"),), - ((TUEXEC|TSUID, "s"), - (TSUID, "S"), - (TUEXEC, "x")), + ((TUREAD, "r"),), + ((TUWRITE, "w"),), + ((TUEXEC | TSUID, "s"), + (TSUID, "S"), + (TUEXEC, "x")), - ((TGREAD, "r"),), - ((TGWRITE, "w"),), - ((TGEXEC|TSGID, "s"), - (TSGID, "S"), - (TGEXEC, "x")), + ((TGREAD, "r"),), + ((TGWRITE, "w"),), + ((TGEXEC | TSGID, "s"), + (TSGID, "S"), + (TGEXEC, "x")), - ((TOREAD, "r"),), - ((TOWRITE, "w"),), - ((TOEXEC|TSVTX, "t"), - (TSVTX, "T"), - (TOEXEC, "x")) + ((TOREAD, "r"),), + ((TOWRITE, "w"),), + ((TOEXEC | TSVTX, "t"), + (TSVTX, "T"), + (TOEXEC, "x")) ) def filemode(mode): @@ -401,14 +401,14 @@ fileobj = _StreamProxy(fileobj) comptype = fileobj.getcomptype() - self.name = name or "" - self.mode = mode + self.name = name or "" + self.mode = mode self.comptype = comptype - self.fileobj = fileobj - self.bufsize = bufsize - self.buf = "" - self.pos = 0L - self.closed = False + self.fileobj = fileobj + self.bufsize = bufsize + self.buf = "" + self.pos = 0L + self.closed = False if comptype == "gz": try: @@ -441,7 +441,7 @@ """Initialize for writing with gzip compression. """ self.cmp = self.zlib.compressobj(9, self.zlib.DEFLATED, - -self.zlib.MAX_WBITS, + - self.zlib.MAX_WBITS, self.zlib.DEF_MEM_LEVEL, 0) timestamp = struct.pack("" % (self.__class__.__name__,self.name,id(self)) + return "<%s %r at %#x>" % (self.__class__.__name__, self.name, id(self)) def get_info(self, encoding, errors): """Return the TarInfo's attributes as a dictionary. @@ -1568,7 +1568,7 @@ while True: if self.next() is None: if self.offset > 0: - self.fileobj.seek(- BLOCKSIZE, 1) + self.fileobj.seek(-BLOCKSIZE, 1) break if self.mode in "aw": @@ -1579,6 +1579,16 @@ self.fileobj.write(buf) self.offset += len(buf) + #-------------------------------------------------------------------------- + # Support of 'with' statemet + #-------------------------------------------------------------------------- + def __enter__(self): + return self + + def __exit__(self, type, value, tb): + self.close() + + def _getposix(self): return self.format == USTAR_FORMAT def _setposix(self, value): @@ -1736,8 +1746,8 @@ # All *open() methods are registered here. OPEN_METH = { - "tar": "taropen", # uncompressed tar - "gz": "gzopen", # gzip compressed tar + "tar": "taropen", # uncompressed tar + "gz": "gzopen", # gzip compressed tar "bz2": "bz2open" # bzip2 compressed tar } Index: Lib/test/test_tarfile.py =================================================================== --- Lib/test/test_tarfile.py (revision 75934) +++ Lib/test/test_tarfile.py (working copy) @@ -270,7 +270,7 @@ class StreamReadTest(ReadTest): - mode="r|" + mode = "r|" def test_fileobj_regular_file(self): tarinfo = self.tar.next() # get "regtype" (can't use getmember) @@ -962,7 +962,7 @@ tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, encoding="iso8859-1") t = tarfile.TarInfo() t.name = u"äöü" # non-ASCII - t.uid = 8**8 # too large + t.uid = 8 ** 8 # too large t.pax_headers = pax_headers tar.addfile(t) tar.close() @@ -1261,7 +1261,24 @@ def test_partial_input_bz2(self): self._test_partial_input("r:bz2") +class WithStmtTest(unittest.TestCase): + # Support of with statement + tarname = tarname + mode = "r:" + + def test_with_statement (self): + with tarfile.open(self.tarname) as self.tar: + tarinfo = self.tar.getmember("ustar/regtype") + fobj = self.tar.extractfile(tarinfo) + data = fobj.read() + self.assertTrue((len(data), md5sum(data)) == (tarinfo.size, md5_regtype), + "extraction using 'with' statement failed") + + self.assertTrue(self.tar.closed, "file not closed using 'with' statement") + + + def test_main(): if not os.path.exists(TEMPDIR): os.mkdir(TEMPDIR) @@ -1283,6 +1300,7 @@ PaxUnicodeTest, AppendTest, LimitsTest, + WithStmtTest ] if hasattr(os, "link"):