| | 27 | |
|---|
| | 28 | def format_exception (filename, should_remove_self): |
|---|
| | 29 | type, value, tb = sys.exc_info () |
|---|
| | 30 | sys.last_type = type |
|---|
| | 31 | sys.last_value = value |
|---|
| | 32 | sys.last_traceback = tb |
|---|
| | 33 | if type is SyntaxError: |
|---|
| | 34 | try: # parse the error message |
|---|
| | 35 | msg, (dummy_filename, lineno, offset, line) = value |
|---|
| | 36 | except: |
|---|
| | 37 | pass # Not the format we expect; leave it alone |
|---|
| | 38 | else: |
|---|
| | 39 | # Stuff in the right filename |
|---|
| | 40 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|---|
| | 41 | sys.last_value = value |
|---|
| | 42 | res = traceback.format_exception_only (type, value) |
|---|
| | 43 | # There are some compilation errors which do not provide traceback so we |
|---|
| | 44 | # should not massage it. |
|---|
| | 45 | if should_remove_self: |
|---|
| | 46 | tblist = traceback.extract_tb (tb) |
|---|
| | 47 | del tblist[:1] |
|---|
| | 48 | res = traceback.format_list (tblist) |
|---|
| | 49 | if res: |
|---|
| | 50 | res.insert(0, "Traceback (most recent call last):\n") |
|---|
| | 51 | res[len(res):] = traceback.format_exception_only (type, value) |
|---|
| | 52 | # traceback.print_exception(type, value, tb) |
|---|
| | 53 | for line in res: print line, |
|---|
| 33 | | try: |
|---|
| 34 | | try: execfile (file, __main__.__dict__) |
|---|
| 35 | | except: |
|---|
| 36 | | (type, value, tb) = sys.exc_info () |
|---|
| 37 | | # Lose the stack frame for this location. |
|---|
| 38 | | tb = tb.tb_next |
|---|
| 39 | | if tb is None: # print_exception won't do it |
|---|
| 40 | | print "Traceback (most recent call last):" |
|---|
| 41 | | traceback.print_exception (type, value, tb) |
|---|
| | 60 | # We cannot use real execfile since it has a bug where the file stays |
|---|
| | 61 | # locked forever (under w32) if SyntaxError occurs. |
|---|
| | 62 | # --- code based on code.py and PyShell.py. |
|---|
| | 63 | try: |
|---|
| | 64 | try: |
|---|
| | 65 | source = open (file, "r").read() |
|---|
| | 66 | code = compile (source, file, "exec") |
|---|
| | 67 | # Other exceptions (shouldn't be any...) will (correctly) fall |
|---|
| | 68 | # through to "final". |
|---|
| | 69 | except (OverflowError, SyntaxError, ValueError): |
|---|
| | 70 | # FIXME: When can compile() raise anything else than |
|---|
| | 71 | # SyntaxError ???? |
|---|
| | 72 | format_exception (file, False) |
|---|
| | 73 | return |
|---|
| | 74 | try: |
|---|
| | 75 | exec code in __main__.__dict__ |
|---|
| | 76 | except: |
|---|
| | 77 | format_exception (file, True) |
|---|