Ticket #11812: 11812.2.patch

File 11812.2.patch, 6.0 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 120efc8c9cbb9a7521f48d4413ca076473da9400
    # Parent  ce324e28c3334398d3552640e2cb1520d22465a3
    trac 11812: tracebacks for attach
    * * *
    Fix conversion of elements of one number field to another
    
    diff -r ce324e28c333 -r 120efc8c9cbb sage/misc/all.py
    a b  
    5959
    6060from defaults import set_default_variable_name
    6161
    62 from preparser import preparse, implicit_multiplication, BackslashOperator, attached_files, detach, load_attach_path, reset_load_attach_path
     62from preparser import preparse, implicit_multiplication, BackslashOperator, attached_files, detach, load_attach_path, reset_load_attach_path, load_attach_mode
    6363
    6464from interpreter import preparser
    6565
  • sage/misc/interpreter.py

    diff -r ce324e28c333 -r 120efc8c9cbb 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 -r ce324e28c333 -r 120efc8c9cbb sage/misc/preparser.py
    a b  
    11051105######################################################
    11061106
    11071107attached = {}
     1108
     1109load_debug_mode = False
     1110attach_debug_mode = True
     1111
    11081112def preparse_file(contents, globals=None, numeric_literals=True):
    11091113    """
    11101114    Preparses input, attending to numeric literals and load/attach
     
    11491153    for F, tm in attached.iteritems():
    11501154        new_tm = os.path.getmtime(F)
    11511155        if os.path.exists(F) and new_tm > tm:
    1152             contents = 'load "%s"\n'%F + contents
    1153         attached[F] = new_tm
     1156            contents = 'attach "%s"\n'%F + contents
    11541157   
    11551158    # We keep track of which files have been loaded so far
    11561159    # in order to avoid a recursive load that would result
     
    14431446    ``filename`` itself is also evaluated in the scope.  If the name
    14441447    starts with ``http://``, it is treated as a URL and downloaded.
    14451448
    1446     .. note:: For Cython files, the situation is more complicated --
    1447        the module is first compiled to a temporary module ``t`` and
    1448        executed via::
     1449    .. NOTE::
    14491450
    1450            from t import *
     1451        For Cython files, the situation is more complicated --
     1452        the module is first compiled to a temporary module ``t`` and
     1453        executed via::
     1454
     1455            from t import *
    14511456
    14521457    INPUT:
    14531458
     
    14571462    - ``globals`` -- a string:object dictionary; the context in which
    14581463      to evaluate the ``filename`` and exec its contents
    14591464
    1460     - ``attach`` -- a boolean (default: False); whether to add the
    1461       file to the list of attached files
    1462 
     1465    - ``attach`` -- a boolean (default: ``False``); whether to add the
     1466      file to the list of attached files.
     1467     
    14631468    EXAMPLES:
    14641469
    14651470    Note that .py files are *not* preparsed::
     
    15931598    if fpath.endswith('.py'):
    15941599        execfile(fpath, globals)
    15951600    elif fpath.endswith('.sage'):
    1596         exec(preparse_file(open(fpath).read()) + "\n", globals)
     1601        if (attach and attach_debug_mode) or ((not attach) and load_debug_mode):
     1602            # Preparse to a file to enable tracebacks with
     1603            # code snippets. Use preparse_file_named to make
     1604            # the file name appear in the traceback as well.
     1605            # See Trac 11812.
     1606            from sage.misc.interpreter import preparse_file_named         
     1607            execfile(preparse_file_named(fpath), globals)
     1608        else:
     1609            # Preparse in memory only for speed.
     1610            exec(preparse_file(open(fpath).read()) + "\n", globals)
    15971611    elif fpath.endswith('.spyx') or fpath.endswith('.pyx'):
    15981612        import interpreter
    15991613        exec(interpreter.load_cython(fpath), globals)
     
    16111625        attached[fpath] = os.path.getmtime(fpath)
    16121626
    16131627
     1628def load_attach_mode(load_debug=None, attach_debug=None):
     1629    """
     1630    Get or modify the current debug mode for loading and attaching
     1631    .sage files.
     1632
     1633    In debug mode, loaded or attached .sage files are preparsed
     1634    through a file to make their tracebacks more informative.
     1635    If not in debug mode, then .sage files are preparsed
     1636    in memory only for performance.
     1637
     1638    At startup, debug mode is ``True`` for attaching and ``False``
     1639    for loading.
     1640   
     1641    INPUT:
     1642
     1643    - ``load_debug`` - boolean or (the default) ``None``;
     1644      if not ``None``, then set a new value for the debug mode
     1645      for loading files.
     1646
     1647    - ``attach_debug`` - boolean or (the default) ``None``;
     1648      same as ``load_debug``, but for attaching files.
     1649
     1650    OUTPUT:
     1651
     1652    - if all input values ``None``, returns a tuple
     1653      giving the current modes for loading and attaching.
     1654   
     1655    EXAMPLES::
     1656
     1657        sage: load_attach_debug()
     1658        (False, True)
     1659        sage: load_attach_debug(attach_debug=False)
     1660        sage: load_attach_debug()
     1661        (False, False)
     1662        sage: load_attach_debug(load_debug=True)
     1663        sage: load_attach_debug()
     1664        (True, False)
     1665        sage: load_attach_debug(load_debug=False, attach_debug=True)
     1666    """
     1667    global load_debug_mode
     1668    global attach_debug_mode
     1669    if load_debug is None and attach_debug is None:
     1670        return (load_debug_mode, attach_debug_mode)
     1671    if not load_debug is None:
     1672        load_debug_mode = load_debug
     1673    if not attach_debug is None:
     1674        attach_debug_mode = attach_debug
     1675
     1676
    16141677def load_attach_path(path=None, replace=False):
    16151678    """
    16161679    Get or modify the current search path for loading and attaching