Move test_imp over to file context managers.

This commit is contained in:
Brett Cannon 2010-10-29 23:47:23 +00:00
parent 105df5d75b
commit 749afa95ce
1 changed files with 22 additions and 22 deletions

View File

@ -55,27 +55,27 @@ class ImportTests(unittest.TestCase):
def test_find_module_encoding(self): def test_find_module_encoding(self):
for mod, encoding, _ in self.test_strings: for mod, encoding, _ in self.test_strings:
fd = imp.find_module('module_' + mod, self.test_path)[0] with imp.find_module('module_' + mod, self.test_path)[0] as fd:
self.assertEqual(fd.encoding, encoding) self.assertEqual(fd.encoding, encoding)
def test_issue1267(self): def test_issue1267(self):
for mod, encoding, _ in self.test_strings: for mod, encoding, _ in self.test_strings:
fp, filename, info = imp.find_module('module_' + mod, fp, filename, info = imp.find_module('module_' + mod,
self.test_path) self.test_path)
self.assertNotEqual(fp, None) with fp:
self.assertEqual(fp.encoding, encoding) self.assertNotEqual(fp, None)
self.assertEqual(fp.tell(), 0) self.assertEqual(fp.encoding, encoding)
self.assertEqual(fp.readline(), '# test %s encoding\n' self.assertEqual(fp.tell(), 0)
% encoding) self.assertEqual(fp.readline(), '# test %s encoding\n'
fp.close() % encoding)
fp, filename, info = imp.find_module("tokenize") fp, filename, info = imp.find_module("tokenize")
self.assertNotEqual(fp, None) with fp:
self.assertEqual(fp.encoding, "utf-8") self.assertNotEqual(fp, None)
self.assertEqual(fp.tell(), 0) self.assertEqual(fp.encoding, "utf-8")
self.assertEqual(fp.readline(), self.assertEqual(fp.tell(), 0)
'"""Tokenization help for Python programs.\n') self.assertEqual(fp.readline(),
fp.close() '"""Tokenization help for Python programs.\n')
def test_issue3594(self): def test_issue3594(self):
temp_mod_name = 'test_imp_helper' temp_mod_name = 'test_imp_helper'
@ -140,15 +140,15 @@ class ImportTests(unittest.TestCase):
with open(temp_mod_name + '.py', 'w') as file: with open(temp_mod_name + '.py', 'w') as file:
file.write('a = 1\n') file.write('a = 1\n')
file, filename, info = imp.find_module(temp_mod_name) file, filename, info = imp.find_module(temp_mod_name)
self.assertIsNotNone(file) with file:
self.assertTrue(filename[:-3].endswith(temp_mod_name)) self.assertIsNotNone(file)
self.assertEqual(info[0], '.py') self.assertTrue(filename[:-3].endswith(temp_mod_name))
self.assertEqual(info[1], 'U') self.assertEqual(info[0], '.py')
self.assertEqual(info[2], imp.PY_SOURCE) self.assertEqual(info[1], 'U')
self.assertEqual(info[2], imp.PY_SOURCE)
mod = imp.load_module(temp_mod_name, file, filename, info) mod = imp.load_module(temp_mod_name, file, filename, info)
self.assertEqual(mod.a, 1) self.assertEqual(mod.a, 1)
file.close()
mod = imp.load_source(temp_mod_name, temp_mod_name + '.py') mod = imp.load_source(temp_mod_name, temp_mod_name + '.py')
self.assertEqual(mod.a, 1) self.assertEqual(mod.a, 1)