mirror of
https://github.com/python/cpython.git
synced 2025-09-01 14:38:00 +00:00
Remove no longer needed work-around for bz2 file object support.
This commit is contained in:
parent
45fb082180
commit
bb44b73e17
2 changed files with 5 additions and 64 deletions
|
@ -632,66 +632,6 @@ class _StreamProxy(object):
|
||||||
self.fileobj.close()
|
self.fileobj.close()
|
||||||
# class StreamProxy
|
# class StreamProxy
|
||||||
|
|
||||||
class _BZ2Proxy(object):
|
|
||||||
"""Small proxy class that enables external file object
|
|
||||||
support for "r:bz2" and "w:bz2" modes. This is actually
|
|
||||||
a workaround for a limitation in bz2 module's BZ2File
|
|
||||||
class which (unlike gzip.GzipFile) has no support for
|
|
||||||
a file object argument.
|
|
||||||
"""
|
|
||||||
|
|
||||||
blocksize = 16 * 1024
|
|
||||||
|
|
||||||
def __init__(self, fileobj, mode):
|
|
||||||
self.fileobj = fileobj
|
|
||||||
self.mode = mode
|
|
||||||
self.name = getattr(self.fileobj, "name", None)
|
|
||||||
self.init()
|
|
||||||
|
|
||||||
def init(self):
|
|
||||||
import bz2
|
|
||||||
self.pos = 0
|
|
||||||
if self.mode == "r":
|
|
||||||
self.bz2obj = bz2.BZ2Decompressor()
|
|
||||||
self.fileobj.seek(0)
|
|
||||||
self.buf = b""
|
|
||||||
else:
|
|
||||||
self.bz2obj = bz2.BZ2Compressor()
|
|
||||||
|
|
||||||
def read(self, size):
|
|
||||||
x = len(self.buf)
|
|
||||||
while x < size:
|
|
||||||
raw = self.fileobj.read(self.blocksize)
|
|
||||||
if not raw:
|
|
||||||
break
|
|
||||||
data = self.bz2obj.decompress(raw)
|
|
||||||
self.buf += data
|
|
||||||
x += len(data)
|
|
||||||
|
|
||||||
buf = self.buf[:size]
|
|
||||||
self.buf = self.buf[size:]
|
|
||||||
self.pos += len(buf)
|
|
||||||
return buf
|
|
||||||
|
|
||||||
def seek(self, pos):
|
|
||||||
if pos < self.pos:
|
|
||||||
self.init()
|
|
||||||
self.read(pos - self.pos)
|
|
||||||
|
|
||||||
def tell(self):
|
|
||||||
return self.pos
|
|
||||||
|
|
||||||
def write(self, data):
|
|
||||||
self.pos += len(data)
|
|
||||||
raw = self.bz2obj.compress(data)
|
|
||||||
self.fileobj.write(raw)
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
if self.mode == "w":
|
|
||||||
raw = self.bz2obj.flush()
|
|
||||||
self.fileobj.write(raw)
|
|
||||||
# class _BZ2Proxy
|
|
||||||
|
|
||||||
#------------------------
|
#------------------------
|
||||||
# Extraction file object
|
# Extraction file object
|
||||||
#------------------------
|
#------------------------
|
||||||
|
@ -1829,10 +1769,8 @@ class TarFile(object):
|
||||||
except ImportError:
|
except ImportError:
|
||||||
raise CompressionError("bz2 module is not available")
|
raise CompressionError("bz2 module is not available")
|
||||||
|
|
||||||
if fileobj is not None:
|
fileobj = bz2.BZ2File(filename=name if fileobj is None else None,
|
||||||
fileobj = _BZ2Proxy(fileobj, mode)
|
mode=mode, fileobj=fileobj, compresslevel=compresslevel)
|
||||||
else:
|
|
||||||
fileobj = bz2.BZ2File(name, mode, compresslevel=compresslevel)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
t = cls.taropen(name, mode, fileobj, **kwargs)
|
t = cls.taropen(name, mode, fileobj, **kwargs)
|
||||||
|
|
|
@ -222,6 +222,9 @@ class CommonReadTest(ReadTest):
|
||||||
class MiscReadTest(CommonReadTest):
|
class MiscReadTest(CommonReadTest):
|
||||||
|
|
||||||
def test_no_name_argument(self):
|
def test_no_name_argument(self):
|
||||||
|
if self.mode.endswith("bz2"):
|
||||||
|
# BZ2File has no name attribute.
|
||||||
|
return
|
||||||
with open(self.tarname, "rb") as fobj:
|
with open(self.tarname, "rb") as fobj:
|
||||||
tar = tarfile.open(fileobj=fobj, mode=self.mode)
|
tar = tarfile.open(fileobj=fobj, mode=self.mode)
|
||||||
self.assertEqual(tar.name, os.path.abspath(fobj.name))
|
self.assertEqual(tar.name, os.path.abspath(fobj.name))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue