summaryrefslogtreecommitdiff
path: root/etc/emacs.py
diff options
context:
space:
mode:
authorStefan Monnier <monnier@iro.umontreal.ca>2007-04-04 14:48:47 +0000
committerStefan Monnier <monnier@iro.umontreal.ca>2007-04-04 14:48:47 +0000
commitade817bdf2fbe321a756e44f5e3ef62a31f80fa2 (patch)
treebff821de158c181f4c522b3f7339107eeddc97bc /etc/emacs.py
parent8ea81ae0ef5d73b270ac8ebecdf20249eb4eab33 (diff)
(format_exception): New function.
(eexecfile): Use it instead of traceback.print_exception. Don't use execfile to avoid a bug in w32.
Diffstat (limited to 'etc/emacs.py')
-rw-r--r--etc/emacs.py52
1 files changed, 44 insertions, 8 deletions
diff --git a/etc/emacs.py b/etc/emacs.py
index e38ee70fab5..0d0f2e75ed3 100644
--- a/etc/emacs.py
+++ b/etc/emacs.py
@@ -25,20 +25,56 @@ from sets import Set
__all__ = ["eexecfile", "eargs", "complete", "ehelp", "eimport", "modpath"]
+def format_exception (filename, should_remove_self):
+ type, value, tb = sys.exc_info ()
+ sys.last_type = type
+ sys.last_value = value
+ sys.last_traceback = tb
+ if type is SyntaxError:
+ try: # parse the error message
+ msg, (dummy_filename, lineno, offset, line) = value
+ except:
+ pass # Not the format we expect; leave it alone
+ else:
+ # Stuff in the right filename
+ value = SyntaxError(msg, (filename, lineno, offset, line))
+ sys.last_value = value
+ res = traceback.format_exception_only (type, value)
+ # There are some compilation errors which do not provide traceback so we
+ # should not massage it.
+ if should_remove_self:
+ tblist = traceback.extract_tb (tb)
+ del tblist[:1]
+ res = traceback.format_list (tblist)
+ if res:
+ res.insert(0, "Traceback (most recent call last):\n")
+ res[len(res):] = traceback.format_exception_only (type, value)
+ # traceback.print_exception(type, value, tb)
+ for line in res: print line,
+
def eexecfile (file):
"""Execute FILE and then remove it.
Execute the file within the __main__ namespace.
If we get an exception, print a traceback with the top frame
(ourselves) excluded."""
+ # We cannot use real execfile since it has a bug where the file stays
+ # locked forever (under w32) if SyntaxError occurs.
+ # --- code based on code.py and PyShell.py.
try:
- try: execfile (file, __main__.__dict__)
- except:
- (type, value, tb) = sys.exc_info ()
- # Lose the stack frame for this location.
- tb = tb.tb_next
- if tb is None: # print_exception won't do it
- print "Traceback (most recent call last):"
- traceback.print_exception (type, value, tb)
+ try:
+ source = open (file, "r").read()
+ code = compile (source, file, "exec")
+ # Other exceptions (shouldn't be any...) will (correctly) fall
+ # through to "final".
+ except (OverflowError, SyntaxError, ValueError):
+ # FIXME: When can compile() raise anything else than
+ # SyntaxError ????
+ format_exception (file, False)
+ return
+ try:
+ exec code in __main__.__dict__
+ except:
+ format_exception (file, True)
finally:
os.remove (file)