(original) (raw)
changeset: 69955:3ab1eb027856 branch: 3.1 parent: 69950:e4acbe2c8164 user: Ezio Melotti ezio.melotti@gmail.com date: Mon May 09 06:41:55 2011 +0300 files: Lib/test/support.py description: #11910: change import_fresh_module to return None when one of the "fresh" modules can not be imported. diff -r e4acbe2c8164 -r 3ab1eb027856 Lib/test/support.py --- a/Lib/test/support.py Mon May 09 03:54:30 2011 +0300 +++ b/Lib/test/support.py Mon May 09 06:41:55 2011 +0300 @@ -80,12 +80,14 @@ def _save_and_remove_module(name, orig_modules): """Helper function to save and remove a module from sys.modules - Return value is True if the module was in sys.modules and - False otherwise.""" + Return True if the module was in sys.modules, False otherwise. + Raise ImportError if the module can't be imported.""" saved = True try: orig_modules[name] = sys.modules[name] except KeyError: + # try to import the module and raise an error if it can't be imported + __import__(name) saved = False else: del sys.modules[name] @@ -95,8 +97,7 @@ def _save_and_block_module(name, orig_modules): """Helper function to save and block a module in sys.modules - Return value is True if the module was in sys.modules and - False otherwise.""" + Return True if the module was in sys.modules, False otherwise.""" saved = True try: orig_modules[name] = sys.modules[name] @@ -112,6 +113,7 @@ the sys.modules cache is restored to its original state. Modules named in fresh are also imported anew if needed by the import. + If one of these modules can't be imported, None is returned. Importing of modules named in blocked is prevented while the fresh import takes place. @@ -133,6 +135,8 @@ if not _save_and_block_module(blocked_name, orig_modules): names_to_remove.append(blocked_name) fresh_module = importlib.import_module(name) + except ImportError: + fresh_module = None finally: for orig_name, module in orig_modules.items(): sys.modules[orig_name] = module /ezio.melotti@gmail.com