changeset 76937:a995e0466061

(format_exception): New function. (eexecfile): Use it instead of traceback.print_exception. Don't use execfile to avoid a bug in w32.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Wed, 04 Apr 2007 14:48:47 +0000
parents 22f62f97ae60
children c43eb2a211a9
files etc/ChangeLog etc/emacs.py
diffstat 2 files changed, 55 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/etc/ChangeLog	Wed Apr 04 14:06:52 2007 +0000
+++ b/etc/ChangeLog	Wed Apr 04 14:48:47 2007 +0000
@@ -1,3 +1,9 @@
+2007-04-04  Slawomir Nowaczyk  <slawomir.nowaczyk.847@student.lu.se>
+
+	* emacs.py (format_exception): New function.
+	(eexecfile): Use it instead of traceback.print_exception.
+	Don't use execfile to avoid a bug in w32.
+
 2007-04-04  Glenn Morris  <rgm@gnu.org>
 
 	* MACHINES: Mention preprocessor to use with /opt/SUNWspro/bin/cc
@@ -367,7 +373,7 @@
 	(section{TODO Items and Checkboxes}): Checkbox keys moved to this
 	section, added documentation for the key `C-c #'.
 
-2006-11-05  Slawomir Nowaczyk  <slawek@cs.lth.se>  (tiny change)
+2006-11-05  Slawomir Nowaczyk  <slawek@cs.lth.se>
 
 	* emacs.py (eargs): Provide eldoc message for builtin types.
 	  Make sure eargs always outputs sentinel, to avoid Emacs freeze.
@@ -462,7 +468,7 @@
 	(eimport): Use __main__ rather than `emacs' namespace.
 	(modpath): New fun.
 
-2006-08-20  Slawomir Nowaczyk  <slawomir.nowaczyk.847@student.lu.se>  (tiny change)
+2006-08-20  Slawomir Nowaczyk  <slawomir.nowaczyk.847@student.lu.se>
 
 	* emacs.py (eexecfile): Use the __main__ rather than `emacs' namespace.
 
@@ -725,8 +731,8 @@
 	* pl-refcard.tex (section{Info}): Ditto.  Translation suggested by
 	Slawomir Nowaczyk <slawomir.nowaczyk.847@student.lu.se>.
 
-	* cs-refcard.tex (section{Info}): Use `s' instead of `M-s'.  Entry
-	for `i' is not translated yet.
+	* cs-refcard.tex (section{Info}): Use `s' instead of `M-s'.
+	Entry for `i' is not translated yet.
 
 	* pt-br-refcard.tex (section{Info}): Ditto.
 
@@ -1455,7 +1461,7 @@
 
 	* NEWS: Lots of clarifications and cleanups.
 
-2005-05-05  Slawomir Nowaczyk  <slawek@cs.lth.se>  (tiny change)
+2005-05-05  Slawomir Nowaczyk  <slawek@cs.lth.se>
 
 	* TUTORIAL.pl: Updated header.
 
--- a/etc/emacs.py	Wed Apr 04 14:06:52 2007 +0000
+++ b/etc/emacs.py	Wed Apr 04 14:48:47 2007 +0000
@@ -25,20 +25,56 @@
 
 __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)