Ticket #11812: trac_11812-traceback_attach.patch

File trac_11812-traceback_attach.patch, 4.2 KB (added by Marco Streng, 11 years ago)
  • sage/misc/preparser.py

    # HG changeset patch
    # User Marco Streng <marco.streng@gmail.com>
    # Date 1316701202 -3600
    # Node ID fb13ef7e630b307416313d068ad3fa70f9577a86
    # Parent  2a2abbcad325ccca9399981ceddf5897eb467e64
    trac 11812: tracebacks for attach
    
    diff -r 2a2abbcad325 -r fb13ef7e630b 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
     
    14591459
    14601460    - ``attach`` -- a boolean (default: False); whether to add the
    14611461      file to the list of attached files
     1462     
     1463    - ``preparse_to_file`` -- a boolean or None; whether to preparse
     1464      sage files to a file (for usable tracebacks) or only to memory
     1465      (for speed). If None, then take preparse_to_file=attach.
    14621466
    14631467    EXAMPLES:
    14641468
     
    15481552        $ sage
    15491553        sage: load_attach_path()          # not tested
    15501554        ['.', '/path/to/my/library', '/path/to/utils']
     1555       
     1556    When using `attach` for .sage files, the default settings give tracebacks
     1557    that include file names and line numbers. For efficiency reasons, this may
     1558    not be true for `load`::
     1559   
     1560        sage: sage.misc.reset.reset_attached(); reset_load_attach_path() # clean up
     1561        sage: t_dir = tmp_dir()
     1562        sage: fullpath = os.path.join(t_dir, 'myfile.sage')
     1563        sage: f = open(fullpath, 'w')
     1564        sage: f.write("\n")
     1565        sage: f.write("# We are going to divide by zero.\n")
     1566        sage: f.write("# Ok, here goes:\n")
     1567        sage: f.write("1/0\n")
     1568        sage: f.write("# And this will come after.\n")
     1569        sage: f.write("# Or not.")
     1570        sage: f.flush()
     1571        sage: load_attach_path(t_dir)
     1572       
     1573        sage: attach('myfile.sage')
     1574        Traceback (most recent call last):
     1575        ... myfile_sage_0.py in <module>()
     1576              7 # We are going to divide by zero.
     1577              8 # Ok, here goes:
     1578        ----> 9 _sage_const_1 /_sage_const_0
     1579             10 # And this will come after.
     1580             11 # Or not.
     1581        ...
     1582        ZeroDivisionError: Rational division by zero
     1583       
     1584        sage: load('myfile.sage')
     1585        Traceback (most recent call last):
     1586        ...
     1587        .../<string> in <module>()
     1588        ...
     1589        ZeroDivisionError: Rational division by zero
     1590       
     1591    TESTS:
     1592   
     1593    To check the content of the Traceback of the last example in automated
     1594    doctests, we use pexpect::
     1595   
     1596        sage: import pexpect
     1597        sage: s = pexpect.spawn('sage')
     1598        sage: t = s.sendline("load_attach_path('" + t_dir + "')")
     1599        sage: t = s.sendline("attach('myfile.sage')")
     1600        sage: t = s.expect('Rational division by zero', timeout=90)
     1601        sage: s.before.find("8 # Ok, here goes:") > 0
     1602        True
     1603        sage: s.before.find("----> 9 _sage_const_1 /_sage_const_0") > 0
     1604        True
     1605        sage: s.before.find("myfile") > 0
     1606        True
    15511607    """
    15521608    try:
    15531609        filename = eval(filename, globals)
     
    15581614        v = filename.split()
    15591615        if len(v) > 1:
    15601616            for file in v:
    1561                 load(file, globals, attach=attach)
     1617                load(file, globals, attach=attach, preparse_to_file=preparse_to_file)
    15621618            return
    15631619
    15641620    filename = filename.strip()
     
    15931649    if fpath.endswith('.py'):
    15941650        execfile(fpath, globals)
    15951651    elif fpath.endswith('.sage'):
    1596         exec(preparse_file(open(fpath).read()) + "\n", globals)
     1652        if preparse_to_file == None:
     1653            preparse_to_file = attach
     1654        if preparse_to_file:   
     1655            from sage.misc.interpreter import preparse_file_named         
     1656            execfile(preparse_file_named(fpath), globals)
     1657        else:
     1658            exec(preparse_file(open(fpath).read()) + "\n", globals)
    15971659    elif fpath.endswith('.spyx') or fpath.endswith('.pyx'):
    15981660        import interpreter
    15991661        exec(interpreter.load_cython(fpath), globals)