Ticket #4975: trac_4975.patch

File trac_4975.patch, 1.9 KB (added by mhansen, 14 months ago)
  • sage-preparse

    # HG changeset patch
    # User Mike Hansen <mhansen@gmail.com>
    # Date 1232360477 28800
    # Node ID bc35da7a04ebbed6ad516372ab159b4e9f4f8cb1
    # Parent  68bc7d11954e0b49a97750c6b2bfd9b623efcc0b
    Fixed #4975: Sage chokes on utf-8 encoded files
    
    diff -r 68bc7d11954e -r bc35da7a04eb sage-preparse
    a b  
    1717files = sys.argv[2:] 
    1818 
    1919# There must be at least 1 file or we display an error/usage message 
    20 # and exit.  
     20# and exit 
    2121if len(files) == 0: 
    2222    print "Usage: %s <file1.sage> <file2.sage>..."%sys.argv[0] 
    2323    print "Creates files file1.py, file2.py ... that are the Sage" 
     
    7878    #if os.path.exists(fname) and os.path.getmtime(fname) >= os.path.getmtime(f): 
    7979    #    return 
    8080 
    81     # Finally open the file and preparse it. 
     81    # Finally open the file 
    8282    F = open(f).read() 
     83 
     84    #Check to see if a coding is specified in the .sage file. 
     85    #If it is, then we want to copy it over to the new file 
     86    #and not include it in the preprocessing 
     87    if F.startswith('# -*- coding:'): 
     88        end = F.find('\n') 
     89        coding = F[:end+1] if end != -1 else F 
     90        F = F[len(coding):] 
     91    else: 
     92        coding = '' 
     93 
     94    #Preparse it 
    8395    from sage.misc.preparser  import preparse_file 
    8496    G = preparse_file(F, magic=False, do_time=True, ignore_prompts=False) 
    8597 
    8698    # Check for load/attach commands. 
    8799    G = do_load_and_attach(G, f, files_before) 
    88100 
     101 
    89102    # Put the Sage library include along with a autogen message in the file. 
    90103    # It is ** critical ** that we put this after the mdoule docstring, since 
    91104    # otherwise the module docstring will disappear.  
    92     insert = '%s%s.\nfrom sage.all_cmdline import *   # import sage library\n'%(AUTOGEN_MSG, f) 
     105    insert = '%s%s%s.\nfrom sage.all_cmdline import *   # import sage library\n'%(coding, AUTOGEN_MSG, f) 
    93106    i = find_position_right_after_module_docstring(G) 
    94107    G = G[:i] + insert + G[i:] 
    95108