summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_decimal.py17
-rw-r--r--Lib/test/test_modulefinder.py16
-rw-r--r--Lib/test/test_multibytecodec.py4
-rw-r--r--Lib/test/test_multibytecodec_support.py56
-rw-r--r--Lib/test/test_pipes.py30
-rw-r--r--Lib/test/test_shutil.py12
6 files changed, 79 insertions, 56 deletions
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index 4a35465..a88da5b 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -225,14 +225,15 @@ class DecimalTest(unittest.TestCase):
if skip_expected:
raise unittest.SkipTest
return
- for line in open(file):
- line = line.replace('\r\n', '').replace('\n', '')
- #print line
- try:
- t = self.eval_line(line)
- except DecimalException as exception:
- #Exception raised where there shoudn't have been one.
- self.fail('Exception "'+exception.__class__.__name__ + '" raised on line '+line)
+ with open(file) as f:
+ for line in f:
+ line = line.replace('\r\n', '').replace('\n', '')
+ #print line
+ try:
+ t = self.eval_line(line)
+ except DecimalException as exception:
+ #Exception raised where there shoudn't have been one.
+ self.fail('Exception "'+exception.__class__.__name__ + '" raised on line '+line)
return
diff --git a/Lib/test/test_modulefinder.py b/Lib/test/test_modulefinder.py
index b4fa06a..a184217 100644
--- a/Lib/test/test_modulefinder.py
+++ b/Lib/test/test_modulefinder.py
@@ -204,11 +204,17 @@ def open_file(path):
def create_package(source):
ofi = None
- for line in source.splitlines():
- if line.startswith(" ") or line.startswith("\t"):
- ofi.write(line.strip() + "\n")
- else:
- ofi = open_file(os.path.join(TEST_DIR, line.strip()))
+ try:
+ for line in source.splitlines():
+ if line.startswith(" ") or line.startswith("\t"):
+ ofi.write(line.strip() + "\n")
+ else:
+ if ofi:
+ ofi.close()
+ ofi = open_file(os.path.join(TEST_DIR, line.strip()))
+ finally:
+ if ofi:
+ ofi.close()
class ModuleFinderTest(unittest.TestCase):
def _do_test(self, info, report=False):
diff --git a/Lib/test/test_multibytecodec.py b/Lib/test/test_multibytecodec.py
index 849573a..fe772e1 100644
--- a/Lib/test/test_multibytecodec.py
+++ b/Lib/test/test_multibytecodec.py
@@ -48,8 +48,8 @@ class Test_MultibyteCodec(unittest.TestCase):
def test_codingspec(self):
try:
for enc in ALL_CJKENCODINGS:
- print('# coding:', enc, file=io.open(TESTFN, 'w'))
- exec(open(TESTFN).read())
+ code = '# coding: {}\n'.format(enc)
+ exec(code)
finally:
support.unlink(TESTFN)
diff --git a/Lib/test/test_multibytecodec_support.py b/Lib/test/test_multibytecodec_support.py
index 70e4d75..ed15ce1 100644
--- a/Lib/test/test_multibytecodec_support.py
+++ b/Lib/test/test_multibytecodec_support.py
@@ -278,7 +278,7 @@ class TestBase_Mapping(unittest.TestCase):
def __init__(self, *args, **kw):
unittest.TestCase.__init__(self, *args, **kw)
try:
- self.open_mapping_file() # test it to report the error early
+ self.open_mapping_file().close() # test it to report the error early
except (IOError, HTTPException):
self.skipTest("Could not retrieve "+self.mapfileurl)
@@ -295,36 +295,38 @@ class TestBase_Mapping(unittest.TestCase):
unichrs = lambda s: ''.join(map(chr, map(eval, s.split('+'))))
urt_wa = {}
- for line in self.open_mapping_file():
- if not line:
- break
- data = line.split('#')[0].strip().split()
- if len(data) != 2:
- continue
-
- csetval = eval(data[0])
- if csetval <= 0x7F:
- csetch = bytes([csetval & 0xff])
- elif csetval >= 0x1000000:
- csetch = bytes([(csetval >> 24), ((csetval >> 16) & 0xff),
- ((csetval >> 8) & 0xff), (csetval & 0xff)])
- elif csetval >= 0x10000:
- csetch = bytes([(csetval >> 16), ((csetval >> 8) & 0xff),
- (csetval & 0xff)])
- elif csetval >= 0x100:
- csetch = bytes([(csetval >> 8), (csetval & 0xff)])
- else:
- continue
+ with self.open_mapping_file() as f:
+ for line in f:
+ if not line:
+ break
+ data = line.split('#')[0].strip().split()
+ if len(data) != 2:
+ continue
+
+ csetval = eval(data[0])
+ if csetval <= 0x7F:
+ csetch = bytes([csetval & 0xff])
+ elif csetval >= 0x1000000:
+ csetch = bytes([(csetval >> 24), ((csetval >> 16) & 0xff),
+ ((csetval >> 8) & 0xff), (csetval & 0xff)])
+ elif csetval >= 0x10000:
+ csetch = bytes([(csetval >> 16), ((csetval >> 8) & 0xff),
+ (csetval & 0xff)])
+ elif csetval >= 0x100:
+ csetch = bytes([(csetval >> 8), (csetval & 0xff)])
+ else:
+ continue
- unich = unichrs(data[1])
- if ord(unich) == 0xfffd or unich in urt_wa:
- continue
- urt_wa[unich] = csetch
+ unich = unichrs(data[1])
+ if ord(unich) == 0xfffd or unich in urt_wa:
+ continue
+ urt_wa[unich] = csetch
- self._testpoint(csetch, unich)
+ self._testpoint(csetch, unich)
def _test_mapping_file_ucm(self):
- ucmdata = self.open_mapping_file().read()
+ with self.open_mapping_file() as f:
+ ucmdata = f.read()
uc = re.findall('<a u="([A-F0-9]{4})" b="([0-9A-F ]+)"/>', ucmdata)
for uni, coded in uc:
unich = chr(int(uni, 16))
diff --git a/Lib/test/test_pipes.py b/Lib/test/test_pipes.py
index 3d31fce..f2b58d5 100644
--- a/Lib/test/test_pipes.py
+++ b/Lib/test/test_pipes.py
@@ -23,17 +23,21 @@ class SimplePipeTests(unittest.TestCase):
f = t.open(TESTFN, 'w')
f.write('hello world #1')
f.close()
- self.assertEqual(open(TESTFN).read(), 'HELLO WORLD #1')
+ with open(TESTFN) as f:
+ self.assertEqual(f.read(), 'HELLO WORLD #1')
def testSimplePipe2(self):
- open(TESTFN, 'w').write('hello world #2')
+ with open(TESTFN, 'w') as f:
+ f.write('hello world #2')
t = pipes.Template()
t.append(s_command + ' < $IN > $OUT', pipes.FILEIN_FILEOUT)
t.copy(TESTFN, TESTFN2)
- self.assertEqual(open(TESTFN2).read(), 'HELLO WORLD #2')
+ with open(TESTFN2) as f:
+ self.assertEqual(f.read(), 'HELLO WORLD #2')
def testSimplePipe3(self):
- open(TESTFN, 'w').write('hello world #2')
+ with open(TESTFN, 'w') as f:
+ f.write('hello world #2')
t = pipes.Template()
t.append(s_command + ' < $IN', pipes.FILEIN_STDOUT)
f = t.open(TESTFN, 'r')
@@ -45,16 +49,20 @@ class SimplePipeTests(unittest.TestCase):
def testEmptyPipeline1(self):
# copy through empty pipe
d = 'empty pipeline test COPY'
- open(TESTFN, 'w').write(d)
- open(TESTFN2, 'w').write('')
+ with open(TESTFN, 'w') as f:
+ f.write(d)
+ with open(TESTFN2, 'w') as f:
+ f.write('')
t=pipes.Template()
t.copy(TESTFN, TESTFN2)
- self.assertEqual(open(TESTFN2).read(), d)
+ with open(TESTFN2) as f:
+ self.assertEqual(f.read(), d)
def testEmptyPipeline2(self):
# read through empty pipe
d = 'empty pipeline test READ'
- open(TESTFN, 'w').write(d)
+ with open(TESTFN, 'w') as f:
+ f.write(d)
t=pipes.Template()
f = t.open(TESTFN, 'r')
try:
@@ -66,8 +74,10 @@ class SimplePipeTests(unittest.TestCase):
# write through empty pipe
d = 'empty pipeline test WRITE'
t = pipes.Template()
- t.open(TESTFN, 'w').write(d)
- self.assertEqual(open(TESTFN).read(), d)
+ with t.open(TESTFN, 'w') as f:
+ f.write(d)
+ with open(TESTFN) as f:
+ self.assertEqual(f.read(), d)
def testQuoting(self):
safeunquoted = string.ascii_letters + string.digits + '@%_-+=:,./'
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
index baa1943..81c9036 100644
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -285,7 +285,8 @@ class TestShutil(unittest.TestCase):
if hasattr(os, "link"):
os.link(src, dst)
self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
- self.assertEqual(open(src,'r').read(), 'cheddar')
+ with open(src, 'r') as f:
+ self.assertEqual(f.read(), 'cheddar')
os.remove(dst)
# Using `src` here would mean we end up with a symlink pointing
@@ -293,7 +294,8 @@ class TestShutil(unittest.TestCase):
# TESTFN/cheese.
os.symlink('cheese', dst)
self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
- self.assertEqual(open(src,'r').read(), 'cheddar')
+ with open(src, 'r') as f:
+ self.assertEqual(f.read(), 'cheddar')
os.remove(dst)
finally:
try:
@@ -690,9 +692,11 @@ class TestMove(unittest.TestCase):
pass
def _check_move_file(self, src, dst, real_dst):
- contents = open(src, "rb").read()
+ with open(src, "rb") as f:
+ contents = f.read()
shutil.move(src, dst)
- self.assertEqual(contents, open(real_dst, "rb").read())
+ with open(real_dst, "rb") as f:
+ self.assertEqual(contents, f.read())
self.assertFalse(os.path.exists(src))
def _check_move_dir(self, src, dst, real_dst):