Ticket #652: ncalexan-652.patch

File ncalexan-652.patch, 3.4 KB (added by ncalexan, 15 years ago)
  • sage/misc/interpreter.py

    # HG changeset patch
    # User Nick Alexander <ncalexander@gmail.com>
    # Date 1200778236 28800
    # Node ID 013ce46490d39dfc8d2192c725aded799998908f
    # Parent  9d2d4aeaf414259f8a2b77f1231ddfffe5684b84
    Address #652: at http://trac.sagemath.org/sage_trac/ticket/652.  Now 'load'
    preparses .sage files to .py files in temporary directories.
    
    diff -r 9d2d4aeaf414 -r 013ce46490d3 sage/misc/interpreter.py
    a b def do_prefilter_paste(line, continuatio 
    185185                    if F.endswith('.py'):
    186186                        _ip.magic('run -i "%s"'%F)
    187187                    elif F.endswith('.sage'):
    188                         _ip.magic('run -i "%s"'%process_file(F))
     188                        _ip.magic('run -i "%s"'%preparse_file_named(F))
    189189                    elif F.endswith('.spyx') or F.endswith('.pyx'):
    190190                        X = load_cython(F)
    191191                        __IPYTHON__.push(X)
    def do_prefilter_paste(line, continuatio 
    276276                    raise ImportError, "Error loading '%s'"%name
    277277            elif name.endswith('.sage'):
    278278                try:
    279                     line = '%run -i "' + process_file(name) + '"'
     279                    line = '%run -i "' + preparse_file_named(name) + '"'
    280280                except IOError, s:
    281281                    print s
    282282                    raise ImportError, "Error loading '%s'"%name
    def do_prefilter_paste(line, continuatio 
    330330                raise ImportError, "File '%s' not found."%name
    331331        elif name.endswith('.sage'):
    332332            try:
    333                 line = '%run -i "' + process_file(name) + '"'
     333                line = '%run -i "' + preparse_file_named(name) + '"'
    334334                attached[name] = os.path.getmtime(name)
    335335            except IOError, OSError:
    336336                raise ImportError, "File '%s' not found."%name
    def load_cython(name): 
    359359    import sys
    360360    sys.path.append(dir)
    361361    return 'from %s import *'%mod
    362 
    363 def process_file(name):
     362   
     363def preparse_file_named_to_stream(name, out):
     364    r"""
     365    Preparse file named \code{name} (presumably a .sage file), outputting to
     366    stream \code{out}.
     367    """
    364368    cur = os.path.abspath(os.curdir)
    365369    dir, name = os.path.split(os.path.abspath(name))
    366     name2 = "%s/%s.py"%(dir,name[:name.find('.')])
    367370    os.chdir(dir)
    368371    contents = open(name).read()
    369372    parsed = preparse_file(contents, attached, do_time=True)
    370373    os.chdir(cur)
    371     W = open(name2,'w')
    372     W.write('#'*70+'\n')
    373     W.write('# This file was *autogenerated* from the file %s.\n'%name)
    374     W.write('#'*70+'\n')
    375     W.write(parsed)
    376     W.close()
    377     return name2
    378    
     374    out.write('#'*70+'\n')
     375    out.write('# This file was *autogenerated* from the file %s.\n'%os.path.abspath(name))
     376    out.write('#'*70+'\n')
     377    out.write(parsed)
     378
     379def preparse_file_named(name):
     380    r"""
     381    Preparse file named \code{name} (presumably a .sage file), outputting to a
     382    temporary file.  Returns name of temporary file.
     383    """
     384    import sage.misc.misc
     385    dir, name = os.path.split(os.path.abspath(name))
     386    tmpfilename = sage.misc.misc.tmp_filename(name) + ".py"
     387    out = open(tmpfilename,'w')
     388    preparse_file_named_to_stream(name, out)
     389    out.close()
     390    return tmpfilename
    379391
    380392def sage_prefilter(self, block, continuation):
    381393    """