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 ........
This commit is contained in:
parent
3d400b7a58
commit
ea5d827b72
|
@ -214,7 +214,8 @@ def _load_testfile(filename, package, module_relative, encoding):
|
||||||
# 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, encoding=encoding).read(), filename
|
with open(filename, encoding=encoding) as f:
|
||||||
|
return f.read(), filename
|
||||||
|
|
||||||
def _indent(s, indent=4):
|
def _indent(s, indent=4):
|
||||||
"""
|
"""
|
||||||
|
@ -2503,7 +2504,8 @@ def debug_script(src, pm=False, globs=None):
|
||||||
|
|
||||||
if pm:
|
if pm:
|
||||||
try:
|
try:
|
||||||
exec(open(srcfilename).read(), globs, globs)
|
with open(srcfilename) as f:
|
||||||
|
exec(f.read(), globs, globs)
|
||||||
except:
|
except:
|
||||||
print(sys.exc_info()[1])
|
print(sys.exc_info()[1])
|
||||||
pdb.post_mortem(sys.exc_info()[2])
|
pdb.post_mortem(sys.exc_info()[2])
|
||||||
|
|
|
@ -218,14 +218,15 @@ 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:
|
||||||
line = line.replace('\r\n', '').replace('\n', '')
|
for line in f:
|
||||||
#print line
|
line = line.replace('\r\n', '').replace('\n', '')
|
||||||
try:
|
#print line
|
||||||
t = self.eval_line(line)
|
try:
|
||||||
except DecimalException as exception:
|
t = self.eval_line(line)
|
||||||
#Exception raised where there shoudn't have been one.
|
except DecimalException as exception:
|
||||||
self.fail('Exception "'+exception.__class__.__name__ + '" raised on line '+line)
|
#Exception raised where there shoudn't have been one.
|
||||||
|
self.fail('Exception "'+exception.__class__.__name__ + '" raised on line '+line)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
@ -204,11 +204,17 @@ def open_file(path):
|
||||||
|
|
||||||
def create_package(source):
|
def create_package(source):
|
||||||
ofi = None
|
ofi = None
|
||||||
for line in source.splitlines():
|
try:
|
||||||
if line.startswith(" ") or line.startswith("\t"):
|
for line in source.splitlines():
|
||||||
ofi.write(line.strip() + "\n")
|
if line.startswith(" ") or line.startswith("\t"):
|
||||||
else:
|
ofi.write(line.strip() + "\n")
|
||||||
ofi = open_file(os.path.join(TEST_DIR, line.strip()))
|
else:
|
||||||
|
if ofi:
|
||||||
|
ofi.close()
|
||||||
|
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):
|
||||||
|
|
|
@ -49,8 +49,8 @@ class Test_MultibyteCodec(unittest.TestCase):
|
||||||
def test_codingspec(self):
|
def test_codingspec(self):
|
||||||
try:
|
try:
|
||||||
for enc in ALL_CJKENCODINGS:
|
for enc in ALL_CJKENCODINGS:
|
||||||
print('# coding:', enc, file=io.open(TESTFN, 'w'))
|
code = '# coding: {}\n'.format(enc)
|
||||||
exec(open(TESTFN).read())
|
exec(code)
|
||||||
finally:
|
finally:
|
||||||
support.unlink(TESTFN)
|
support.unlink(TESTFN)
|
||||||
|
|
||||||
|
|
|
@ -277,7 +277,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:
|
except IOError:
|
||||||
self.skipTest("Could not retrieve "+self.mapfileurl)
|
self.skipTest("Could not retrieve "+self.mapfileurl)
|
||||||
|
|
||||||
|
@ -294,36 +294,38 @@ class TestBase_Mapping(unittest.TestCase):
|
||||||
unichrs = lambda s: ''.join(map(chr, map(eval, s.split('+'))))
|
unichrs = lambda s: ''.join(map(chr, map(eval, s.split('+'))))
|
||||||
urt_wa = {}
|
urt_wa = {}
|
||||||
|
|
||||||
for line in self.open_mapping_file():
|
with self.open_mapping_file() as f:
|
||||||
if not line:
|
for line in f:
|
||||||
break
|
if not line:
|
||||||
data = line.split('#')[0].strip().split()
|
break
|
||||||
if len(data) != 2:
|
data = line.split('#')[0].strip().split()
|
||||||
continue
|
if len(data) != 2:
|
||||||
|
continue
|
||||||
|
|
||||||
csetval = eval(data[0])
|
csetval = eval(data[0])
|
||||||
if csetval <= 0x7F:
|
if csetval <= 0x7F:
|
||||||
csetch = bytes([csetval & 0xff])
|
csetch = bytes([csetval & 0xff])
|
||||||
elif csetval >= 0x1000000:
|
elif csetval >= 0x1000000:
|
||||||
csetch = bytes([(csetval >> 24), ((csetval >> 16) & 0xff),
|
csetch = bytes([(csetval >> 24), ((csetval >> 16) & 0xff),
|
||||||
((csetval >> 8) & 0xff), (csetval & 0xff)])
|
((csetval >> 8) & 0xff), (csetval & 0xff)])
|
||||||
elif csetval >= 0x10000:
|
elif csetval >= 0x10000:
|
||||||
csetch = bytes([(csetval >> 16), ((csetval >> 8) & 0xff),
|
csetch = bytes([(csetval >> 16), ((csetval >> 8) & 0xff),
|
||||||
(csetval & 0xff)])
|
(csetval & 0xff)])
|
||||||
elif csetval >= 0x100:
|
elif csetval >= 0x100:
|
||||||
csetch = bytes([(csetval >> 8), (csetval & 0xff)])
|
csetch = bytes([(csetval >> 8), (csetval & 0xff)])
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
unich = unichrs(data[1])
|
unich = unichrs(data[1])
|
||||||
if ord(unich) == 0xfffd or unich in urt_wa:
|
if ord(unich) == 0xfffd or unich in urt_wa:
|
||||||
continue
|
continue
|
||||||
urt_wa[unich] = csetch
|
urt_wa[unich] = csetch
|
||||||
|
|
||||||
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 = chr(int(uni, 16))
|
unich = chr(int(uni, 16))
|
||||||
|
|
|
@ -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):
|
||||||
open(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):
|
||||||
open(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)
|
||||||
self.assertEqual(t.open(TESTFN, 'r').read(), 'HELLO WORLD #2')
|
self.assertEqual(t.open(TESTFN, 'r').read(), 'HELLO WORLD #2')
|
||||||
|
@ -41,16 +45,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'
|
||||||
open(TESTFN, 'w').write(d)
|
with open(TESTFN, 'w') as f:
|
||||||
open(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'
|
||||||
open(TESTFN, 'w').write(d)
|
with open(TESTFN, 'w') as f:
|
||||||
|
f.write(d)
|
||||||
t=pipes.Template()
|
t=pipes.Template()
|
||||||
self.assertEqual(t.open(TESTFN, 'r').read(), d)
|
self.assertEqual(t.open(TESTFN, 'r').read(), d)
|
||||||
|
|
||||||
|
@ -58,8 +66,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 + '@%_-+=:,./'
|
||||||
|
|
|
@ -217,7 +217,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
|
||||||
|
@ -225,7 +226,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:
|
||||||
|
@ -307,9 +309,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):
|
||||||
|
|
Loading…
Reference in New Issue