import inspect, sys # /etc/hostname is one line, but our source has multiple lines code = compile('\n\n\n1/0', '/etc/hostname', 'exec') try: exec(code, {}) except Exception: tb = sys.exc_info()[2] else: assert False, "unreachable, exec should always raise exception" # this fails with an IndexError inspect.getinnerframes(tb)