# HG changeset patch
# User Marco Streng <marco.streng@gmail.com>
# Date 1316701202 -3600
# Node ID aba32edef21191b05ac8d63b64fbeec3ee6bd442
# Parent  ce324e28c3334398d3552640e2cb1520d22465a3
trac 11812: tracebacks for attach

diff -r ce324e28c333 -r aba32edef211 sage/misc/preparser.py
--- a/sage/misc/preparser.py	Mon May 23 13:36:51 2011 +0000
+++ b/sage/misc/preparser.py	Thu Sep 22 15:20:02 2011 +0100
@@ -1437,17 +1437,19 @@
 # user can modify the path with the function load_attach_path.
 reset_load_attach_path()
 
-def load(filename, globals, attach=False):
+def load(filename, globals, attach=False, preparse_to_file=None):
     """
     Executes a file in the scope given by ``globals``.  The
     ``filename`` itself is also evaluated in the scope.  If the name
     starts with ``http://``, it is treated as a URL and downloaded.
 
-    .. note:: For Cython files, the situation is more complicated --
-       the module is first compiled to a temporary module ``t`` and
-       executed via::
+    .. NOTE::
 
-           from t import *
+        For Cython files, the situation is more complicated --
+        the module is first compiled to a temporary module ``t`` and
+        executed via::
+
+            from t import *
 
     INPUT:
 
@@ -1457,8 +1459,13 @@
     - ``globals`` -- a string:object dictionary; the context in which
       to evaluate the ``filename`` and exec its contents
 
-    - ``attach`` -- a boolean (default: False); whether to add the
+    - ``attach`` -- a boolean (default: ``False``); whether to add the
       file to the list of attached files
+      
+    - ``preparse_to_file`` -- a boolean or (the default) ``None``;
+      whether to preparse .sage files to a file (for usable tracebacks)
+      or only to memory (for speed). If ``None``, then take
+      ``preparse_to_file=attach``.
 
     EXAMPLES:
 
@@ -1558,7 +1565,7 @@
         v = filename.split()
         if len(v) > 1:
             for file in v:
-                load(file, globals, attach=attach)
+                load(file, globals, attach=attach, preparse_to_file=preparse_to_file)
             return
 
     filename = filename.strip()
@@ -1593,7 +1600,18 @@
     if fpath.endswith('.py'):
         execfile(fpath, globals)
     elif fpath.endswith('.sage'):
-        exec(preparse_file(open(fpath).read()) + "\n", globals)
+        if preparse_to_file is None:
+            preparse_to_file = attach
+        if preparse_to_file:
+            # Preparse to a file to enable tracebacks with
+            # code snippets. Use preparse_file_named to make
+            # the file name appear in the traceback as well.
+            # See Trac 11812.
+            from sage.misc.interpreter import preparse_file_named         
+            execfile(preparse_file_named(fpath), globals)
+        else:
+            # Preparse in memory only for speed.
+            exec(preparse_file(open(fpath).read()) + "\n", globals)
     elif fpath.endswith('.spyx') or fpath.endswith('.pyx'):
         import interpreter
         exec(interpreter.load_cython(fpath), globals)
