Ticket #11812: 11812.3.patch

File 11812.3.patch, 5.3 KB (added by Marco Streng, 11 years ago)
  • sage/misc/all.py

    # HG changeset patch
    # User Marco Streng <marco.streng@gmail.com>
    # Date 1316701202 -3600
    # Node ID 35df90db0a09bca3f50506592740af9c693142d4
    # Parent  8a0b4f90f1ca76dbdba159897c39209c5da85442
    trac 11812: tracebacks for attach
    
    diff --git a/sage/misc/all.py b/sage/misc/all.py
    a b  
    6161
    6262from defaults import set_default_variable_name
    6363
    64 from preparser import preparse, implicit_multiplication, BackslashOperator, attached_files, detach, load_attach_path, reset_load_attach_path
     64from preparser import preparse, implicit_multiplication, BackslashOperator, attached_files, detach, load_attach_path, reset_load_attach_path, load_attach_mode
    6565
    6666from interpreter import preparser
    6767
  • sage/misc/interpreter.py

    diff --git a/sage/misc/interpreter.py b/sage/misc/interpreter.py
    a b  
    159159
    160160    # Process attached files.
    161161    for F in modified_attached_files():
    162         _ip.runlines(load_wrap(F))
     162        # We attach the files again instead of loading them,
     163        # to preserve tracebacks or efficiency according
     164        # to the settings of load_attach_mode().
     165        _ip.runlines(load_wrap(F, attach=True))
    163166       
    164167    # Get rid of leading sage: prompts so that pasting of examples
    165168    # from the documentation works.  This is like MAGMA's
  • sage/misc/preparser.py

    diff --git a/sage/misc/preparser.py b/sage/misc/preparser.py
    a b  
    11381138######################################################
    11391139
    11401140attached = {}
     1141
     1142load_debug_mode = False
     1143attach_debug_mode = True
     1144
    11411145def preparse_file(contents, globals=None, numeric_literals=True):
    11421146    """
    11431147    Preparses input, attending to numeric literals and load/attach
     
    11821186    for F, tm in attached.iteritems():
    11831187        new_tm = os.path.getmtime(F)
    11841188        if os.path.exists(F) and new_tm > tm:
    1185             contents = 'load "%s"\n'%F + contents
    1186         attached[F] = new_tm
     1189            contents = 'attach "%s"\n'%F + contents
    11871190   
    11881191    # We keep track of which files have been loaded so far
    11891192    # in order to avoid a recursive load that would result
     
    14801483    ``filename`` itself is also evaluated in the scope.  If the name
    14811484    starts with ``http://``, it is treated as a URL and downloaded.
    14821485
    1483     .. note:: For Cython files, the situation is more complicated --
    1484        the module is first compiled to a temporary module ``t`` and
    1485        executed via::
     1486    .. NOTE::
    14861487
    1487            from t import *
     1488        For Cython files, the situation is more complicated --
     1489        the module is first compiled to a temporary module ``t`` and
     1490        executed via::
     1491
     1492            from t import *
    14881493
    14891494    INPUT:
    14901495
     
    16301635    if fpath.endswith('.py'):
    16311636        execfile(fpath, globals)
    16321637    elif fpath.endswith('.sage'):
    1633         exec(preparse_file(open(fpath).read()) + "\n", globals)
     1638        if (attach and attach_debug_mode) or ((not attach) and load_debug_mode):
     1639            # Preparse to a file to enable tracebacks with
     1640            # code snippets. Use preparse_file_named to make
     1641            # the file name appear in the traceback as well.
     1642            # See Trac 11812.
     1643            from sage.misc.interpreter import preparse_file_named         
     1644            execfile(preparse_file_named(fpath), globals)
     1645        else:
     1646            # Preparse in memory only for speed.
     1647            exec(preparse_file(open(fpath).read()) + "\n", globals)
    16341648    elif fpath.endswith('.spyx') or fpath.endswith('.pyx'):
    16351649        import interpreter
    16361650        exec(interpreter.load_cython(fpath), globals)
     
    16481662        attached[fpath] = os.path.getmtime(fpath)
    16491663
    16501664
     1665def load_attach_mode(load_debug=None, attach_debug=None):
     1666    """
     1667    Get or modify the current debug mode for loading and attaching
     1668    .sage files.
     1669
     1670    In debug mode, loaded or attached .sage files are preparsed
     1671    through a file to make their tracebacks more informative.
     1672    If not in debug mode, then .sage files are preparsed
     1673    in memory only for performance.
     1674
     1675    At startup, debug mode is ``True`` for attaching and ``False``
     1676    for loading.
     1677   
     1678    INPUT:
     1679
     1680    - ``load_debug`` - boolean or (the default) ``None``;
     1681      if not ``None``, then set a new value for the debug mode
     1682      for loading files.
     1683
     1684    - ``attach_debug`` - boolean or (the default) ``None``;
     1685      same as ``load_debug``, but for attaching files.
     1686
     1687    OUTPUT:
     1688
     1689    - if all input values are ``None``, returns a tuple
     1690      giving the current modes for loading and attaching.
     1691   
     1692    EXAMPLES::
     1693
     1694        sage: load_attach_mode()
     1695        (False, True)
     1696        sage: load_attach_mode(attach_debug=False)
     1697        sage: load_attach_mode()
     1698        (False, False)
     1699        sage: load_attach_mode(load_debug=True)
     1700        sage: load_attach_mode()
     1701        (True, False)
     1702        sage: load_attach_mode(load_debug=False, attach_debug=True)
     1703    """
     1704    global load_debug_mode
     1705    global attach_debug_mode
     1706    if load_debug is None and attach_debug is None:
     1707        return (load_debug_mode, attach_debug_mode)
     1708    if not load_debug is None:
     1709        load_debug_mode = load_debug
     1710    if not attach_debug is None:
     1711        attach_debug_mode = attach_debug
     1712
     1713
    16511714def load_attach_path(path=None, replace=False):
    16521715    """
    16531716    Get or modify the current search path for loading and attaching