Ticket #11812: 11812.patch

File 11812.patch, 3.0 KB (added by Marco Streng, 11 years ago)

simpler patch: fixes the problem without adding complicated new doctests

  • sage/misc/preparser.py

    # 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 b  
    14371437# user can modify the path with the function load_attach_path.
    14381438reset_load_attach_path()
    14391439
    1440 def load(filename, globals, attach=False):
     1440def load(filename, globals, attach=False, preparse_to_file=None):
    14411441    """
    14421442    Executes a file in the scope given by ``globals``.  The
    14431443    ``filename`` itself is also evaluated in the scope.  If the name
    14441444    starts with ``http://``, it is treated as a URL and downloaded.
    14451445
    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::
     1446    .. NOTE::
    14491447
    1450            from t import *
     1448        For Cython files, the situation is more complicated --
     1449        the module is first compiled to a temporary module ``t`` and
     1450        executed via::
     1451
     1452            from t import *
    14511453
    14521454    INPUT:
    14531455
     
    14571459    - ``globals`` -- a string:object dictionary; the context in which
    14581460      to evaluate the ``filename`` and exec its contents
    14591461
    1460     - ``attach`` -- a boolean (default: False); whether to add the
     1462    - ``attach`` -- a boolean (default: ``False``); whether to add the
    14611463      file to the list of attached files
     1464     
     1465    - ``preparse_to_file`` -- a boolean or (the default) ``None``;
     1466      whether to preparse .sage files to a file (for usable tracebacks)
     1467      or only to memory (for speed). If ``None``, then take
     1468      ``preparse_to_file=attach``.
    14621469
    14631470    EXAMPLES:
    14641471
     
    15581565        v = filename.split()
    15591566        if len(v) > 1:
    15601567            for file in v:
    1561                 load(file, globals, attach=attach)
     1568                load(file, globals, attach=attach, preparse_to_file=preparse_to_file)
    15621569            return
    15631570
    15641571    filename = filename.strip()
     
    15931600    if fpath.endswith('.py'):
    15941601        execfile(fpath, globals)
    15951602    elif fpath.endswith('.sage'):
    1596         exec(preparse_file(open(fpath).read()) + "\n", globals)
     1603        if preparse_to_file is None:
     1604            preparse_to_file = attach
     1605        if preparse_to_file:
     1606            # Preparse to a file to enable tracebacks with
     1607            # code snippets. Use preparse_file_named to make
     1608            # the file name appear in the traceback as well.
     1609            # See Trac 11812.
     1610            from sage.misc.interpreter import preparse_file_named         
     1611            execfile(preparse_file_named(fpath), globals)
     1612        else:
     1613            # Preparse in memory only for speed.
     1614            exec(preparse_file(open(fpath).read()) + "\n", globals)
    15971615    elif fpath.endswith('.spyx') or fpath.endswith('.pyx'):
    15981616        import interpreter
    15991617        exec(interpreter.load_cython(fpath), globals)