Ticket #652: ncalexan-652-updated.patch

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

    diff -r f45bbd14ad23 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    """
     368    name = os.path.abspath(name)
     369    dir, _ = os.path.split(name)
    364370    cur = os.path.abspath(os.curdir)
    365     dir, name = os.path.split(os.path.abspath(name))
    366     name2 = "%s/%s.py"%(dir,name[:name.find('.')])
    367371    os.chdir(dir)
    368372    contents = open(name).read()
    369373    parsed = preparse_file(contents, attached, do_time=True)
    370374    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    
     375    out.write('#'*70+'\n')
     376    out.write('# This file was *autogenerated* from the file %s.\n' % name)
     377    out.write('#'*70+'\n')
     378    out.write(parsed)
     379
     380def preparse_file_named(name):
     381    r"""
     382    Preparse file named \code{name} (presumably a .sage file), outputting to a
     383    temporary file.  Returns name of temporary file.
     384    """
     385    import sage.misc.misc
     386    name = os.path.abspath(name)
     387    tmpfilename = os.path.abspath(sage.misc.misc.tmp_filename(name) + ".py")
     388    out = open(tmpfilename,'w')
     389    preparse_file_named_to_stream(name, out)
     390    out.close()
     391    return tmpfilename
    379392
    380393def sage_prefilter(self, block, continuation):
    381394    """
  • sage/misc/misc.py

    diff -r f45bbd14ad23 sage/misc/misc.py
    a b __tmp_n = 0 
    13661366__tmp_n = 0
    13671367
    13681368def tmp_filename(name='tmp'):
    1369     name = str(name)
     1369    name = list(str(name))
     1370    for i in range(len(name)):
     1371        # protect against paths with slashes, colons, etc
     1372        if not (name[i].isalpha() or name[i].isdigit()):
     1373            name[i] = '_'
     1374    name = ''.join(name)
     1375
    13701376    global __tmp_n
    13711377    while True:
    13721378        tmp = "%s/%s_%s"%(SAGE_TMP, name, __tmp_n)