mirror of https://github.com/python/cpython
Merged revisions 85403 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r85403 | brian.curtin | 2010-10-12 21:56:26 -0500 (Tue, 12 Oct 2010) | 2 lines Implement #7944. Use `with` throughout the test suite. ........
This commit is contained in:
parent
04d8b220e1
commit
6ec1eb8e73
|
@ -54,26 +54,31 @@ class BZ2FileTest(BaseTest):
|
|||
os.unlink(self.filename)
|
||||
|
||||
def createTempFile(self, crlf=0):
|
||||
f = open(self.filename, "wb")
|
||||
with open(self.filename, "wb") as f:
|
||||
if crlf:
|
||||
data = self.DATA_CRLF
|
||||
else:
|
||||
data = self.DATA
|
||||
f.write(data)
|
||||
f.close()
|
||||
|
||||
def testRead(self):
|
||||
# "Test BZ2File.read()"
|
||||
self.createTempFile()
|
||||
bz2f = BZ2File(self.filename)
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
self.assertRaises(TypeError, bz2f.read, None)
|
||||
self.assertEqual(bz2f.read(), self.TEXT)
|
||||
bz2f.close()
|
||||
|
||||
def testRead0(self):
|
||||
# Test BBZ2File.read(0)"
|
||||
self.createTempFile()
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
self.assertRaises(TypeError, bz2f.read, None)
|
||||
self.assertEqual(bz2f.read(0), "")
|
||||
|
||||
def testReadChunk10(self):
|
||||
# "Test BZ2File.read() in chunks of 10 bytes"
|
||||
self.createTempFile()
|
||||
bz2f = BZ2File(self.filename)
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
text = ''
|
||||
while 1:
|
||||
str = bz2f.read(10)
|
||||
|
@ -81,41 +86,36 @@ class BZ2FileTest(BaseTest):
|
|||
break
|
||||
text += str
|
||||
self.assertEqual(text, text)
|
||||
bz2f.close()
|
||||
|
||||
def testRead100(self):
|
||||
# "Test BZ2File.read(100)"
|
||||
self.createTempFile()
|
||||
bz2f = BZ2File(self.filename)
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
self.assertEqual(bz2f.read(100), self.TEXT[:100])
|
||||
bz2f.close()
|
||||
|
||||
def testReadLine(self):
|
||||
# "Test BZ2File.readline()"
|
||||
self.createTempFile()
|
||||
bz2f = BZ2File(self.filename)
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
self.assertRaises(TypeError, bz2f.readline, None)
|
||||
sio = StringIO(self.TEXT)
|
||||
for line in sio.readlines():
|
||||
self.assertEqual(bz2f.readline(), line)
|
||||
bz2f.close()
|
||||
|
||||
def testReadLines(self):
|
||||
# "Test BZ2File.readlines()"
|
||||
self.createTempFile()
|
||||
bz2f = BZ2File(self.filename)
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
self.assertRaises(TypeError, bz2f.readlines, None)
|
||||
sio = StringIO(self.TEXT)
|
||||
self.assertEqual(bz2f.readlines(), sio.readlines())
|
||||
bz2f.close()
|
||||
|
||||
def testIterator(self):
|
||||
# "Test iter(BZ2File)"
|
||||
self.createTempFile()
|
||||
bz2f = BZ2File(self.filename)
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
sio = StringIO(self.TEXT)
|
||||
self.assertEqual(list(iter(bz2f)), sio.readlines())
|
||||
bz2f.close()
|
||||
|
||||
def testClosedIteratorDeadlock(self):
|
||||
# "Test that iteration on a closed bz2file releases the lock."
|
||||
|
@ -154,17 +154,15 @@ class BZ2FileTest(BaseTest):
|
|||
|
||||
def testWrite(self):
|
||||
# "Test BZ2File.write()"
|
||||
bz2f = BZ2File(self.filename, "w")
|
||||
with BZ2File(self.filename, "w") as bz2f:
|
||||
self.assertRaises(TypeError, bz2f.write)
|
||||
bz2f.write(self.TEXT)
|
||||
bz2f.close()
|
||||
f = open(self.filename, 'rb')
|
||||
with open(self.filename, 'rb') as f:
|
||||
self.assertEqual(self.decompress(f.read()), self.TEXT)
|
||||
f.close()
|
||||
|
||||
def testWriteChunks10(self):
|
||||
# "Test BZ2File.write() with chunks of 10 bytes"
|
||||
bz2f = BZ2File(self.filename, "w")
|
||||
with BZ2File(self.filename, "w") as bz2f:
|
||||
n = 0
|
||||
while 1:
|
||||
str = self.TEXT[n*10:(n+1)*10]
|
||||
|
@ -172,86 +170,75 @@ class BZ2FileTest(BaseTest):
|
|||
break
|
||||
bz2f.write(str)
|
||||
n += 1
|
||||
bz2f.close()
|
||||
f = open(self.filename, 'rb')
|
||||
with open(self.filename, 'rb') as f:
|
||||
self.assertEqual(self.decompress(f.read()), self.TEXT)
|
||||
f.close()
|
||||
|
||||
def testWriteLines(self):
|
||||
# "Test BZ2File.writelines()"
|
||||
bz2f = BZ2File(self.filename, "w")
|
||||
with BZ2File(self.filename, "w") as bz2f:
|
||||
self.assertRaises(TypeError, bz2f.writelines)
|
||||
sio = StringIO(self.TEXT)
|
||||
bz2f.writelines(sio.readlines())
|
||||
bz2f.close()
|
||||
# patch #1535500
|
||||
self.assertRaises(ValueError, bz2f.writelines, ["a"])
|
||||
f = open(self.filename, 'rb')
|
||||
with open(self.filename, 'rb') as f:
|
||||
self.assertEqual(self.decompress(f.read()), self.TEXT)
|
||||
f.close()
|
||||
|
||||
def testWriteMethodsOnReadOnlyFile(self):
|
||||
bz2f = BZ2File(self.filename, "w")
|
||||
with BZ2File(self.filename, "w") as bz2f:
|
||||
bz2f.write("abc")
|
||||
bz2f.close()
|
||||
|
||||
bz2f = BZ2File(self.filename, "r")
|
||||
with BZ2File(self.filename, "r") as bz2f:
|
||||
self.assertRaises(IOError, bz2f.write, "a")
|
||||
self.assertRaises(IOError, bz2f.writelines, ["a"])
|
||||
|
||||
def testSeekForward(self):
|
||||
# "Test BZ2File.seek(150, 0)"
|
||||
self.createTempFile()
|
||||
bz2f = BZ2File(self.filename)
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
self.assertRaises(TypeError, bz2f.seek)
|
||||
bz2f.seek(150)
|
||||
self.assertEqual(bz2f.read(), self.TEXT[150:])
|
||||
bz2f.close()
|
||||
|
||||
def testSeekBackwards(self):
|
||||
# "Test BZ2File.seek(-150, 1)"
|
||||
self.createTempFile()
|
||||
bz2f = BZ2File(self.filename)
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
bz2f.read(500)
|
||||
bz2f.seek(-150, 1)
|
||||
self.assertEqual(bz2f.read(), self.TEXT[500-150:])
|
||||
bz2f.close()
|
||||
|
||||
def testSeekBackwardsFromEnd(self):
|
||||
# "Test BZ2File.seek(-150, 2)"
|
||||
self.createTempFile()
|
||||
bz2f = BZ2File(self.filename)
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
bz2f.seek(-150, 2)
|
||||
self.assertEqual(bz2f.read(), self.TEXT[len(self.TEXT)-150:])
|
||||
bz2f.close()
|
||||
|
||||
def testSeekPostEnd(self):
|
||||
# "Test BZ2File.seek(150000)"
|
||||
self.createTempFile()
|
||||
bz2f = BZ2File(self.filename)
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
bz2f.seek(150000)
|
||||
self.assertEqual(bz2f.tell(), len(self.TEXT))
|
||||
self.assertEqual(bz2f.read(), "")
|
||||
bz2f.close()
|
||||
|
||||
def testSeekPostEndTwice(self):
|
||||
# "Test BZ2File.seek(150000) twice"
|
||||
self.createTempFile()
|
||||
bz2f = BZ2File(self.filename)
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
bz2f.seek(150000)
|
||||
bz2f.seek(150000)
|
||||
self.assertEqual(bz2f.tell(), len(self.TEXT))
|
||||
self.assertEqual(bz2f.read(), "")
|
||||
bz2f.close()
|
||||
|
||||
def testSeekPreStart(self):
|
||||
# "Test BZ2File.seek(-150, 0)"
|
||||
self.createTempFile()
|
||||
bz2f = BZ2File(self.filename)
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
bz2f.seek(-150)
|
||||
self.assertEqual(bz2f.tell(), 0)
|
||||
self.assertEqual(bz2f.read(), self.TEXT)
|
||||
bz2f.close()
|
||||
|
||||
def testOpenDel(self):
|
||||
# "Test opening and deleting a file many times"
|
||||
|
@ -277,16 +264,13 @@ class BZ2FileTest(BaseTest):
|
|||
def testBug1191043(self):
|
||||
# readlines() for files containing no newline
|
||||
data = 'BZh91AY&SY\xd9b\x89]\x00\x00\x00\x03\x80\x04\x00\x02\x00\x0c\x00 \x00!\x9ah3M\x13<]\xc9\x14\xe1BCe\x8a%t'
|
||||
f = open(self.filename, "wb")
|
||||
with open(self.filename, "wb") as f:
|
||||
f.write(data)
|
||||
f.close()
|
||||
bz2f = BZ2File(self.filename)
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
lines = bz2f.readlines()
|
||||
bz2f.close()
|
||||
self.assertEqual(lines, ['Test'])
|
||||
bz2f = BZ2File(self.filename)
|
||||
xlines = list(bz2f.xreadlines())
|
||||
bz2f.close()
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
xlines = list(bz2f.readlines())
|
||||
self.assertEqual(xlines, ['Test'])
|
||||
|
||||
def testContextProtocol(self):
|
||||
|
@ -316,8 +300,7 @@ class BZ2FileTest(BaseTest):
|
|||
# Using a BZ2File from several threads doesn't deadlock (issue #7205).
|
||||
data = "1" * 2**20
|
||||
nthreads = 10
|
||||
f = bz2.BZ2File(self.filename, 'wb')
|
||||
try:
|
||||
with bz2.BZ2File(self.filename, 'wb') as f:
|
||||
def comp():
|
||||
for i in range(5):
|
||||
f.write(data)
|
||||
|
@ -326,27 +309,19 @@ class BZ2FileTest(BaseTest):
|
|||
t.start()
|
||||
for t in threads:
|
||||
t.join()
|
||||
finally:
|
||||
f.close()
|
||||
|
||||
def testMixedIterationReads(self):
|
||||
# Issue #8397: mixed iteration and reads should be forbidden.
|
||||
f = bz2.BZ2File(self.filename, 'wb')
|
||||
try:
|
||||
with bz2.BZ2File(self.filename, 'wb') as f:
|
||||
# The internal buffer size is hard-wired to 8192 bytes, we must
|
||||
# write out more than that for the test to stop half through
|
||||
# the buffer.
|
||||
f.write(self.TEXT * 100)
|
||||
finally:
|
||||
f.close()
|
||||
f = bz2.BZ2File(self.filename, 'rb')
|
||||
try:
|
||||
with bz2.BZ2File(self.filename, 'rb') as f:
|
||||
next(f)
|
||||
self.assertRaises(ValueError, f.read)
|
||||
self.assertRaises(ValueError, f.readline)
|
||||
self.assertRaises(ValueError, f.readlines)
|
||||
finally:
|
||||
f.close()
|
||||
|
||||
class BZ2CompressorTest(BaseTest):
|
||||
def testCompress(self):
|
||||
|
|
Loading…
Reference in New Issue