Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3b4a4f5

Browse files
committed
Stylistic tweaks to the bz2 module to make it easier to transplant changes from
it to http://pypi.python.org/pypi/bz2file.
1 parent fc7b0ec commit 3b4a4f5

2 files changed

Lines changed: 75 additions & 74 deletions

File tree

Lib/bz2.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
__author__ = "Nadeem Vawda <[email protected]>"
1111

12-
import builtins
1312
import io
1413
import warnings
1514

@@ -28,6 +27,8 @@
2827

2928
_BUFFER_SIZE = 8192
3029

30+
_builtin_open = open
31+
3132

3233
class BZ2File(io.BufferedIOBase):
3334

@@ -90,10 +91,10 @@ def __init__(self, filename, mode="r", buffering=None, compresslevel=9):
9091
mode_code = _MODE_WRITE
9192
self._compressor = BZ2Compressor(compresslevel)
9293
else:
93-
raise ValueError("Invalid mode: {!r}".format(mode))
94+
raise ValueError("Invalid mode: %r" % (mode,))
9495

9596
if isinstance(filename, (str, bytes)):
96-
self._fp = builtins.open(filename, mode)
97+
self._fp = _builtin_open(filename, mode)
9798
self._closefp = True
9899
self._mode = mode_code
99100
elif hasattr(filename, "read") or hasattr(filename, "write"):
@@ -189,15 +190,17 @@ def _fill_buffer(self):
189190

190191
if not rawblock:
191192
if self._decompressor.eof:
193+
# End-of-stream marker and end of file. We're good.
192194
self._mode = _MODE_READ_EOF
193195
self._size = self._pos
194196
return False
195197
else:
198+
# Problem - we were expecting more compressed data.
196199
raise EOFError("Compressed file ended before the "
197200
"end-of-stream marker was reached")
198201

199-
# Continue to next stream.
200202
if self._decompressor.eof:
203+
# Continue to next stream.
201204
self._decompressor = BZ2Decompressor()
202205

203206
self._buffer = self._decompressor.decompress(rawblock)
@@ -412,7 +415,7 @@ def seek(self, offset, whence=0):
412415
self._read_all(return_data=False)
413416
offset = self._size + offset
414417
else:
415-
raise ValueError("Invalid value for whence: {}".format(whence))
418+
raise ValueError("Invalid value for whence: %s" % (whence,))
416419

417420
# Make it so that offset is the number of bytes to skip forward.
418421
if offset < self._pos:

Lib/test/test_bz2.py

Lines changed: 67 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22
from test import support
3-
from test.support import TESTFN, bigmemtest, _4G
3+
from test.support import bigmemtest, _4G
44

55
import unittest
66
from io import BytesIO
@@ -18,10 +18,10 @@
1818
bz2 = support.import_module('bz2')
1919
from bz2 import BZ2File, BZ2Compressor, BZ2Decompressor
2020

21-
has_cmdline_bunzip2 = (sys.platform != "win32")
2221

2322
class BaseTest(unittest.TestCase):
2423
"Base for other testcases."
24+
2525
TEXT_LINES = [
2626
b'root:x:0:0:root:/root:/bin/bash\n',
2727
b'bin:x:1:1:bin:/bin:\n',
@@ -49,13 +49,17 @@ class BaseTest(unittest.TestCase):
4949
DATA = b'BZh91AY&SY.\xc8N\x18\x00\x01>_\x80\x00\x10@\x02\xff\xf0\x01\x07n\x00?\xe7\xff\xe00\x01\x99\xaa\x00\xc0\x03F\x86\x8c#&\x83F\x9a\x03\x06\xa6\xd0\xa6\x93M\x0fQ\xa7\xa8\x06\x804hh\x12$\x11\xa4i4\xf14S\xd2<Q\xb5\x0fH\xd3\xd4\xdd\xd5\x87\xbb\xf8\x94\r\x8f\xafI\x12\xe1\xc9\xf8/E\x00pu\x89\x12]\xc9\xbbDL\nQ\x0e\t1\x12\xdf\xa0\xc0\x97\xac2O9\x89\x13\x94\x0e\x1c7\x0ed\x95I\x0c\xaaJ\xa4\x18L\x10\x05#\x9c\xaf\xba\xbc/\x97\x8a#C\xc8\xe1\x8cW\xf9\xe2\xd0\xd6M\xa7\x8bXa<e\x84t\xcbL\xb3\xa7\xd9\xcd\xd1\xcb\x84.\xaf\xb3\xab\xab\xad`n}\xa0lh\tE,\x8eZ\x15\x17VH>\x88\xe5\xcd9gd6\x0b\n\xe9\x9b\xd5\x8a\x99\xf7\x08.K\x8ev\xfb\xf7xw\xbb\xdf\xa1\x92\xf1\xdd|/";\xa2\xba\x9f\xd5\xb1#A\xb6\xf6\xb3o\xc9\xc5y\\\xebO\xe7\x85\x9a\xbc\xb6f8\x952\xd5\xd7"%\x89>V,\xf7\xa6z\xe2\x9f\xa3\xdf\x11\x11"\xd6E)I\xa9\x13^\xca\xf3r\xd0\x03U\x922\xf26\xec\xb6\xed\x8b\xc3U\x13\x9d\xc5\x170\xa4\xfa^\x92\xacDF\x8a\x97\xd6\x19\xfe\xdd\xb8\xbd\x1a\x9a\x19\xa3\x80ankR\x8b\xe5\xd83]\xa9\xc6\x08\x82f\xf6\xb9"6l$\xb8j@\xc0\x8a\xb0l1..\xbak\x83ls\x15\xbc\xf4\xc1\x13\xbe\xf8E\xb8\x9d\r\xa8\x9dk\x84\xd3n\xfa\xacQ\x07\xb1%y\xaav\xb4\x08\xe0z\x1b\x16\xf5\x04\xe9\xcc\xb9\x08z\x1en7.G\xfc]\xc9\x14\xe1B@\xbb!8`'
5050

5151
def setUp(self):
52-
self.filename = TESTFN
52+
self.filename = support.TESTFN
5353

5454
def tearDown(self):
5555
if os.path.isfile(self.filename):
5656
os.unlink(self.filename)
5757

58-
if has_cmdline_bunzip2:
58+
if sys.platform == "win32":
59+
# bunzip2 isn't available to run on Windows.
60+
def decompress(self, data):
61+
return bz2.decompress(data)
62+
else:
5963
def decompress(self, data):
6064
pop = subprocess.Popen("bunzip2", shell=True,
6165
stdin=subprocess.PIPE,
@@ -69,31 +73,21 @@ def decompress(self, data):
6973
ret = bz2.decompress(data)
7074
return ret
7175

72-
else:
73-
# bunzip2 isn't available to run on Windows.
74-
def decompress(self, data):
75-
return bz2.decompress(data)
7676

7777
class BZ2FileTest(BaseTest):
78-
"Test BZ2File type miscellaneous methods."
78+
"Test the BZ2File class."
7979

8080
def createTempFile(self, streams=1):
8181
with open(self.filename, "wb") as f:
8282
f.write(self.DATA * streams)
8383

8484
def testBadArgs(self):
85-
with self.assertRaises(TypeError):
86-
BZ2File(123.456)
87-
with self.assertRaises(ValueError):
88-
BZ2File("/dev/null", "z")
89-
with self.assertRaises(ValueError):
90-
BZ2File("/dev/null", "rx")
91-
with self.assertRaises(ValueError):
92-
BZ2File("/dev/null", "rbt")
93-
with self.assertRaises(ValueError):
94-
BZ2File("/dev/null", compresslevel=0)
95-
with self.assertRaises(ValueError):
96-
BZ2File("/dev/null", compresslevel=10)
85+
self.assertRaises(TypeError, BZ2File, 123.456)
86+
self.assertRaises(ValueError, BZ2File, "/dev/null", "z")
87+
self.assertRaises(ValueError, BZ2File, "/dev/null", "rx")
88+
self.assertRaises(ValueError, BZ2File, "/dev/null", "rbt")
89+
self.assertRaises(ValueError, BZ2File, "/dev/null", compresslevel=0)
90+
self.assertRaises(ValueError, BZ2File, "/dev/null", compresslevel=10)
9791

9892
def testRead(self):
9993
self.createTempFile()
@@ -214,9 +208,8 @@ def testClosedIteratorDeadlock(self):
214208
self.createTempFile()
215209
bz2f = BZ2File(self.filename)
216210
bz2f.close()
217-
self.assertRaises(ValueError, bz2f.__next__)
218-
# This call will deadlock if the above .__next__ call failed to
219-
# release the lock.
211+
self.assertRaises(ValueError, next, bz2f)
212+
# This call will deadlock if the above call failed to release the lock.
220213
self.assertRaises(ValueError, bz2f.readlines)
221214

222215
def testWrite(self):
@@ -379,7 +372,7 @@ def testSeekable(self):
379372
bz2f.close()
380373
self.assertRaises(ValueError, bz2f.seekable)
381374

382-
bz2f = BZ2File(BytesIO(), mode="w")
375+
bz2f = BZ2File(BytesIO(), "w")
383376
try:
384377
self.assertFalse(bz2f.seekable())
385378
finally:
@@ -405,7 +398,7 @@ def testReadable(self):
405398
bz2f.close()
406399
self.assertRaises(ValueError, bz2f.readable)
407400

408-
bz2f = BZ2File(BytesIO(), mode="w")
401+
bz2f = BZ2File(BytesIO(), "w")
409402
try:
410403
self.assertFalse(bz2f.readable())
411404
finally:
@@ -422,7 +415,7 @@ def testWritable(self):
422415
bz2f.close()
423416
self.assertRaises(ValueError, bz2f.writable)
424417

425-
bz2f = BZ2File(BytesIO(), mode="w")
418+
bz2f = BZ2File(BytesIO(), "w")
426419
try:
427420
self.assertTrue(bz2f.writable())
428421
finally:
@@ -476,7 +469,7 @@ def testThreading(self):
476469
# Issue #7205: Using a BZ2File from several threads shouldn't deadlock.
477470
data = b"1" * 2**20
478471
nthreads = 10
479-
with bz2.BZ2File(self.filename, 'wb') as f:
472+
with BZ2File(self.filename, 'wb') as f:
480473
def comp():
481474
for i in range(5):
482475
f.write(data)
@@ -487,28 +480,27 @@ def comp():
487480
t.join()
488481

489482
def testWithoutThreading(self):
490-
bz2 = support.import_fresh_module("bz2", blocked=("threading",))
491-
with bz2.BZ2File(self.filename, "wb") as f:
483+
module = support.import_fresh_module("bz2", blocked=("threading",))
484+
with module.BZ2File(self.filename, "wb") as f:
492485
f.write(b"abc")
493-
with bz2.BZ2File(self.filename, "rb") as f:
486+
with module.BZ2File(self.filename, "rb") as f:
494487
self.assertEqual(f.read(), b"abc")
495488

496489
def testMixedIterationAndReads(self):
497490
self.createTempFile()
498491
linelen = len(self.TEXT_LINES[0])
499492
halflen = linelen // 2
500-
with bz2.BZ2File(self.filename) as bz2f:
493+
with BZ2File(self.filename) as bz2f:
501494
bz2f.read(halflen)
502495
self.assertEqual(next(bz2f), self.TEXT_LINES[0][halflen:])
503496
self.assertEqual(bz2f.read(), self.TEXT[linelen:])
504-
with bz2.BZ2File(self.filename) as bz2f:
497+
with BZ2File(self.filename) as bz2f:
505498
bz2f.readline()
506499
self.assertEqual(next(bz2f), self.TEXT_LINES[1])
507500
self.assertEqual(bz2f.readline(), self.TEXT_LINES[2])
508-
with bz2.BZ2File(self.filename) as bz2f:
501+
with BZ2File(self.filename) as bz2f:
509502
bz2f.readlines()
510-
with self.assertRaises(StopIteration):
511-
next(bz2f)
503+
self.assertRaises(StopIteration, next, bz2f)
512504
self.assertEqual(bz2f.readlines(), [])
513505

514506
def testMultiStreamOrdering(self):
@@ -576,6 +568,7 @@ def testSeekBackwardsBytesIO(self):
576568
bz2f.seek(-150, 1)
577569
self.assertEqual(bz2f.read(), self.TEXT[500-150:])
578570

571+
579572
class BZ2CompressorTest(BaseTest):
580573
def testCompress(self):
581574
bz2c = BZ2Compressor()
@@ -687,97 +680,102 @@ def testDecompressMultiStream(self):
687680

688681

689682
class OpenTest(BaseTest):
683+
"Test the open function."
684+
685+
def open(self, *args, **kwargs):
686+
return bz2.open(*args, **kwargs)
687+
690688
def test_binary_modes(self):
691-
with bz2.open(self.filename, "wb") as f:
689+
with self.open(self.filename, "wb") as f:
692690
f.write(self.TEXT)
693691
with open(self.filename, "rb") as f:
694-
file_data = bz2.decompress(f.read())
692+
file_data = self.decompress(f.read())
695693
self.assertEqual(file_data, self.TEXT)
696-
with bz2.open(self.filename, "rb") as f:
694+
with self.open(self.filename, "rb") as f:
697695
self.assertEqual(f.read(), self.TEXT)
698-
with bz2.open(self.filename, "ab") as f:
696+
with self.open(self.filename, "ab") as f:
699697
f.write(self.TEXT)
700698
with open(self.filename, "rb") as f:
701-
file_data = bz2.decompress(f.read())
699+
file_data = self.decompress(f.read())
702700
self.assertEqual(file_data, self.TEXT * 2)
703701

704702
def test_implicit_binary_modes(self):
705703
# Test implicit binary modes (no "b" or "t" in mode string).
706-
with bz2.open(self.filename, "w") as f:
704+
with self.open(self.filename, "w") as f:
707705
f.write(self.TEXT)
708706
with open(self.filename, "rb") as f:
709-
file_data = bz2.decompress(f.read())
707+
file_data = self.decompress(f.read())
710708
self.assertEqual(file_data, self.TEXT)
711-
with bz2.open(self.filename, "r") as f:
709+
with self.open(self.filename, "r") as f:
712710
self.assertEqual(f.read(), self.TEXT)
713-
with bz2.open(self.filename, "a") as f:
711+
with self.open(self.filename, "a") as f:
714712
f.write(self.TEXT)
715713
with open(self.filename, "rb") as f:
716-
file_data = bz2.decompress(f.read())
714+
file_data = self.decompress(f.read())
717715
self.assertEqual(file_data, self.TEXT * 2)
718716

719717
def test_text_modes(self):
720718
text = self.TEXT.decode("ascii")
721719
text_native_eol = text.replace("\n", os.linesep)
722-
with bz2.open(self.filename, "wt") as f:
720+
with self.open(self.filename, "wt") as f:
723721
f.write(text)
724722
with open(self.filename, "rb") as f:
725-
file_data = bz2.decompress(f.read()).decode("ascii")
723+
file_data = self.decompress(f.read()).decode("ascii")
726724
self.assertEqual(file_data, text_native_eol)
727-
with bz2.open(self.filename, "rt") as f:
725+
with self.open(self.filename, "rt") as f:
728726
self.assertEqual(f.read(), text)
729-
with bz2.open(self.filename, "at") as f:
727+
with self.open(self.filename, "at") as f:
730728
f.write(text)
731729
with open(self.filename, "rb") as f:
732-
file_data = bz2.decompress(f.read()).decode("ascii")
730+
file_data = self.decompress(f.read()).decode("ascii")
733731
self.assertEqual(file_data, text_native_eol * 2)
734732

735733
def test_fileobj(self):
736-
with bz2.open(BytesIO(self.DATA), "r") as f:
734+
with self.open(BytesIO(self.DATA), "r") as f:
737735
self.assertEqual(f.read(), self.TEXT)
738-
with bz2.open(BytesIO(self.DATA), "rb") as f:
736+
with self.open(BytesIO(self.DATA), "rb") as f:
739737
self.assertEqual(f.read(), self.TEXT)
740738
text = self.TEXT.decode("ascii")
741-
with bz2.open(BytesIO(self.DATA), "rt") as f:
739+
with self.open(BytesIO(self.DATA), "rt") as f:
742740
self.assertEqual(f.read(), text)
743741

744742
def test_bad_params(self):
745743
# Test invalid parameter combinations.
746-
with self.assertRaises(ValueError):
747-
bz2.open(self.filename, "wbt")
748-
with self.assertRaises(ValueError):
749-
bz2.open(self.filename, "rb", encoding="utf-8")
750-
with self.assertRaises(ValueError):
751-
bz2.open(self.filename, "rb", errors="ignore")
752-
with self.assertRaises(ValueError):
753-
bz2.open(self.filename, "rb", newline="\n")
744+
self.assertRaises(ValueError,
745+
self.open, self.filename, "wbt")
746+
self.assertRaises(ValueError,
747+
self.open, self.filename, "rb", encoding="utf-8")
748+
self.assertRaises(ValueError,
749+
self.open, self.filename, "rb", errors="ignore")
750+
self.assertRaises(ValueError,
751+
self.open, self.filename, "rb", newline="\n")
754752

755753
def test_encoding(self):
756754
# Test non-default encoding.
757755
text = self.TEXT.decode("ascii")
758756
text_native_eol = text.replace("\n", os.linesep)
759-
with bz2.open(self.filename, "wt", encoding="utf-16-le") as f:
757+
with self.open(self.filename, "wt", encoding="utf-16-le") as f:
760758
f.write(text)
761759
with open(self.filename, "rb") as f:
762-
file_data = bz2.decompress(f.read()).decode("utf-16-le")
760+
file_data = self.decompress(f.read()).decode("utf-16-le")
763761
self.assertEqual(file_data, text_native_eol)
764-
with bz2.open(self.filename, "rt", encoding="utf-16-le") as f:
762+
with self.open(self.filename, "rt", encoding="utf-16-le") as f:
765763
self.assertEqual(f.read(), text)
766764

767765
def test_encoding_error_handler(self):
768766
# Test with non-default encoding error handler.
769-
with bz2.open(self.filename, "wb") as f:
767+
with self.open(self.filename, "wb") as f:
770768
f.write(b"foo\xffbar")
771-
with bz2.open(self.filename, "rt", encoding="ascii", errors="ignore") \
769+
with self.open(self.filename, "rt", encoding="ascii", errors="ignore") \
772770
as f:
773771
self.assertEqual(f.read(), "foobar")
774772

775773
def test_newline(self):
776774
# Test with explicit newline (universal newline mode disabled).
777775
text = self.TEXT.decode("ascii")
778-
with bz2.open(self.filename, "wt", newline="\n") as f:
776+
with self.open(self.filename, "wt", newline="\n") as f:
779777
f.write(text)
780-
with bz2.open(self.filename, "rt", newline="\r") as f:
778+
with self.open(self.filename, "rt", newline="\r") as f:
781779
self.assertEqual(f.readlines(), [text])
782780

783781

0 commit comments

Comments
 (0)