diff -r 6f0e49ed0589 Lib/test/test_imp.py --- a/Lib/test/test_imp.py Wed Nov 14 11:19:42 2012 +0000 +++ b/Lib/test/test_imp.py Wed Nov 14 19:02:04 2012 -0300 @@ -7,6 +7,8 @@ from test import support import unittest import warnings +import compileall +from contextlib import contextmanager class LockTests(unittest.TestCase): @@ -42,8 +44,70 @@ self.fail("release_lock() without lock should raise " "RuntimeError") -class ImportTests(unittest.TestCase): + +@contextmanager +def ExpandRestoreSysPath(addendum): + oldpath = sys.path[:] + sys.path.append(addendum) + try: + yield + finally: + sys.path = oldpath + + +class TestModules(unittest.TestCase): def setUp(self): + self._cleanUp = [] + + # setup compiled module + self.compiledModuleName = 'simplest' + + # 1st. create a simple module having only a `OK = True` statement + fname = self.compiledModuleName+'.py' + with open(fname, 'wb') as f: + f.write(b'OK = True') + # 2nd. compile it (to get its .pyc file data..) + compileall.py_compile.compile(fname) + self._cleanUp.append(fname) + + # 3rd. import it to get .pyc path.. + with support.CleanImport(self.compiledModuleName): + with ExpandRestoreSysPath('.'): + module = __import__(self.compiledModuleName) + compiledFile = module.__cached__ + + os.unlink(fname) + self._cleanUp.append(compiledFile) + + # 4th. get .pyc data + with open(compiledFile,'rb') as f: + compiled = f.read() + + # 5th. save to current working directory, as `modulename.pyc` + loadableCompiled = self.compiledModuleName+'.pyc' + with open(loadableCompiled, 'wb') as f: + f.write(compiled) + self._cleanUp.append(loadableCompiled) + + self.ImportModuleTests = { + imp.C_EXTENSION: 'time', + imp.C_BUILTIN:'marshal', + imp.PY_FROZEN:'__hello__', + imp.PKG_DIRECTORY:'distutils', + imp.PY_COMPILED: self.compiledModuleName, + } + + def tearDown(self): + for fname in self._cleanUp: + try: + os.unlink(fname) + except: + pass + + +class ImportTests(TestModules): + def setUp(self): + super(ImportTests, self).setUp() mod = importlib.import_module('test.encoded_modules') self.test_strings = mod.test_strings self.test_path = mod.__path__ @@ -205,6 +269,19 @@ self.assertIs(orig_path, new_os.path) self.assertIsNot(orig_getenv, new_os.getenv) + def test_load_by_kind(self): + for kind, modname in self.ImportModuleTests.items(): + with ExpandRestoreSysPath('.'): # used for compiled only.. + self._check_kind_modname(kind, modname) + + def _check_kind_modname(self, kind, modname): + _file, _path, _details = imp.find_module(modname) + if _file: + self.addCleanup(_file.close) + self.assertEqual(_details[2], kind) + imp.load_module(modname, _file, _path, _details) + + @support.cpython_only def test_issue15828_load_extensions(self): # Issue 15828 picked up that the adapter between the old imp API @@ -229,8 +306,7 @@ self.assertEqual(name, err.exception.name) -class ReloadTests(unittest.TestCase): - +class ReloadTests(TestModules): """Very basic tests to make sure that imp.reload() operates just like reload().""" @@ -245,16 +321,19 @@ with support.EnvironmentVarGuard(): import os imp.reload(os) + + def try_reload_kind(self, kind): + name = self.ImportModuleTests[kind] + with support.CleanImport(name): + module = __import__(name) + imp.reload(module) def test_extension(self): - with support.CleanImport('time'): - import time - imp.reload(time) + self.try_reload_kind(imp.C_EXTENSION) def test_builtin(self): - with support.CleanImport('marshal'): - import marshal - imp.reload(marshal) + self.try_reload_kind(imp.C_BUILTIN) + class PEP3147Tests(unittest.TestCase):