Commit 99d848bd authored by Antoine Pitrou's avatar Antoine Pitrou

Merged revisions 85503 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r85503 | antoine.pitrou | 2010-10-15 00:11:44 +0200 (ven., 15 oct. 2010) | 2 lines

  More proper closing of files
........
parent 80800d35
...@@ -216,7 +216,8 @@ def _load_testfile(filename, package, module_relative): ...@@ -216,7 +216,8 @@ def _load_testfile(filename, package, module_relative):
# get_data() opens files as 'rb', so one must do the equivalent # get_data() opens files as 'rb', so one must do the equivalent
# conversion as universal newlines would do. # conversion as universal newlines would do.
return file_contents.replace(os.linesep, '\n'), filename return file_contents.replace(os.linesep, '\n'), filename
return open(filename).read(), filename with open(filename) as f:
return f.read(), filename
# Use sys.stdout encoding for ouput. # Use sys.stdout encoding for ouput.
_encoding = getattr(sys.__stdout__, 'encoding', None) or 'utf-8' _encoding = getattr(sys.__stdout__, 'encoding', None) or 'utf-8'
......
...@@ -224,12 +224,13 @@ class DecimalTest(unittest.TestCase): ...@@ -224,12 +224,13 @@ class DecimalTest(unittest.TestCase):
if skip_expected: if skip_expected:
raise unittest.SkipTest raise unittest.SkipTest
return return
for line in open(file): with open(file) as f:
for line in f:
line = line.replace('\r\n', '').replace('\n', '') line = line.replace('\r\n', '').replace('\n', '')
#print line #print line
try: try:
t = self.eval_line(line) t = self.eval_line(line)
except DecimalException, exception: except DecimalException as exception:
#Exception raised where there shoudn't have been one. #Exception raised where there shoudn't have been one.
self.fail('Exception "'+exception.__class__.__name__ + '" raised on line '+line) self.fail('Exception "'+exception.__class__.__name__ + '" raised on line '+line)
......
...@@ -211,11 +211,17 @@ def open_file(path): ...@@ -211,11 +211,17 @@ def open_file(path):
def create_package(source): def create_package(source):
ofi = None ofi = None
try:
for line in source.splitlines(): for line in source.splitlines():
if line.startswith(" ") or line.startswith("\t"): if line.startswith(" ") or line.startswith("\t"):
ofi.write(line.strip() + "\n") ofi.write(line.strip() + "\n")
else: else:
if ofi:
ofi.close()
ofi = open_file(os.path.join(TEST_DIR, line.strip())) ofi = open_file(os.path.join(TEST_DIR, line.strip()))
finally:
if ofi:
ofi.close()
class ModuleFinderTest(unittest.TestCase): class ModuleFinderTest(unittest.TestCase):
def _do_test(self, info, report=False): def _do_test(self, info, report=False):
......
...@@ -46,12 +46,9 @@ class Test_MultibyteCodec(unittest.TestCase): ...@@ -46,12 +46,9 @@ class Test_MultibyteCodec(unittest.TestCase):
'apple\x92ham\x93spam', 'test.cjktest') 'apple\x92ham\x93spam', 'test.cjktest')
def test_codingspec(self): def test_codingspec(self):
try:
for enc in ALL_CJKENCODINGS: for enc in ALL_CJKENCODINGS:
print >> open(TESTFN, 'w'), '# coding:', enc code = '# coding: {}\n'.format(enc)
exec open(TESTFN) exec code
finally:
os.unlink(TESTFN)
def test_init_segfault(self): def test_init_segfault(self):
# bug #3305: this used to segfault # bug #3305: this used to segfault
......
...@@ -252,7 +252,7 @@ class TestBase_Mapping(unittest.TestCase): ...@@ -252,7 +252,7 @@ class TestBase_Mapping(unittest.TestCase):
def __init__(self, *args, **kw): def __init__(self, *args, **kw):
unittest.TestCase.__init__(self, *args, **kw) unittest.TestCase.__init__(self, *args, **kw)
try: 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): except (IOError, HTTPException):
self.skipTest("Could not retrieve "+self.mapfileurl) self.skipTest("Could not retrieve "+self.mapfileurl)
...@@ -270,7 +270,8 @@ class TestBase_Mapping(unittest.TestCase): ...@@ -270,7 +270,8 @@ class TestBase_Mapping(unittest.TestCase):
unichrs = lambda s: u''.join(_unichr(c) for c in s.split('+')) unichrs = lambda s: u''.join(_unichr(c) for c in s.split('+'))
urt_wa = {} urt_wa = {}
for line in self.open_mapping_file(): with self.open_mapping_file() as f:
for line in f:
if not line: if not line:
break break
data = line.split('#')[0].strip().split() data = line.split('#')[0].strip().split()
...@@ -299,7 +300,8 @@ class TestBase_Mapping(unittest.TestCase): ...@@ -299,7 +300,8 @@ class TestBase_Mapping(unittest.TestCase):
self._testpoint(csetch, unich) self._testpoint(csetch, unich)
def _test_mapping_file_ucm(self): 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) uc = re.findall('<a u="([A-F0-9]{4})" b="([0-9A-F ]+)"/>', ucmdata)
for uni, coded in uc: for uni, coded in uc:
unich = unichr(int(uni, 16)) unich = unichr(int(uni, 16))
......
...@@ -23,17 +23,21 @@ class SimplePipeTests(unittest.TestCase): ...@@ -23,17 +23,21 @@ class SimplePipeTests(unittest.TestCase):
f = t.open(TESTFN, 'w') f = t.open(TESTFN, 'w')
f.write('hello world #1') f.write('hello world #1')
f.close() 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): def testSimplePipe2(self):
file(TESTFN, 'w').write('hello world #2') with open(TESTFN, 'w') as f:
f.write('hello world #2')
t = pipes.Template() t = pipes.Template()
t.append(s_command + ' < $IN > $OUT', pipes.FILEIN_FILEOUT) t.append(s_command + ' < $IN > $OUT', pipes.FILEIN_FILEOUT)
t.copy(TESTFN, TESTFN2) 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): def testSimplePipe3(self):
file(TESTFN, 'w').write('hello world #2') with open(TESTFN, 'w') as f:
f.write('hello world #2')
t = pipes.Template() t = pipes.Template()
t.append(s_command + ' < $IN', pipes.FILEIN_STDOUT) t.append(s_command + ' < $IN', pipes.FILEIN_STDOUT)
with t.open(TESTFN, 'r') as f: with t.open(TESTFN, 'r') as f:
...@@ -42,16 +46,20 @@ class SimplePipeTests(unittest.TestCase): ...@@ -42,16 +46,20 @@ class SimplePipeTests(unittest.TestCase):
def testEmptyPipeline1(self): def testEmptyPipeline1(self):
# copy through empty pipe # copy through empty pipe
d = 'empty pipeline test COPY' d = 'empty pipeline test COPY'
file(TESTFN, 'w').write(d) with open(TESTFN, 'w') as f:
file(TESTFN2, 'w').write('') f.write(d)
with open(TESTFN2, 'w') as f:
f.write('')
t=pipes.Template() t=pipes.Template()
t.copy(TESTFN, TESTFN2) t.copy(TESTFN, TESTFN2)
self.assertEqual(open(TESTFN2).read(), d) with open(TESTFN2) as f:
self.assertEqual(f.read(), d)
def testEmptyPipeline2(self): def testEmptyPipeline2(self):
# read through empty pipe # read through empty pipe
d = 'empty pipeline test READ' d = 'empty pipeline test READ'
file(TESTFN, 'w').write(d) with open(TESTFN, 'w') as f:
f.write(d)
t=pipes.Template() t=pipes.Template()
with t.open(TESTFN, 'r') as f: with t.open(TESTFN, 'r') as f:
self.assertEqual(f.read(), d) self.assertEqual(f.read(), d)
...@@ -60,8 +68,10 @@ class SimplePipeTests(unittest.TestCase): ...@@ -60,8 +68,10 @@ class SimplePipeTests(unittest.TestCase):
# write through empty pipe # write through empty pipe
d = 'empty pipeline test WRITE' d = 'empty pipeline test WRITE'
t = pipes.Template() t = pipes.Template()
t.open(TESTFN, 'w').write(d) with t.open(TESTFN, 'w') as f:
self.assertEqual(open(TESTFN).read(), d) f.write(d)
with open(TESTFN) as f:
self.assertEqual(f.read(), d)
def testQuoting(self): def testQuoting(self):
safeunquoted = string.ascii_letters + string.digits + '@%_-+=:,./' safeunquoted = string.ascii_letters + string.digits + '@%_-+=:,./'
......
...@@ -277,7 +277,8 @@ class TestShutil(unittest.TestCase): ...@@ -277,7 +277,8 @@ class TestShutil(unittest.TestCase):
os.link(src, dst) os.link(src, dst)
self.assertRaises(shutil.Error, shutil.copyfile, 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) os.remove(dst)
# Using `src` here would mean we end up with a symlink pointing # Using `src` here would mean we end up with a symlink pointing
...@@ -285,7 +286,8 @@ class TestShutil(unittest.TestCase): ...@@ -285,7 +286,8 @@ class TestShutil(unittest.TestCase):
# TESTFN/cheese. # TESTFN/cheese.
os.symlink('cheese', dst) os.symlink('cheese', dst)
self.assertRaises(shutil.Error, shutil.copyfile, 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) os.remove(dst)
finally: finally:
try: try:
...@@ -588,9 +590,11 @@ class TestMove(unittest.TestCase): ...@@ -588,9 +590,11 @@ class TestMove(unittest.TestCase):
pass pass
def _check_move_file(self, src, dst, real_dst): 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) 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)) self.assertFalse(os.path.exists(src))
def _check_move_dir(self, src, dst, real_dst): def _check_move_dir(self, src, dst, real_dst):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment