tests = [ ("import hell.first_circle" , "('hell.first_circle', None)" ), ("import hell.first_circle as limbo", "('hell.first_circle', None)" ), ("from hell.ninth_circle import *" , "('hell.ninth_circle', ('*',))"), ("from hell.ninth_circle import antaeus", "('hell.ninth_circle', ('antaeus',))"), ("from hell.ninth_circle import antaeus as giant", "('hell.ninth_circle', ('antaeus',))") ] import sys # Replace __builtin__.__import__ to trace the calls import __builtin__ def __import__(name, globs, locs, fromlist): global result result = str((name, fromlist)) raise ImportError() __builtin__.__import__ = __import__ if __name__ == "__main__": failed = 0 for statement, expected in tests: print 50*"-" print "statement:", statement print " - expected:", expected try: exec(statement) except ImportError: pass print " - got: ", result if expected != result: failed += 1 if failed: print print "*** FAILURE:", failed, "errors ***" print sys.exit(failed)