Index: Lib/test/test_support.py =================================================================== --- Lib/test/test_support.py (revision 61589) +++ Lib/test/test_support.py (working copy) @@ -37,6 +37,13 @@ and unexpected skips. """ +class OptionalImportFailed(TestSkipped): + """Test skipped because an optional import failed. + + This is raised when a test calls optional_import() for a name that + can't be imported. + """ + verbose = 1 # Flag set to 0 by regrtest.py use_resources = None # Flag set to [] by regrtest.py max_memuse = 0 # Disable bigmem tests (they will still be run with @@ -288,7 +295,14 @@ fn, _ = urllib.urlretrieve(url, filename) return open(fn) +def optional_import(name, fromlist=[]): + """Tries to import name; the test is skipped on ImportError.""" + try: + return __import__(name, fromlist=fromlist) + except ImportError: + raise OptionalImportFailed("optional import of '%s' failed." % name) + class WarningMessage(object): "Holds the result of the latest showwarning() call" def __init__(self):